Skip to content

Commit fbf10f4

Browse files
committed
Big problem dump
1 parent 17a6b0c commit fbf10f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4940
-0
lines changed

Atcoder/AGC 033-C.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <utility>
3+
#include <vector>
4+
using namespace std;
5+
6+
vector<int> graph[200001];
7+
8+
pair<int, int> dfs(int node = 1, int parent = 0) {
9+
pair<int, int> ret = {node, 1};
10+
for (int i : graph[node])
11+
if (i != parent) {
12+
pair<int, int> cand = dfs(i, node);
13+
if (cand.second >= ret.second) ret = {cand.first, cand.second + 1};
14+
}
15+
return ret;
16+
}
17+
18+
int main() {
19+
cin.tie(0)->sync_with_stdio(0);
20+
int n;
21+
cin >> n;
22+
for (int i = 1; i < n; i++) {
23+
int u, v;
24+
cin >> u >> v;
25+
graph[u].push_back(v);
26+
graph[v].push_back(u);
27+
}
28+
if (dfs(dfs().first).second % 3 == 2)
29+
cout << "Second";
30+
else
31+
cout << "First";
32+
return 0;
33+
}

Atcoder/AGC 053-B.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <queue>
3+
using namespace std;
4+
typedef long long ll;
5+
6+
ll a[400001];
7+
8+
int main() {
9+
cin.tie(0)->sync_with_stdio(0);
10+
int n;
11+
cin >> n;
12+
ll ans = 0;
13+
for (int i = 1; i <= 2 * n; i++) {
14+
cin >> a[i];
15+
ans += a[i];
16+
}
17+
priority_queue<ll> pq;
18+
for (int i = 1; i <= n; i++) {
19+
pq.push(-a[n - i + 1]);
20+
pq.push(-a[n + i]);
21+
ans += pq.top();
22+
pq.pop();
23+
}
24+
cout << ans;
25+
}

SACO/SACO 22-nails.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <utility>
4+
#include <vector>
5+
using namespace std;
6+
7+
pair<int, int> banners[300001];
8+
9+
int main() {
10+
cin.tie(0)->sync_with_stdio(0);
11+
int n;
12+
cin >> n;
13+
for (int i = 0; i < n; i++) cin >> banners[i].second >> banners[i].first;
14+
sort(banners, banners + n);
15+
vector<int> ans;
16+
int last_covered = 0;
17+
for (int i = 0; i < n; i++) {
18+
if (banners[i].second > last_covered) {
19+
last_covered = banners[i].first;
20+
ans.push_back(last_covered);
21+
}
22+
}
23+
cout << ans.size() << '\n';
24+
for (int i : ans) cout << i << ' ';
25+
return 0;
26+
}

SACO/SACO 22-police.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int neighbours[1000000];
5+
6+
int main() {
7+
cin.tie(0)->sync_with_stdio(0);
8+
int n, ans = 0;
9+
cin >> n;
10+
for (int i = 1; i < n; i++) {
11+
int u, v;
12+
cin >> u >> v;
13+
neighbours[u]++;
14+
neighbours[v]++;
15+
}
16+
for (int i = 0; i < n; i++) {
17+
if (neighbours[i] > 1) ans++;
18+
}
19+
cout << ans;
20+
return 0;
21+
}

SACO/SACO 22-stars.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
using namespace std;
4+
5+
struct TrieNode {
6+
TrieNode *children[2];
7+
int subtree_size = 0;
8+
9+
TrieNode() {
10+
children[0] = children[1] = nullptr;
11+
}
12+
};
13+
14+
int n, r, x[100001];
15+
16+
void insert(TrieNode *root, int val) {
17+
TrieNode *node = root;
18+
for (int i = 30; ~i; i--) {
19+
int bit = (val >> i) & 1;
20+
if (!node->children[bit])
21+
node->children[bit] = new TrieNode();
22+
node = node->children[bit];
23+
node->subtree_size++;
24+
}
25+
}
26+
27+
int num_covered(TrieNode *root, int pos) {
28+
TrieNode *node = root;
29+
int ans = 0;
30+
for (int i = 30; ~i && node; i--) {
31+
int bit = (pos >> i) & 1;
32+
if ((r >> i) & 1) {
33+
if (node->children[bit]) ans += node->children[bit]->subtree_size;
34+
node = node->children[!bit];
35+
} else node = node->children[bit];
36+
}
37+
if (node) ans++;
38+
return ans;
39+
}
40+
41+
int main() {
42+
cin.tie(0)->sync_with_stdio(0);
43+
cin >> n >> r;
44+
TrieNode *root = new TrieNode();
45+
for (int i = 0; i < n; i++) {
46+
cin >> x[i];
47+
insert(root, x[i]);
48+
}
49+
int best_coverage = 0, best_pos = -1;
50+
sort(x, x + n);
51+
for (int i = 0; i < n; i++) {
52+
int coverage = num_covered(root, x[i]);
53+
if (coverage > best_coverage) {
54+
best_coverage = coverage;
55+
best_pos = x[i];
56+
}
57+
}
58+
cout << best_pos;
59+
return 0;
60+
}

