Skip to content

Commit c3dbb62

Browse files
committed
Bunch of random CF
1 parent d80efe4 commit c3dbb62

File tree

17 files changed

+866
-0
lines changed

17 files changed

+866
-0
lines changed

codeforces/1/a.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from math import ceil
2+
3+
def main():
4+
n, m, a = map(int, input().split())
5+
6+
w = int(ceil(n / a))
7+
h = int(ceil(m / a))
8+
9+
print(w * h)
10+
11+
main()

codeforces/1/b.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import re
2+
3+
def rc(s):
4+
valid = re.compile(r'R(\d+)C(\d+)')
5+
match = valid.match(s)
6+
if match is None:
7+
return None, None
8+
else:
9+
return list(map(int, match.groups()))
10+
11+
def excel_to_int(s):
12+
ans = 0
13+
for c in s:
14+
ans += ord(c) - ord('A')
15+
ans += 1
16+
ans *= 26
17+
18+
return ans // 26
19+
20+
def int_to_excel(x):
21+
out = []
22+
while x:
23+
x -= 1
24+
out.append(chr(ord('A') + int(x % 26)))
25+
x //= 26
26+
27+
return ''.join(reversed(out))
28+
29+
def main():
30+
n = int(input())
31+
for _ in range(n):
32+
s = input()
33+
x, y = rc(s)
34+
35+
if x is None:
36+
row_start = 0
37+
while s[row_start] not in '0123456789':
38+
row_start += 1
39+
40+
col_excel = s[:row_start]
41+
col = excel_to_int(col_excel)
42+
row = s[row_start:]
43+
44+
print('R{}C{}'.format(row, col))
45+
else:
46+
col = int_to_excel(y)
47+
print('{}{}'.format(col, x))
48+
49+
main()

codeforces/144/d.cpp

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
#include <algorithm>
4+
#include <utility>
5+
#include <vector>
6+
#include <map>
7+
#include <queue>
8+
9+
using namespace std;
10+
11+
const int MAXN = 100005;
12+
int n, m, start;
13+
long long l;
14+
15+
vector<pair<int, long long> > graph[MAXN];
16+
long long dist[MAXN];
17+
18+
typedef pair<long long, int> state;
19+
20+
void dijkstra() {
21+
// find the shortest path from the source to all nodes
22+
23+
memset(dist, -1, sizeof(dist));
24+
priority_queue<state, vector<state>, greater<state> > pq;
25+
pq.push(make_pair(0LL, start));
26+
27+
while (!pq.empty()) {
28+
state cur = pq.top();
29+
pq.pop();
30+
31+
long long dist_here = cur.first;
32+
int pos = cur.second;
33+
34+
if (dist[pos] != -1 and dist[pos] <= dist_here)
35+
continue;
36+
37+
dist[pos] = dist_here;
38+
39+
for (pair<int, long long> edge : graph[pos]) {
40+
int next_place = edge.first;
41+
long long next_dist = dist_here + edge.second;
42+
43+
if (dist[next_place] == -1 or dist_here < dist[next_place]) {
44+
pq.push(make_pair(next_dist, next_place));
45+
}
46+
}
47+
}
48+
}
49+
50+
int main() {
51+
scanf("%d %d %d", &n, &m, &start);
52+
53+
int u, v;
54+
long long w;
55+
for (int i = 0; i < m; ++i) {
56+
scanf("%d %d %lld", &u, &v, &w);
57+
graph[u].push_back(make_pair(v, w));
58+
graph[v].push_back(make_pair(u, w));
59+
}
60+
61+
scanf("%lld", &l);
62+
63+
dijkstra();
64+
65+
long long ans = 0;
66+
for (u = 1; u <= n; ++u) {
67+
// printf("City %d at dist %lld\n", u, dist[u]);
68+
if (dist[u] == l) {
69+
// printf("Silo at city %d\n", u);
70+
++ans;
71+
continue;
72+
}
73+
74+
if (dist[u] > l) {
75+
continue;
76+
}
77+
78+
for (pair<int, long long> edge : graph[u]) {
79+
v = edge.first;
80+
w = edge.second;
81+
82+
if (dist[u] + w <= l) {
83+
continue;
84+
}
85+
86+
// edge case: the distance is the same from either side
87+
if (u < v and
88+
dist[u] < l and l < dist[u] + w and
89+
dist[v] < l and l < dist[v] + w and
90+
dist[u] + dist[v] + w == 2 * l) {
91+
// printf("Midpoint silo between %d and %d\n", u, v);
92+
++ans;
93+
}
94+
95+
// consider the point coming from u
96+
if (dist[u] < l and l < dist[u] + w) {
97+
// the point is l - dist[u] away from u
98+
long long u_dist = l - dist[u];
99+
// the point is dist[v] + w - u_dist away from v
100+
if (l < dist[v] + w - u_dist) {
101+
// printf("Silo at distance %lld along edge between %d and %d\n", u_dist, u, v);
102+
++ans;
103+
}
104+
}
105+
}
106+
}
107+
108+
printf("%lld\n", ans);
109+
110+
return 0;
111+
}
File renamed without changes.

