Skip to content

Commit a5037ae

Browse files
committed
Solve problems 981[A, B, C] from codeforces
1 parent d39fe78 commit a5037ae

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Idea:
3+
- Brute force, try every possible substring.
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
int main() {
11+
string s;
12+
cin >> s;
13+
14+
int mx = 0;
15+
for(int i = 0; i < s.length(); ++i) {
16+
for(int j = 1; j <= s.length(); ++j) {
17+
string tmp = s.substr(i, j), rev = tmp;
18+
reverse(rev.begin(), rev.end());
19+
if(tmp != rev)
20+
mx = max(mx, int(tmp.length()));
21+
}
22+
}
23+
24+
cout << mx << endl;
25+
26+
return 0;
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Idea:
3+
- Greedy, for each value in each company choose the largest one.
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
int const N = 1e5 + 1;
11+
int n, m;
12+
map<int, int> a, b;
13+
vector<int> all;
14+
15+
int main() {
16+
scanf("%d", &n);
17+
for(int i = 0, x, y; i < n; ++i) {
18+
scanf("%d %d", &x, &y);
19+
a[x] = y;
20+
all.push_back(x);
21+
}
22+
23+
scanf("%d", &m);
24+
for(int i = 0, x, y; i < m; ++i) {
25+
scanf("%d %d", &x, &y);
26+
b[x] = y;
27+
all.push_back(x);
28+
}
29+
30+
sort(all.begin(), all.end());
31+
all.resize(unique(all.begin(), all.end()) - all.begin());
32+
33+
long long res = 0;
34+
for(int i = 0, mx; i < all.size(); ++i) {
35+
mx = 0;
36+
if(a.count(all[i]))
37+
mx = max(mx, a[all[i]]);
38+
if(b.count(all[i]))
39+
mx = max(mx, b[all[i]]);
40+
res += mx;
41+
}
42+
43+
printf("%lld\n", res);
44+
45+
return 0;
46+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Idea:
3+
- Greedy, there is a solution if and only if the tree is a single path
4+
or there is one node has more than two edges go from it to another nodes.
5+
*/
6+
7+
#include <bits/stdc++.h>
8+
9+
using namespace std;
10+
11+
int const N = 1e5 + 1;
12+
int n;
13+
bool vis[N];
14+
vector<vector<int> > g;
15+
vector<int> sol;
16+
17+
void DFS(int u) {
18+
vis[u] = true;
19+
20+
bool ok = false;
21+
for(int i = 0, v; i < g[u].size(); ++i) {
22+
v = g[u][i];
23+
if(!vis[v]) {
24+
ok = true;
25+
DFS(v);
26+
}
27+
}
28+
29+
if(!ok)
30+
sol.push_back(u + 1);
31+
}
32+
33+
int main() {
34+
scanf("%d", &n);
35+
g.resize(n);
36+
for(int i = 0, a, b; i < n - 1; ++i) {
37+
scanf("%d %d", &a, &b);
38+
--a, --b;
39+
g[a].push_back(b);
40+
swap(a, b);
41+
g[a].push_back(b);
42+
}
43+
44+
int gt2 = 0;
45+
for(int i = 0; i < n; ++i)
46+
if(g[i].size() > 2)
47+
++gt2;
48+
49+
if(gt2 > 1) {
50+
puts("No");
51+
return 0;
52+
}
53+
54+
puts("Yes");
55+
56+
if(gt2 == 0) {
57+
puts("1");
58+
for(int i = 0; i < g.size(); ++i)
59+
if(g[i].size() == 1)
60+
printf("%d ", i + 1);
61+
puts("");
62+
return 0;
63+
}
64+
65+
int mx = 0, idx;
66+
for(int i = 0; i < n; ++i) {
67+
if(g[i].size() > mx) {
68+
mx = g[i].size();
69+
idx = i;
70+
}
71+
}
72+
73+
printf("%d\n", mx);
74+
75+
DFS(idx);
76+
++idx;
77+
78+
for(int i = 0; i < sol.size(); ++i)
79+
printf("%d %d\n", idx, sol[i]);
80+
81+
return 0;
82+
}

CodeForces/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@
430430
- [979A. Pizza, Pizza, Pizza!!!](http://codeforces.com/contest/979/problem/A)
431431
- [979B. Treasure Hunt](http://codeforces.com/contest/979/problem/B)
432432
- [979C. Kuro and Walking Route](http://codeforces.com/contest/979/problem/C)
433+
- [981A. Antipalindrome](http://codeforces.com/contest/981/problem/A)
434+
- [981B. Businessmen Problems](http://codeforces.com/contest/981/problem/B)
435+
- [981C. Useful Decomposition](http://codeforces.com/contest/981/problem/C)
433436
- [982A. Row](http://codeforces.com/contest/982/problem/A)
434437
- [982B. Bus of Characters](http://codeforces.com/contest/982/problem/B)
435438
- [982C. Cut 'em all!](http://codeforces.com/contest/982/problem/C)

0 commit comments

Comments
 (0)