Skip to content

Commit 57a1ec4

Browse files
committed
modified: .vscode/settings.json
new file: Dijkstra.java renamed: bytedance/闪充总时间计算.py -> bytedance/充电总时间计算.py new file: bytedance/兔群增殖之谜.py new file: bytedance/小U的生活事件快乐值最大化.py new file: bytedance/最大连续子数组和问题.py new file: bytedance/英雄升级与奖励最大化.py new file: luogu/P1160.cpp modified: test.cpp modified: test.py new file: test2.cpp
1 parent 39eef60 commit 57a1ec4

11 files changed

+397
-28
lines changed

.vscode/settings.json

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,99 @@
33
"*.asm": "assembly",
44
"*.py": "python",
55
".clang-format": "yaml",
6-
"ostream": "cpp"
6+
"ostream": "cpp",
7+
"bitset": "cpp",
8+
"iostream": "cpp",
9+
"any": "cpp",
10+
"array": "cpp",
11+
"atomic": "cpp",
12+
"barrier": "cpp",
13+
"bit": "cpp",
14+
"*.tcc": "cpp",
15+
"cctype": "cpp",
16+
"cfenv": "cpp",
17+
"charconv": "cpp",
18+
"chrono": "cpp",
19+
"cinttypes": "cpp",
20+
"clocale": "cpp",
21+
"cmath": "cpp",
22+
"codecvt": "cpp",
23+
"compare": "cpp",
24+
"complex": "cpp",
25+
"concepts": "cpp",
26+
"condition_variable": "cpp",
27+
"coroutine": "cpp",
28+
"csetjmp": "cpp",
29+
"csignal": "cpp",
30+
"cstdarg": "cpp",
31+
"cstddef": "cpp",
32+
"cstdint": "cpp",
33+
"cstdio": "cpp",
34+
"cstdlib": "cpp",
35+
"cstring": "cpp",
36+
"ctime": "cpp",
37+
"cuchar": "cpp",
38+
"cwchar": "cpp",
39+
"cwctype": "cpp",
40+
"deque": "cpp",
41+
"forward_list": "cpp",
42+
"list": "cpp",
43+
"map": "cpp",
44+
"set": "cpp",
45+
"string": "cpp",
46+
"unordered_map": "cpp",
47+
"unordered_set": "cpp",
48+
"vector": "cpp",
49+
"exception": "cpp",
50+
"expected": "cpp",
51+
"algorithm": "cpp",
52+
"functional": "cpp",
53+
"iterator": "cpp",
54+
"memory": "cpp",
55+
"memory_resource": "cpp",
56+
"numeric": "cpp",
57+
"optional": "cpp",
58+
"random": "cpp",
59+
"ratio": "cpp",
60+
"regex": "cpp",
61+
"source_location": "cpp",
62+
"string_view": "cpp",
63+
"system_error": "cpp",
64+
"tuple": "cpp",
65+
"type_traits": "cpp",
66+
"utility": "cpp",
67+
"format": "cpp",
68+
"fstream": "cpp",
69+
"future": "cpp",
70+
"generator": "cpp",
71+
"initializer_list": "cpp",
72+
"iomanip": "cpp",
73+
"iosfwd": "cpp",
74+
"istream": "cpp",
75+
"latch": "cpp",
76+
"limits": "cpp",
77+
"mutex": "cpp",
78+
"new": "cpp",
79+
"numbers": "cpp",
80+
"print": "cpp",
81+
"ranges": "cpp",
82+
"scoped_allocator": "cpp",
83+
"semaphore": "cpp",
84+
"shared_mutex": "cpp",
85+
"span": "cpp",
86+
"spanstream": "cpp",
87+
"sstream": "cpp",
88+
"stacktrace": "cpp",
89+
"stdexcept": "cpp",
90+
"stdfloat": "cpp",
91+
"stop_token": "cpp",
92+
"streambuf": "cpp",
93+
"syncstream": "cpp",
94+
"text_encoding": "cpp",
95+
"thread": "cpp",
96+
"typeindex": "cpp",
97+
"typeinfo": "cpp",
98+
"valarray": "cpp",
99+
"variant": "cpp"
7100
}
8101
}

