Skip to content

Commit 11af1e3

Browse files
Add files via upload
1 parent 9bbaddb commit 11af1e3

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

Other_Tasks/CF2106_A_Dr_TC.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for _ in range(int(input())):
2+
n = int(input())
3+
s = str(input())
4+
print(sum(1 if c == '0' else n-1 for c in s))

Other_Tasks/CF2106_B_St_Chroma.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for _ in range(int(input())):
2+
n, x = map(int, input().split())
3+
arr = [i for i in range(x)] + [i for i in range(x+1, n)] + ([x] if x < n else [])
4+
print(*arr)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
for _ in range(int(input())):
2+
n, k = map(int, input().split())
3+
arr = list(map(int, input().split()))
4+
brr = list(map(int, input().split()))
5+
x = [(a + b) for a, b in zip(arr, brr) if b >= 0]
6+
7+
if len(x) == 0:
8+
smallest_x = max(arr)
9+
biggest_x = k + min(arr)
10+
print(max(0, biggest_x - smallest_x + 1))
11+
continue
12+
13+
if all(q == x[0] for q in x):
14+
q = x[0]
15+
can = all(q - a >= 0 and q - a <= k for (a, b) in zip(arr, brr) if b < 0)
16+
print(1 if can else 0)
17+
else:
18+
print(0)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
9+
int n, m;
10+
ll k;
11+
vector<int> arr;
12+
vector<int> brr;
13+
vector<array<int, 2>> memo;
14+
15+
int dp(int i, int avail) {
16+
if (memo[i][avail] != -1) return memo[i][avail];
17+
if (i == n) {
18+
if (avail == 0 || brr[m - 1] > k)
19+
return memo[i][avail] = m;
20+
else
21+
return memo[i][avail] = m - 1;
22+
}
23+
24+
int ans = 1e9;
25+
if (avail == 1) {
26+
// we try to use the magic flower now
27+
int j = dp(i, 0);
28+
if (j == 0) return memo[i][avail] = 0;
29+
if (brr[j - 1] <= k) {
30+
// we can use the flower
31+
ans = min(ans, j - 1);
32+
}
33+
}
34+
// don't worry about the flower
35+
int j = dp(i + 1, avail);
36+
if (j == 0) return memo[i][avail] = 0;
37+
if (brr[j - 1] <= arr[i]) {
38+
ans = min(ans, j - 1);
39+
} else {
40+
ans = min(ans, j);
41+
}
42+
return memo[i][avail] = ans;
43+
}
44+
45+
bool canDo(ll mid) {
46+
k = mid;
47+
memo.assign(n + 1, {-1, -1});
48+
return dp(0, 1) == 0;
49+
}
50+
51+
void solve() {
52+
cin >> n >> m;
53+
arr.resize(n);
54+
for (auto &x : arr) cin >> x;
55+
brr.resize(m);
56+
for (auto &x : brr) cin >> x;
57+
58+
constexpr ll inf = 1e18;
59+
ll lo = 0, hi = inf;
60+
61+
while (lo < hi) {
62+
ll mid = (lo + hi) / 2;
63+
if (canDo(mid)) {
64+
hi = mid;
65+
} else {
66+
lo = mid + 1;
67+
}
68+
}
69+
70+
if (lo == inf) {
71+
cout << -1 << endl;
72+
} else {
73+
cout << lo << endl;
74+
}
75+
}
76+
77+
int main() {
78+
int tc;
79+
cin >> tc;
80+
while (tc--) {
81+
solve();
82+
}
83+
84+
return 0;
85+
}

0 commit comments

Comments
 (0)