Skip to content

Commit a553b68

Browse files
committedApr 21, 2023
Solved some more problems from an atcoder round I last worked on 2022-03-05
1 parent 7c981a0 commit a553b68

File tree

8 files changed

+2690
-323
lines changed

8 files changed

+2690
-323
lines changed
 

‎atcoder/2022-03-05_beginner_242/F.cpp

+917-323
Large diffs are not rendered by default.

‎atcoder/2022-03-05_beginner_242/F.in

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
40 40 30 30
2+
3+
2 2 1 1
4+
5+
1 2 1 1
6+
7+
8+
19
3 3 1 1
210

311
2 2 1 1

‎atcoder/2022-03-05_beginner_242/F.thinking

+20
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,24 @@ F(R, c, b) * G(R-c', c+c', b') gets added onto F(R,
1414
F(r, c, b) * G(
1515

1616

17+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
18+
19+
How many ways to use K rooks on a RxC grid?
20+
Well...
21+
22+
How many ways to use K rooks and RxC grid while needing every row and col?
23+
24+
H(R, C, K) = bin(R*C, K)
25+
26+
27+
28+
F(R, C, K) = H(R,C,K) - sum of F(r,c,K) * binom(R,r) * binom(C,c)
29+
With K fixed, this should be computable in O(R*C).
30+
31+
32+
Then final answer is sum of F(rb, cb, B) * F(rw, cw, W) * binom(R,rb) * binom(R-rb,rc) * binom(C,cb) * binom(C-cb,cw).
33+
(rb+rw <= R, cb+cw <= C)
34+
35+
This part is O(N^4).
36+
1737
.

‎atcoder/2022-03-05_beginner_242/F_old.cpp

+551
Large diffs are not rendered by default.

‎atcoder/2022-03-05_beginner_242/G.cpp

+1,029
Large diffs are not rendered by default.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
I could try segtree with position = person and value = final answer + (state)
3+
I'd need a lot of state though. Each segment needs a list of unpaired people?
4+
5+
6+
7+
I could try adding people to the list one at a time.
8+
Segtree value = answer from here to the end.
9+
Add kth person of a certain color --> I need k/2 range updates.
10+
11+
12+
13+
.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /usr/bin/bash
2+
set -e # Halt on error throughout this script (e.g. if diff finds a difference)
3+
4+
if [ $# -ne 1 ]
5+
then
6+
echo "Need exactly 1 argument: (problem name)."
7+
exit
8+
fi
9+
10+
PROBLEM="invalid"
11+
PROBLEM=$1
12+
13+
# commands needed to prepare the solutions.
14+
# just comment out if one/both uses Python or otherwise doesn't need prep.
15+
g++ --std=c++20 "$PROBLEM".cpp -g -O2 -o a.out \
16+
-DDCCLYDE_LOCAL
17+
g++ --std=c++20 -DDCCLYDE_BRUTEFORCE "$PROBLEM".cpp -g -O2 -o b.out \
18+
-DDCCLYDE_LOCAL
19+
20+
for((k = 1; k < 100000 ; ++k)); do
21+
echo $k
22+
pypy3 gen.py > gen.in
23+
a.out < gen.in > fast.out \
24+
# 2> /dev/null
25+
b.out < gen.in > brute.out \
26+
# 2> /dev/null
27+
28+
diff -w fast.out brute.out
29+
done
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#region load template's helpers
2+
import sys
3+
sys.path.append("/home/dcclyde/puzzles/code/templates")
4+
from template import *
5+
#endregion
6+
#region ri, rv
7+
def ri(q, b=None):
8+
if isinstance(b, int):
9+
q = (q, b)
10+
elif isinstance(q, int):
11+
q = (q,q)
12+
return random.randint(q[0], q[1])
13+
14+
def rv(q, n):
15+
N = ri(n)
16+
return [ri(q) for _ in range(N)]
17+
#endregion
18+
#region rgraph
19+
def rgraph(nr, er, mute=False):
20+
N = ri(nr)
21+
E = ri(er)
22+
E = min(E, N*(N-1)//2)
23+
edge_options = [(a, b) for a in range(N) for b in range(a+1, N)]
24+
edges = random.sample(edge_options, E)
25+
26+
if not mute:
27+
print(N, E)
28+
pvn1(edges)
29+
return N, E, edges
30+
#endregion
31+
#region rtree
32+
#region DSU
33+
class DSU:
34+
def __init__(self, _N):
35+
self.N = _N
36+
self.e = [-1] * self.N
37+
def get(self, x):
38+
if self.e[x] < 0:
39+
return x
40+
self.e[x] = self.e[self.e[x]]
41+
return self.e[x]
42+
def sameSet(self, a,b): return self.get(a) == self.get(b)
43+
def size(self, x): return -self.e[self.get(x)]
44+
def unite(self, x, y):
45+
x = self.get(x); y = self.get(y)
46+
if (x == y): return False
47+
if (self.e[x] > self.e[y]): x,y = y,x
48+
self.e[x] += self.e[y]
49+
self.e[y] = x
50+
return True
51+
#endregion
52+
def rtree(nr, mute=False):
53+
N = ri(nr)
54+
edges = []
55+
for a in range(1, N):
56+
b = ri((0, a-1))
57+
edges.append([a,b])
58+
random.shuffle(edges[-1])
59+
60+
perm = list(range(N))
61+
random.shuffle(perm)
62+
edges = [[perm[x] for x in q] for q in edges]
63+
if not mute:
64+
print(N)
65+
pvn1(edges)
66+
return N, edges
67+
#endregion
68+
#region rtree_rooted
69+
def rtree_rooted(nr, mute=False):
70+
N, edges = rtree(nr, mute=True)
71+
G = [[] for _ in range(N)]
72+
for a, b in edges:
73+
G[a].append(b)
74+
G[b].append(a)
75+
root = 0
76+
parents = [-2] * N
77+
parents[root] = -1
78+
# traverse.
79+
todo = [root]
80+
while len(todo) > 0:
81+
curr = todo.pop()
82+
for o in G[curr]:
83+
if parents[o] == -2:
84+
parents[o] = curr
85+
todo.append(o)
86+
87+
parents = parents[1:]
88+
if not mute:
89+
print(N)
90+
pv1(parents)
91+
return N, parents
92+
93+
def rtree_rooted_v2(nr, mute=False):
94+
# this version assumes v's parent will have an index < v.
95+
N = ri(nr)
96+
parents = [ri((0, k)) for k in range(N-1)]
97+
if not mute:
98+
print(N)
99+
pv1(parents)
100+
return N, parents
101+
#endregion
102+
103+
104+
RC_RANGE = (1, 5)
105+
WB_RANGE = (1, 3)
106+
107+
def gen_test():
108+
R, C = rv(RC_RANGE, 2)
109+
W, B = rv(WB_RANGE, 2)
110+
ps(R, C, B, W)
111+
112+
113+
# ! IF RANDOM TESTS PASS:
114+
# ! * OVERFLOW! Remember pii, FOR, maxi/mini, INF Write an "overflow gen"?
115+
# ! * RECODE FROM SCRATCH? Maybe in Python?
116+
# ! * Misunderstanding the problem rules?
117+
# ! * Brute force wrong - is it sufficiently different?
118+
# ! * Edge case not buildable in brute forceable inputs (or needs mid size AND pathological)
119+
#region past mistakes
120+
# * Uninitialized variable
121+
# * I used INF = 1e9 but answer could be bigger
122+
#endregion
123+
gen_test()

0 commit comments

Comments
 (0)
Please sign in to comment.