Dijkstra.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Dijkstra {
2+
public int[] findShortestPath(int[][] graph, int start) {
3+
int n = graph.length;
4+
int[] distances = new int[n];
5+
boolean[] visited = new boolean[n];
6+
7+
// 初始化距离数组
8+
for (int i = 0; i < n; i++) {
9+
distances[i] = Integer.MAX_VALUE;
10+
}
11+
distances[start] = 0;
12+
13+
// 主循环
14+
for (int count = 0; count < n - 1; count++) {
15+
// 找到未访问节点中距离最小的
16+
int minDistance = Integer.MAX_VALUE;
17+
int minIndex = -1;
18+
19+
for (int v = 0; v < n; v++) {
20+
if (!visited[v] && distances[v] < minDistance) {
21+
minDistance = distances[v];
22+
minIndex = v;
23+
}
24+
}
25+
26+
// 标记为已访问
27+
visited[minIndex] = true;
28+
29+
// 更新相邻节点的距离
30+
for (int v = 0; v < n; v++) {
31+
if (!visited[v] && graph[minIndex][v] != 0
32+
&& distances[minIndex] != Integer.MAX_VALUE
33+
&& distances[minIndex] + graph[minIndex][v] < distances[v]) {
34+
distances[v] = distances[minIndex] + graph[minIndex][v];
35+
}
36+
}
37+
}
38+
return distances;
39+
}
40+
}