codeforces/493/a.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import defaultdict
2+
3+
def main():
4+
home = input()
5+
away = input()
6+
num_fouls = int(input())
7+
8+
players = defaultdict(int)
9+
banned = set()
10+
for _ in range(num_fouls):
11+
minute, team, player, card = input().split()
12+
team_name = home if team == 'h' else away
13+
14+
if card == 'y':
15+
players[(team_name, player)] += 1
16+
else:
17+
players[(team_name, player)] = 2
18+
19+
if (team_name, player) not in banned and players[(team_name, player)] >= 2:
20+
print(team_name, player, minute)
21+
banned.add((team_name, player))
22+
23+
24+
main()

codeforces/493/b.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def main():
2+
n = int(input())
3+
4+
p1, p2 = [], []
5+
6+
last = 1
7+
for _ in range(n):
8+
move = int(input())
9+
10+
if move > 0:
11+
p1.append(move)
12+
last = 1
13+
else:
14+
p2.append(-move)
15+
last = 2
16+
17+
if sum(p1) != sum(p2):
18+
print('first' if sum(p1) > sum(p2) else 'second')
19+
elif p1 != p2:
20+
print('first' if p1 > p2 else 'second')
21+
else:
22+
print('first' if last == 1 else 'second')
23+
24+
main()

codeforces/493/c.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
def lte(n, a, d):
2+
# count the number of values in a <= d
3+
if a[0] > d:
4+
return 0
5+
if a[-1] <= d:
6+
return n
7+
else:
8+
lo = 0
9+
hi = n - 1
10+
while lo + 1 < hi:
11+
mid = (lo + hi) // 2
12+
if a[mid] <= d:
13+
lo = mid
14+
else:
15+
hi = mid
16+
return lo + 1
17+
18+
def score(n, m, a, b, d):
19+
ca = lte(n, a, d)
20+
cb = lte(m, b, d)
21+
22+
a_score = 2 * ca + 3 * (n - ca)
23+
b_score = 2 * cb + 3 * (m - cb)
24+
25+
return a_score, b_score
26+
27+
28+
def main():
29+
n = int(input())
30+
a = list(map(int, input().split()))
31+
32+
m = int(input())
33+
b = list(map(int, input().split()))
34+
35+
a.sort()
36+
b.sort()
37+
38+
factor = 2 if 2 * (n - m) > 3 * (n - m) else 3
39+
best_diff = factor * (n - m)
40+
best_a = factor * n
41+
out = str(factor * n) + ':' + str(factor * m)
42+
43+
mega = set(a + b)
44+
45+
for d in mega:
46+
if d <= 0:
47+
continue
48+
49+
a_score, b_score = score(n, m, a, b, d)
50+
diff = a_score - b_score
51+
52+
if diff > best_diff or (diff == best_diff and a_score > best_a):
53+
best_diff = diff
54+
best_a = a_score
55+
out = str(a_score) + ':' + str(b_score)
56+
57+
print(out)
58+
59+
main()

codeforces/493/d.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
n = int(input())
2+
if n % 2 == 0:
3+
print("white")
4+
print("1 2")
5+
else:
6+
print("black")

codeforces/510/c.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
#include <algorithm>
4+
#include <utility>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
const int MAXA = 26;
10+
const int MAXN = 102;
11+
12+
int n;
13+
vector<int> graph[MAXA];
14+
15+
char strings[MAXN][2 * MAXN];
16+
int in_degree[MAXA];
17+
char out[MAXA + 1];
18+
19+
int main() {
20+
scanf("%d", &n);
21+
for (int i = 0; i < n; ++i) {
22+
scanf("%s", strings[i]);
23+
}
24+
25+
bool impossible = false;
26+
memset(in_degree, 0, sizeof(in_degree));
27+
for (int i = 1; i < n; ++i) {
28+
// find the first character that differs between s[i - 1] and s[i]
29+
int a = strlen(strings[i - 1]);
30+
int b = strlen(strings[i]);
31+
int j = 0;
32+
while (j < a and j < b and strings[i - 1][j] == strings[i][j]) {
33+
++j;
34+
}
35+
36+
// abcd
37+
// ab
38+
39+
// no alphabet can fix this
40+
41+
if (j >= b and j < a) {
42+
impossible = true;
43+
} else if (j < a and j < b) {
44+
// create an edge
45+
char before = strings[i - 1][j];
46+
char after = strings[i][j];
47+
48+
// printf("Edge between %c and %c\n", before, after);
49+
graph[before - 'a'].push_back(after - 'a');
50+
++in_degree[after - 'a'];
51+
}
52+
}
53+
54+
for (int i = 0; i < MAXA; ++i) {
55+
// find a node with 0 in degree
56+
bool found = false;
57+
for (int j = 0; j < MAXA; ++j) {
58+
if (in_degree[j] == 0) {
59+
in_degree[j] = -1;
60+
out[i] = j + 'a';
61+
found = true;
62+
63+
// printf("%d %c\n", i, out[i]);
64+
for (int k : graph[j]) {
65+
--in_degree[k];
66+
}
67+
68+
break;
69+
}
70+
}
71+
72+
if (!found) {
73+
impossible = true;
74+
break;
75+
}
76+
}
77+
78+
if (impossible) {
79+
puts("Impossible");
80+
} else {
81+
printf("%s\n", out);
82+
}
83+
84+
return 0;
85+
}

0 commit comments

Comments
 (0)