SACO/brackets-checker.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "testlib.h"
2+
using namespace std;
3+
4+
int n;
5+
long long dp[200][200][400];
6+
7+
readAndCheckAnswer(InStream& in) {
8+
int h = in.readInt(1, 200, "h");
9+
int w = in.readInt(1, 200, "w");
10+
11+
for (int i = 0; i < h; i++)
12+
for (int j = 0; j < w; j++)
13+
for (int k = 0; k < 400; k++)
14+
dp[i][j][k] = 0;
15+
dp[0][0][1] = 1;
16+
17+
in.readEoln();
18+
for (int i = 0; i < h; i++) {
19+
string s = in.readToken("[+-]{" + to_string(w) + "}", "s");
20+
for (int j = 0; j < w; j++) {
21+
if (s[j] == '+') {
22+
if (i)
23+
for (int k = 1; k < 400; k++)
24+
dp[i][j][k] += dp[i - 1][j][k - 1];
25+
if (j)
26+
for (int k = 1; k < 400; k++)
27+
dp[i][j][k] += dp[i][j - 1][k - 1];
28+
} else {
29+
if (i)
30+
for (int k = 0; k < 399; k++)
31+
dp[i][j][k] += dp[i - 1][j][k + 1];
32+
if (j)
33+
for (int k = 0; k < 399; k++)
34+
dp[i][j][k] += dp[i][j - 1][k + 1];
35+
}
36+
}
37+
}
38+
39+
if (dp[h - 1][w - 1][0] != n)
40+
in.quitf(_wa, "Wrong number of paths");
41+
}
42+
43+
int main(int argc, char* argv[]) {
44+
registerTestlibCmd(argc, argv);
45+
n = inf.readInt();
46+
readAndCheckAnswer(ans);
47+
readAndCheckAnswer(ouf);
48+
quitf(_ok, "n=%d", n);
49+
}

SACO/brackets-gen.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <bits/stdc++.h>
2+
#include "testlib.h"
3+
using namespace std;
4+
5+
int main(int argc, char* argv[]) {
6+
registerGen(argc, argv, 1);
7+
int max_n = atoi(argv[1]), st = atoi(argv[2]);
8+
if (st == 2) cout << max_n << '\n';
9+
else if (st == 4) cout << (1 << max_n) << '\n';
10+
else cout << rnd.next(1, max_n) << '\n';
11+
return 0;
12+
}

SACO/brackets-validator.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "testlib.h"
2+
using namespace std;
3+
4+
int n;
5+
long long dp[200][200][400];
6+
7+
readAndCheckAnswer(InStream& in) {
8+
int h = in.readInt(1, 200);
9+
int w = in.readInt(1, 200);
10+
dp[0][0][1] = 1;
11+
for (int i = 0; i < h; i++) {
12+
for (int j = 0; j < w; j++) {
13+
char c = in.readChar();
14+
if (c == '+') {
15+
if (i)
16+
for (int k = 1; k < 400; k++)
17+
dp[i][j][k] += dp[i - 1][j][k - 1];
18+
if (j)
19+
for (int k = 1; k < 400; k++)
20+
dp[i][j][k] += dp[i][j - 1][k - 1];
21+
} else {
22+
if (i)
23+
for (int k = 0; k < 399; k++)
24+
dp[i][j][k] += dp[i - 1][j][k + 1];
25+
if (j)
26+
for (int k = 0; k < 399; k++)
27+
dp[i][j][k] += dp[i][j - 1][k + 1];
28+
}
29+
}
30+
}
31+
32+
if (dp[h - 1][w - 1][0] != n)
33+
in.quitf(_wa, "Wrong number of paths: expected %d but got %d, h=%d w=%d", n, dp[h - 1][w - 1][0], h, w);
34+
}
35+
36+
int main(int argc, char* argv[]) {
37+
registerTestlibCmd(argc, argv);
38+
n = inf.readInt();
39+
readAndCheckAnswer(ouf);
40+
quitf(_ok, "n=%d", n);
41+
}