bytedance/兔群增殖之谜.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def solution(A):
2+
# Write your code here.
3+
if A == 1:
4+
return 1
5+
a, b = 1, 0
6+
for _ in range(2, A + 1):
7+
a, b = a + b, a
8+
return a + b
9+
10+
11+
if __name__ == "__main__":
12+
# Add your test cases here
13+
print(solution(2) == 2)
14+
print(solution(5) == 8)
15+
print(solution(1) == 1)
16+
print(solution(15) == 987)
17+
print(solution(50) == 20365011074)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def solution(n: int, T: int, H: int, t: list, h: list, a: list) -> int:
2+
dp = [[0] * (H + 1) for _ in range(T + 1)]
3+
for i in range(n):
4+
for j in range(T, t[i] - 1, -1):
5+
for k in range(H, h[i] - 1, -1):
6+
dp[j][k] = max(dp[j][k], dp[j - t[i]][k - h[i]] + a[i])
7+
return dp[T][H]
8+
9+
if __name__ == '__main__':
10+
print(solution(n = 2, T = 2, H = 2, t = [1, 3], h = [3, 1], a = [3, 4]) == 0)
11+
print(solution(n = 3, T = 5, H = 5, t = [2, 1, 3], h = [1, 3, 2], a = [10, 7, 8]) == 18)
12+
print(solution(n = 1, T = 3, H = 3, t = [4], h = [4], a = [5]) == 0)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 题解:https://bytedance.larkoffice.com/docx/W4q5dpCaDok6AFxqKyUcq2Y7ndc
2+
'''
3+
朴素的思路:枚举替换的位置,然后计算替换后的最大子数组和
4+
对于每个位置i,计算包含a[i]的最大子数组和的方法是先计算前面的元素的贡献和后面的元素的贡献,然后再加上max(a[i], x)。而计算贡献可以用前缀和来优化。
5+
6+
这道题与最大子数组和的区别在于,最大子数组和是不允许替换元素的,而这道题是允许替换元素的。对于前者,我们可以枚举每个位置i,然后计算以a[i]结尾的最大子数组和,最后取最大值。但是在这道题中,如果采用同样方法,我们就无法考虑替换后的a[i]对后面的数的贡献。因此我们枚举的是包含a[i]的最大子数组和,而不是以a[i]结尾的最大子数组和。
7+
'''
8+
def solution(n: int, x: int, a: list) -> int:
9+
# 将a数组中的最小值替换为x
10+
m = min(a)
11+
if x < m:
12+
a[a.index(m)] = x
13+
pre, suf = [0] * (n + 1), [0] * (n + 1)
14+
for i in range(n):
15+
pre[i] = max(0, pre[i - 1] + a[i - 1] if i > 0 else 0)
16+
for i in range(n - 1, -1, -1):
17+
suf[i] = max(0, suf[i + 1] + a[i + 1] if i < n - 1 else 0)
18+
return max(pre[i] + suf[i] + max(x, a[i]) for i in range(n))
19+
20+
21+
if __name__ == '__main__':
22+
print(solution(n = 5, x = 10, a = [5, -1, -5, -3, 2]) == 15)
23+
print(solution(n = 2, x = -3, a = [-5, -2]) == -2)
24+
print(solution(n = 6, x = 10, a = [4, -2, -11, -1, 4, -1]) == 15)
25+
print(solution(n = 16, x = 1, a = [17,17,4,13,11,3,6,13,7,13,13,13,6,16,6,11]) == 167)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''
2+
题目链接:https://www.marscode.cn/practice/8ee68j852y64nl?problem_id=7414004855076470828
3+
官方题解:https://bytedance.larkoffice.com/docx/OJSBd0oBcorDvwxxWATcF99enKb
4+
5+
想要获得对应的奖励,小W需要将英雄的初始能力值升级到 b_i,并且所有英雄的初始能力值都为 1,所以我们可以先对每个目标能力值 b_i 预处理出对应所需的升级次数,用数组 d 表示, d[i] 表示想达到目标值 i 所需的升级次数
6+
在处理出 d 数组后,我们将问题转化为了:有 n 个英雄,最多通过k 次升级操作来提升英雄的能力值,然后每个英雄需要 d[b[i]]次操作,才能获得对应的奖励 c[i]。这是一个经典的01背包问题,套上模版即可通过
7+
8+
难点:如何处理出 d 数组
9+
d[t]表示从1到t需要的最小操作次数
10+
操作的规定为:
11+
* 选择一个正整数x
12+
* a = a + a // x (即下取整)
13+
思路:
14+
对于正整数t,它一定是由一个比它小的数i加上一个数i//j得到的,其中j是一个正整数。
15+
j <= i,因为j > i时,i//j = 0,没有意义。
16+
正向思维:我们可以枚举i和j,然后计算出t,然后更新d[t] = min(d[t], d[i] + 1),代码为:
17+
for i in range(1, t):
18+
for j in range(1, i + 1):
19+
x = i + i // j
20+
if x == t:
21+
d[t] = min(d[i] + 1, d[t])
22+
这是求解一个d[t]的方法。
23+
24+
但是我们需要求解d[1]到d[m]的值,其中m是b数组中的最大值。
25+
因此:我们可以枚举从1到m的数,对于每个数i,我们枚举j,然后计算出它所有可以一步到达的数t,如果t <= m,那么我们就更新d[t] = min(d[i] + 1, d[t])。由于t一定是由比它小的某个数一步到达的,因此我们可以保证枚举完d[t]的所有情况,将它更新为最小。代码为:
26+
for i in range(1, m + 1):
27+
for j in range(1, i + 1):
28+
t = i + i // j
29+
if t <= m:
30+
d[t] = min(d[i] + 1, d[t])
31+
32+
33+
总结:这个思路的来源是一个数的最优解与比它小的数的最优解有关,因此我们可以从小到大递推,最终得到所有数的最优解。
34+
'''
35+
def solution(n, k, b, c):
36+
m = max(b)
37+
d = [1e9] * (m + 1)
38+
d[1] = 0
39+
for i in range(1, m + 1):
40+
for j in range(1, i + 1):
41+
t = i + i // j
42+
if t <= m:
43+
d[t] = min(d[i] + 1, d[t])
44+
cost = [int(d[i]) for i in b]
45+
# 从n个人(物品)中选,最多选k个(背包容量是k),每个人(物品)的体积是cost,价值是c
46+
# 要求选出的物品的价值和最大
47+
# 01背包问题
48+
dp = [0] * (k + 1)
49+
for i in range(n):
50+
for j in range(k, cost[i] - 1, -1):
51+
dp[j] = max(dp[j], dp[j - cost[i]] + c[i])
52+
return dp[k]
53+
54+
55+
if __name__ == "__main__":
56+
# Add your test cases here
57+
print(solution(4, 4, [1, 7, 5, 2], [2, 6, 5, 2]) == 9)
58+
print(solution(3, 0, [3, 5, 2], [5, 4, 7]) == 0)
59+
print(solution(3, 3, [3, 5, 2], [5, 4, 7]) == 12)

luogu/P1160.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
这个程序实现了一个双向链表的插入和删除操作。程序首先读取一个整数 n,表示要插入的节点数量。接着读取 n-1 对整数 k 和 p,表示在节点 k 的左边或右边插入新节点。然后读取一个整数 m,表示要删除的节点数量,并读取 m 个整数表示要删除的节点。最后输出链表中剩余的节点。
3+
4+
具体步骤如下:
5+
1. 定义一个结构体 Node 表示链表节点,包含左右指针。
6+
2. 定义一个数组 q 存储链表节点,数组 has_deleted 标记节点是否被删除。
7+
3. 实现插入和删除操作的函数 insert 和 remove。
8+
4. 在主函数中读取输入,执行插入和删除操作,最后输出链表中剩余的节点。
9+
*/
10+
11+
#include <bits/stdc++.h>
12+
using namespace std;
13+
14+
using ll = long long;
15+
using pii = pair<int, int>;
16+
17+
const int N = 1e5 + 10;
18+
19+
// 定义链表节点结构体
20+
struct Node
21+
{
22+
int l, r; // 左右指针
23+
};
24+
Node q[N]; // 存储链表节点的数组
25+
bool has_deleted[N]; // 标记节点是否被删除
26+
27+
// 在节点 a 的右边插入节点 x
28+
void insert(int a, int x)
29+
{
30+
q[x].r = q[a].r; // x 的右指针指向 a 的右指针指向的节点
31+
q[x].l = a; // x 的左指针指向 a
32+
q[q[a].r].l = x; // a 的右指针指向的节点的左指针指向 x
33+
q[a].r = x; // a 的右指针指向 x
34+
}
35+
36+
// 删除节点 x
37+
void remove(int x)
38+
{
39+
q[q[x].l].r = q[x].r; // x 的左指针指向的节点的右指针指向 x 的右指针指向的节点
40+
q[q[x].r].l = q[x].l; // x 的右指针指向的节点的左指针指向 x 的左指针指向的节点
41+
}
42+
43+
int main()
44+
{
45+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); // 关闭同步,加快读入输入的速度
46+
47+
int n;
48+
cin >> n; // 读取节点数量
49+
50+
// 将0作为链表的头节点(不存数据),数据从1开始。数组下标就是节点的编号
51+
q[0].r = 1; // 初始化链表头节点,0的右边是1
52+
q[1].l = 0, q[1].r = -1; // 初始化链表第一个节点,1·的左边是0,右边设为-1,表示链表结束。
53+
for (int i = 2; i <= n; i++)
54+
{
55+
int k, p;
56+
cin >> k >> p; // 读取插入位置和方向
57+
if (p == 0) insert(q[k].l, i); // 在 k 的左边插入 i,在k的左边就是在k的左节点的右边插入i
58+
else insert(k, i); // 在 k 的右边插入 i
59+
}
60+
int m;
61+
cin >> m; // 读取要删除的节点数量
62+
for (int i = 0; i < m; i++)
63+
{
64+
int x;
65+
cin >> x; // 读取要删除的节点
66+
if (has_deleted[x]) continue; // 如果节点已经被删除,跳过
67+
else
68+
{
69+
has_deleted[x] = true; // 标记节点已删除
70+
remove(x); // 删除节点
71+
}
72+
}
73+
for (int i = q[0].r; i != -1; i = q[i].r) // 输出链表中剩余的节点,前面说过,0是头节点,0的右边是第一个节点。之前把1号的节点的右节点设为-1,标志结束。那在插入的时候,尾结点左边插入的话,-1可以标志结束。在尾结点右边插入的话,新的尾结点的右节点还是-1,删除时同理。因此在我们的插入和删除操作中,一直维护了-1作为链表的结束标志这一个特性。
74+
cout << i << ' ';
75+
return 0;
76+
}

0 commit comments

Comments
 (0)