Skip to content

Commit 88ed473

Browse files
committed
feat: add codejam 2021 quali
1 parent b6e006c commit 88ed473

File tree

5 files changed

+850
-0
lines changed

5 files changed

+850
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#include <algorithm>
2+
#include <bitset>
3+
#include <cassert>
4+
#include <chrono>
5+
#include <cmath>
6+
#include <complex>
7+
#include <cstdio>
8+
#include <cstring>
9+
#include <ctime>
10+
#include <functional>
11+
#include <iomanip>
12+
#include <iostream>
13+
#include <iterator>
14+
#include <limits>
15+
#include <map>
16+
#include <numeric>
17+
#include <queue>
18+
#include <random>
19+
#include <set>
20+
#include <stack>
21+
#include <string>
22+
#include <tuple>
23+
#include <utility>
24+
#include <vector>
25+
26+
#define int long long
27+
#define double long double
28+
#define ff first
29+
#define ss second
30+
#define endl '\n'
31+
#define ii pair<int, int>
32+
#define mp make_pair
33+
#define mt make_tuple
34+
#define DESYNC \
35+
ios_base::sync_with_stdio(false); \
36+
cin.tie(0); \
37+
cout.tie(0)
38+
#define pb push_back
39+
#define vi vector<int>
40+
#define vii vector<ii>
41+
#define all(x) x.begin(), x.end()
42+
#define EPS 1e-9
43+
#define INF 1e18
44+
#define ROOT 1
45+
#define curtime chrono::steady_clock::now().time_since_epoch().count
46+
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
47+
using ll = long long;
48+
using namespace std;
49+
const double PI = acos(-1);
50+
const int M = 1000000007;
51+
// const int M = 998244353;
52+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
53+
inline int mod(int n, int m = M) {
54+
int ret = n % m;
55+
if (ret < 0) ret += m;
56+
return ret;
57+
}
58+
59+
int exp(int n, int k, int m = M) {
60+
if (k == 0) return 1;
61+
if (k == 1) return n;
62+
int ax = exp(n, k / 2, m);
63+
ax = mod(ax * ax, m);
64+
if (k % 2) ax = mod(ax * n, m);
65+
return ax;
66+
}
67+
68+
int gcd(int a, int b) {
69+
if (a == 0)
70+
return b;
71+
else
72+
return gcd(b % a, a);
73+
}
74+
75+
/* LIBRARY INCLUDES */
76+
77+
/* END OF LIBRARY INCLUDES */
78+
79+
/* TEMPLATE DEFINES */
80+
// #define FILE_INPUT
81+
#define PRINT_TEST_CASE
82+
#define MULTIPLE_TEST_CASE
83+
84+
/* SOLUTION CODE */
85+
86+
string s[101];
87+
88+
void solution(int testcase) {
89+
#ifdef PRINT_TEST_CASE
90+
cout << "Case #" << testcase << ": ";
91+
#endif
92+
// Code starts here
93+
int n = 100;
94+
int m = 10000;
95+
vi cnt(m);
96+
97+
vi points(n);
98+
99+
for (int i = 0; i < n; i++) {
100+
cin >> s[i];
101+
for (int j = 0; j < m; j++) {
102+
if (s[i][j] == '1') cnt[j]++, points[i]++;
103+
}
104+
}
105+
106+
vi qs(m);
107+
iota(all(qs), 0);
108+
109+
vi us(n);
110+
iota(all(us), 0);
111+
112+
sort(all(qs), [&](const int a, const int b) {
113+
return cnt[a] > cnt[b] || cnt[a] == cnt[b] && a < b;
114+
});
115+
116+
sort(all(us), [&](const int a, const int b) {
117+
return points[a] < points[b] || points[a] == points[b] && a < b;
118+
});
119+
120+
vi coeff(n, 0);
121+
122+
vector<pair<double, int>> suspicious(n, ii(0, 0));
123+
for (int i = 0; i < n; i++) {
124+
int lst = -1;
125+
suspicious[us[i]] = ii(0, us[i]);
126+
int cur = 0;
127+
for (int j = 0; j < m; j++) {
128+
if (s[us[i]][qs[j]] == '1') {
129+
if (j > 0 && s[us[i]][qs[j - 1]] == '0') coeff[us[i]]++;
130+
cur++;
131+
}
132+
if (j != 0 && j % 500 == 0) {
133+
if (lst != -1) suspicious[us[i]].ff += (lst - cur);
134+
lst = cur;
135+
cur = 0;
136+
}
137+
}
138+
}
139+
140+
vii votes(n);
141+
for (int i = 0; i < n; i++) {
142+
votes[i] = ii(0, i);
143+
}
144+
145+
for (int i = 0; i < n; i++) {
146+
vector<pair<double, int>> ord;
147+
for (int j = i; j < i + 10 && j < n; j++) {
148+
ord.pb(suspicious[us[j]]);
149+
}
150+
sort(all(ord));
151+
votes[ord[0].ss].ff++;
152+
// cout << "voting for " << ord[0].ss + 1 << endl;
153+
}
154+
155+
sort(all(votes), [&](const ii x, const ii y) {
156+
return x.ff * coeff[x.ss] * points[x.ss] >
157+
y.ff * coeff[y.ss] * points[y.ss];
158+
// return x.ff > y.ff || (x.ff == y.ff && coeff[x.ss] > coeff[y.ss]) ||
159+
// (x.ff == y.ff && coeff[x.ss] == coeff[y.ss] &&
160+
// points[x.ss] > points[y.ss]);
161+
});
162+
// for (int i = 0; i < n; i++) {
163+
// cout << votes[i].ss + 1 << " = " << votes[i].ff << " | "
164+
// << points[votes[i].ss] << " | " << coeff[votes[i].ss] << endl;
165+
// }
166+
cout << votes.front().ss + 1 << endl;
167+
}
168+
169+
#ifdef FILE_INPUT
170+
freopen("equal.in", "r", stdin);
171+
freopen("equal.out", "w", stdout);
172+
#endif
173+
174+
int32_t main() {
175+
DESYNC;
176+
int t = 1;
177+
#ifdef MULTIPLE_TEST_CASE
178+
cin >> t;
179+
#endif
180+
int p;
181+
cin >> p;
182+
for (int _testcase = 1; _testcase <= t; _testcase++) solution(_testcase);
183+
}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#include <algorithm>
2+
#include <bitset>
3+
#include <cassert>
4+
#include <chrono>
5+
#include <cmath>
6+
#include <complex>
7+
#include <cstdio>
8+
#include <cstring>
9+
#include <ctime>
10+
#include <functional>
11+
#include <iomanip>
12+
#include <iostream>
13+
#include <iterator>
14+
#include <limits>
15+
#include <map>
16+
#include <numeric>
17+
#include <queue>
18+
#include <random>
19+
#include <set>
20+
#include <stack>
21+
#include <string>
22+
#include <tuple>
23+
#include <utility>
24+
#include <vector>
25+
26+
#define int long long
27+
#define double long double
28+
#define ff first
29+
#define ss second
30+
#define endl '\n'
31+
#define ii pair<int, int>
32+
#define mp make_pair
33+
#define mt make_tuple
34+
#define DESYNC \
35+
ios_base::sync_with_stdio(false); \
36+
cin.tie(0); \
37+
cout.tie(0)
38+
#define pb push_back
39+
#define vi vector<int>
40+
#define vii vector<ii>
41+
#define all(x) x.begin(), x.end()
42+
#define EPS 1e-9
43+
#define INF 1e18
44+
#define ROOT 1
45+
#define curtime chrono::steady_clock::now().time_since_epoch().count
46+
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
47+
using ll = long long;
48+
using namespace std;
49+
const double PI = acos(-1);
50+
const int M = 1000000007;
51+
// const int M = 998244353;
52+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
53+
inline int mod(int n, int m = M) {
54+
int ret = n % m;
55+
if (ret < 0) ret += m;
56+
return ret;
57+
}
58+
59+
int exp(int n, int k, int m = M) {
60+
if (k == 0) return 1;
61+
if (k == 1) return n;
62+
int ax = exp(n, k / 2, m);
63+
ax = mod(ax * ax, m);
64+
if (k % 2) ax = mod(ax * n, m);
65+
return ax;
66+
}
67+
68+
int gcd(int a, int b) {
69+
if (a == 0)
70+
return b;
71+
else
72+
return gcd(b % a, a);
73+
}
74+
75+
/* LIBRARY INCLUDES */
76+
77+
/* END OF LIBRARY INCLUDES */
78+
79+
/* TEMPLATE DEFINES */
80+
// #define FILE_INPUT
81+
// #define PRINT_TEST_CASE
82+
// #define MULTIPLE_TEST_CASE
83+
84+
/* SOLUTION CODE */
85+
86+
int submit(vi& v) {
87+
for (int i = 0; i < v.size(); i++) {
88+
cout << (i != 0 ? " " : "") << v[i];
89+
}
90+
cout << endl << flush;
91+
92+
int verdict;
93+
cin >> verdict;
94+
return verdict;
95+
}
96+
97+
map<vector<int>, int> cache;
98+
int asks = 0;
99+
100+
int ask(int a, int b, int c) {
101+
vi v;
102+
asks++;
103+
v.pb(a);
104+
v.pb(b);
105+
v.pb(c);
106+
sort(all(v));
107+
if (cache.count(v)) return cache[v];
108+
cout << a << " " << b << " " << c;
109+
cout << endl << flush;
110+
111+
int verdict;
112+
cin >> verdict;
113+
cache[v] = verdict;
114+
return verdict;
115+
}
116+
117+
vi solve(vi v) {
118+
if (v.size() <= 2) return v;
119+
vi lef, rig, mid;
120+
121+
// shufle every level
122+
// shuffle(all(v), rng);
123+
int n = v.size();
124+
// rig.pb(v[n - 1]);
125+
for (int i = 1; i < n - 1; i++) {
126+
int ans = ask(v[0], v[i], v[n - 1]);
127+
assert(ans != -1);
128+
129+
if (ans == v[i]) {
130+
mid.pb(v[i]);
131+
} else if (ans == v[0]) {
132+
lef.pb(v[i]);
133+
} else
134+
rig.pb(v[i]);
135+
}
136+
137+
// lef.pb(v[0]);
138+
139+
lef = solve(lef);
140+
mid = solve(mid);
141+
rig = solve(rig);
142+
143+
if (lef.size() >= 2) {
144+
int ans = ask(lef[0], lef[1], v[0]);
145+
if (ans == lef[0]) reverse(all(lef));
146+
}
147+
148+
lef.pb(v[0]);
149+
150+
if (rig.size() >= 2) {
151+
int ans = ask(v[n - 1], rig[0], rig[1]);
152+
if (ans == rig[1]) reverse(all(rig));
153+
}
154+
155+
reverse(all(rig));
156+
rig.pb(v[n - 1]);
157+
reverse(all(rig));
158+
159+
// fix mid using lef
160+
if (mid.size() >= 2 && lef.size() > 0) {
161+
int ans = ask(lef[0], mid[0], mid[1]);
162+
if (ans == mid[1]) reverse(all(mid));
163+
}
164+
165+
// fix rig using lef
166+
// if (rig.size() >= 2) {
167+
// int ans = ask(lef[0], rig[0], rig[1]);
168+
// if (ans == rig[1]) reverse(all(rig));
169+
// }
170+
171+
v.clear();
172+
for (int x : lef) v.pb(x);
173+
for (int x : mid) v.pb(x);
174+
for (int x : rig) v.pb(x);
175+
176+
return v;
177+
}
178+
179+
void solution(int testcase) {
180+
#ifdef PRINT_TEST_CASE
181+
cout << "Case #" << testcase << ": ";
182+
#endif
183+
// Code starts here
184+
int t, n, q;
185+
cin >> t >> n >> q;
186+
asks = 0;
187+
int tt = t;
188+
while (t--) {
189+
// start
190+
cache.clear();
191+
vi v(n);
192+
iota(all(v), 1);
193+
v = solve(v);
194+
assert(submit(v) == 1);
195+
}
196+
assert(asks <= q);
197+
}
198+
199+
#ifdef FILE_INPUT
200+
freopen("equal.in", "r", stdin);
201+
freopen("equal.out", "w", stdout);
202+
#endif
203+
204+
int32_t main() {
205+
DESYNC;
206+
int t = 1;
207+
#ifdef MULTIPLE_TEST_CASE
208+
cin >> t;
209+
#endif
210+
for (int _testcase = 1; _testcase <= t; _testcase++) solution(_testcase);
211+
}

0 commit comments

Comments
 (0)