SACO/brackets.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
bool open[200][200];
5+
6+
int main() {
7+
int k;
8+
cin >> k;
9+
10+
if (k == 4) {
11+
cout << "3 4\n+---\n+++-\n+---\n";
12+
return 0;
13+
}
14+
15+
for (int i = 0, x = 0, y = 0; i < 30; i++, x += 2, y += 6, k >>= 1) {
16+
open[x][y] = open[x + 1][y + 1] = true;
17+
open[x + 1][y + 3] = open[x + 2][y + 3] = true;
18+
if (k & 1)
19+
for (int j = x + 2; j < 200; j += 2) open[j][y] = true;
20+
}
21+
for (int i = 0; i < 200; i += 2) open[64][i] = true;
22+
23+
cout << "65 200\n";
24+
for (int i = 0; i < 65; i++) {
25+
for (int j = 0; j < 200; j++) {
26+
if (open[i][j])
27+
cout << '+';
28+
else
29+
cout << '-';
30+
}
31+
cout << '\n';
32+
}
33+
}

SACO/gemstones-gen.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
#include "testlib.h"
3+
using namespace std;
4+
5+
vector<int> tree[200001], perimeter[200001];
6+
array<int, 3> coords[200001];
7+
int counter = 3, assigned[200001];
8+
9+
void dfs1(int node = 1, int x = 1, int y = 2, int z = 3) {
10+
coords[node] = {x, y, z};
11+
if (node == 1) {
12+
if (tree[node].size() == 1) {
13+
dfs1(tree[node][0], y, z, ++counter);
14+
perimeter[x].push_back(y);
15+
perimeter[y].push_back(x);
16+
perimeter[x].push_back(z);
17+
perimeter[z].push_back(x);
18+
} else {
19+
dfs1(tree[node][0], x, y, ++counter);
20+
dfs1(tree[node][1], x, z, ++counter);
21+
perimeter[y].push_back(z);
22+
perimeter[z].push_back(y);
23+
}
24+
} else {
25+
if (tree[node].size() == 0) {
26+
perimeter[x].push_back(z);
27+
perimeter[z].push_back(x);
28+
perimeter[y].push_back(z);
29+
perimeter[z].push_back(y);
30+
} else if (tree[node].size() == 1) {
31+
dfs1(tree[node][0], x, z, ++counter);
32+
perimeter[y].push_back(z);
33+
perimeter[z].push_back(y);
34+
} else {
35+
dfs1(tree[node][0], x, z, ++counter);
36+
dfs1(tree[node][1], y, z, ++counter);
37+
}
38+
}
39+
}
40+
41+
void dfs2(int node = 1, int curr = 1) {
42+
assigned[node] = curr;
43+
for (int i : perimeter[node]) if (!assigned[i])
44+
dfs2(i, curr + 1);
45+
}
46+
47+
int main(int argc, char* argv[]) {
48+
registerGen(argc, argv, 1);
49+
int n = atoi(argv[1]), mx_c = atoi(argv[2]), st = atoi(argv[3]);
50+
for (int i = 2; i <= n - 2; i++) {
51+
int p = rnd.next(1, i - 1);
52+
while (tree[p].size() == 2) p = rnd.next(1, i - 1);
53+
tree[p].push_back(i);
54+
}
55+
dfs1();
56+
dfs2();
57+
cout << n << '\n';
58+
for (int i = 1; i <= n - 2; i++) {
59+
cout << assigned[coords[i][0]] << ' '
60+
<< assigned[coords[i][1]] << ' '
61+
<< assigned[coords[i][2]] << ' ';
62+
cout << rnd.next(1, mx_c) << '\n';
63+
}
64+
return 0;
65+
}

0 commit comments

Comments
 (0)