Skip to content

Commit 09267f4

Browse files
Add files via upload
1 parent ff6bdec commit 09267f4

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
for _ in range(int(input())):
2+
n = int(input())
3+
a = list(input())
4+
a.reverse()
5+
6+
top = n
7+
bottom = 1
8+
9+
ans = []
10+
for x in a:
11+
if x == '<':
12+
ans.append(bottom)
13+
bottom += 1
14+
else:
15+
ans.append(top)
16+
top -= 1
17+
18+
ans.append(bottom)
19+
20+
ans.reverse()
21+
22+
print(*ans)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
for _ in range(int(input())):
2+
n, k = map(int, input().split())
3+
4+
rs = map(int, input().split())
5+
ls = map(int, input().split())
6+
7+
together = [(r, l) for r, l in zip(rs, ls)]
8+
together.sort(key=lambda x: min(x[0], x[1]), reverse=True)
9+
10+
ans = sum(max(x[0], x[1]) for x in together)
11+
ans += sum(min(x[0], x[1]) for x in together[:k-1])
12+
ans += 1
13+
14+
print(ans)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
#define fast_cin() \
9+
ios_base::sync_with_stdio(false); \
10+
cin.tie(NULL); \
11+
cout.tie(NULL);
12+
13+
int n;
14+
vector<vector<int>> heights;
15+
vector<vector<ll>> memo;
16+
vector<ll> costs;
17+
constexpr ll inf = 1e18;
18+
ll dp(int idx, int prev_up) {
19+
if (idx == n) return 0;
20+
if (memo[idx][prev_up] != -1) return memo[idx][prev_up];
21+
if (idx == 0) return min(dp(idx + 1, 0), dp(idx + 1, 1) + costs[idx]);
22+
23+
// we need to check if we can no upgrade this current row
24+
bool same_can = true;
25+
for (int c = 0; c < n; c++) {
26+
if (heights[idx][c] == heights[idx - 1][c] + prev_up) {
27+
same_can = false;
28+
break;
29+
}
30+
}
31+
32+
bool up_can = true;
33+
for (int c = 0; c < n; c++) {
34+
if (heights[idx][c] + 1 == heights[idx - 1][c] + prev_up) {
35+
up_can = false;
36+
break;
37+
}
38+
}
39+
40+
if (same_can && up_can) {
41+
return memo[idx][prev_up] = min(dp(idx + 1, 0), dp(idx + 1, 1) + costs[idx]);
42+
} else if (same_can) {
43+
return memo[idx][prev_up] = dp(idx + 1, 0);
44+
} else if (up_can) {
45+
return memo[idx][prev_up] = dp(idx + 1, 1) + costs[idx];
46+
} else {
47+
return memo[idx][prev_up] = inf;
48+
}
49+
}
50+
51+
void solve() {
52+
cin >> n;
53+
heights.assign(n, vector<int>(n));
54+
memo.assign(n, vector<ll>(2, -1));
55+
56+
for (int i = 0; i < n; i++) {
57+
for (int j = 0; j < n; j++) {
58+
cin >> heights[i][j];
59+
}
60+
}
61+
62+
costs.assign(n, 0);
63+
for (auto &x : costs) {
64+
cin >> x;
65+
}
66+
67+
ll ans = dp(0, 0);
68+
69+
// transpose the matrix
70+
for (int i = 0; i < n; i++) {
71+
for (int j = i + 1; j < n; j++) {
72+
swap(heights[i][j], heights[j][i]);
73+
}
74+
}
75+
76+
memo.assign(n, vector<ll>(2, -1));
77+
for (auto &x : costs) {
78+
cin >> x;
79+
}
80+
81+
ans += dp(0, 0);
82+
cout << (ans >= inf ? -1 : ans) << '\n';
83+
}
84+
85+
int main() {
86+
int tc;
87+
cin >> tc;
88+
while (tc--) solve();
89+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from collections import defaultdict
2+
for _ in range(int(input())):
3+
4+
n = int(input())
5+
pts: list[tuple[int, int]] = []
6+
for i in range(n):
7+
x, y = map(int, input().split())
8+
pts.append((x, y))
9+
10+
by_x: dict[int, list[int]] = defaultdict(lambda: []) # number of y at point x
11+
for i in range(n):
12+
by_x[pts[i][0]].append(pts[i][1])
13+
14+
# get the only one with an odd number of y
15+
odd_x = None
16+
for x, ys in by_x.items():
17+
if len(ys) % 2 == 1:
18+
odd_x = x
19+
break
20+
21+
assert odd_x is not None, "There should be an odd number of y at some x"
22+
23+
# print("the x", odd_x)
24+
25+
s = set(y for y in by_x[odd_x]) # items on the odd_x
26+
27+
for x in by_x:
28+
if x == odd_x:
29+
continue
30+
31+
if x < odd_x: # shift downward
32+
diff = odd_x - x
33+
for y in by_x[x]:
34+
ny = y - diff
35+
if ny in s:
36+
s.remove(ny)
37+
else:
38+
s.add(ny)
39+
else: # shift upward
40+
diff = x - odd_x
41+
for y in by_x[x]:
42+
ny = y + diff
43+
if ny in s:
44+
s.remove(ny)
45+
else:
46+
s.add(ny)
47+
48+
assert len(s) == 1, "There should be only one y left"
49+
50+
print(odd_x, s.pop())
51+
52+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
def prune(arr):
2+
# remove any left bs
3+
# remove any right ps
4+
n = len(arr)
5+
num_left_bs = 0
6+
for i in range(n):
7+
if arr[i] == "B":
8+
num_left_bs += 1
9+
else:
10+
break
11+
num_right_ps = 0
12+
for i in range(n - 1, -1, -1):
13+
if arr[i] == "P":
14+
num_right_ps += 1
15+
else:
16+
break
17+
18+
return arr[num_left_bs : n - num_right_ps]
19+
20+
21+
def f(n: int) -> int:
22+
if n % 2 == 0:
23+
k = n // 2
24+
return k * (k + 1)
25+
else:
26+
k = n // 2
27+
return (k + 1) * (k + 1)
28+
29+
30+
for _ in range(int(input())):
31+
n = int(input())
32+
arr = list(input())
33+
arr = prune(arr)
34+
n = len(arr)
35+
36+
num_p_on_left = [0] * n
37+
num_b_on_right = [0] * n
38+
39+
for i in range(n):
40+
num_p_on_left[i] = (num_p_on_left[i - 1] if i > 0 else 0) + (arr[i] == "P")
41+
42+
for i in range(n - 1, -1, -1):
43+
num_b_on_right[i] = (num_b_on_right[i + 1] if i < n - 1 else 0) + (
44+
arr[i] == "B"
45+
)
46+
47+
# print("arr", arr)
48+
ans = 0
49+
st: list = []
50+
p_edit = 0
51+
for i in range(n):
52+
if arr[i] == "P":
53+
if st and st[-1][0] == "P":
54+
ans += num_b_on_right[i]
55+
# print("added", num_b_on_right[i])
56+
st.pop()
57+
p_edit += 2
58+
else:
59+
st.append((arr[i], None))
60+
elif arr[i] == "B":
61+
if st and st[-1][0] == "B":
62+
ans += st[-1][1]
63+
# print("added", st[-1][1])
64+
st.pop()
65+
else:
66+
st.append((arr[i], num_p_on_left[i] - p_edit))
67+
# print(st)
68+
69+
arr = prune([x[0] for x in st])
70+
# print("arr", arr)
71+
72+
ans += f(len(arr) // 2)
73+
print(ans)
74+

0 commit comments

Comments
 (0)