Skip to content

Commit b819ffa

Browse files
committed
Solve problems 863B, 863D, 863E, 864A, 864B and 864C from codeforces, 1045, 1134 and 1322 from Timus and 49. Odd Sum from csacademy
1 parent 67d6a51 commit b819ffa

13 files changed

+495
-0
lines changed

CS Academy/49. Odd Sum.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long ll;
6+
7+
int const N = 1e5 + 1, oo = 2e9;
8+
int n, m, q, k, x, y, a[N];
9+
10+
int main() {
11+
scanf("%d", &n);
12+
for(int i = 0; i < n; ++i)
13+
scanf("%d", a + i);
14+
15+
int res = 0;
16+
for(int i = 0; i < n; ++i)
17+
for(int j = i + 1; j < n; ++j)
18+
if((a[i] + a[j]) & 1)
19+
++res;
20+
printf("%d\n", res);
21+
22+
return 0;
23+
}
24+

CS Academy/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
- [44. Frequent Numbers](https://csacademy.com/contest/round-44/task/frequent-numbers)
1111
- [44. Square Cover](https://csacademy.com/contest/round-44/task/square-cover)
1212
- [44. Check DFS](https://csacademy.com/contest/round-44/task/check-dfs)
13+
- [49. Odd Sum](https://csacademy.com/contest/round-49/task/odd-sum)

CodeForces/863B. Kayaking.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 101;
6+
int n, a[N];
7+
vector<int> all;
8+
9+
int main() {
10+
scanf("%d", &n);
11+
n *= 2;
12+
for(int i = 0; i < n; ++i)
13+
scanf("%d", a + i);
14+
sort(a, a + n);
15+
16+
int sol = 1e9;
17+
for(int i = 0; i < n; ++i)
18+
for(int j = i + 1; j < n; ++j) {
19+
all.clear();
20+
for(int k = 0; k < n; ++k)
21+
if(k != i && k != j)
22+
all.push_back(a[k]);
23+
24+
int res = 0;
25+
for(int k = 0; k < (int)all.size(); k += 2)
26+
res += (all[k + 1] - all[k]);
27+
28+
sol = min(sol, res);
29+
}
30+
31+
printf("%d\n", sol);
32+
33+
return 0;
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
struct query {
6+
int t, l, r;
7+
};
8+
9+
int const N = 2e5 + 1;
10+
int n, q, m, a[N];
11+
query all[N];
12+
13+
int main() {
14+
scanf("%d %d %d", &n, &q, &m);
15+
for(int i = 0; i < n; ++i)
16+
scanf("%d", a + i);
17+
for(int i = 0; i < q; ++i)
18+
scanf("%d %d %d", &all[i].t, &all[i].l, &all[i].r);
19+
20+
for(int i = 0, cur; i < m; ++i) {
21+
scanf("%d", &cur);
22+
23+
for(int j = q - 1; j >= 0; --j) {
24+
if(cur >= all[j].l && cur <= all[j].r) {
25+
if(all[j].t == 1) {
26+
--cur;
27+
if(cur < all[j].l)
28+
cur = all[j].r;
29+
} else {
30+
int len = all[j].r - all[j].l + 1;
31+
cur -= all[j].l;
32+
cur = len - cur - 1;
33+
cur += all[j].l;
34+
}
35+
}
36+
}
37+
38+
printf("%d ", a[--cur]);
39+
}
40+
41+
return 0;
42+
}

CodeForces/863E. Turn Off The TV.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <bits/stdc++.h>
2+
3+
#define F first
4+
#define S second
5+
6+
using namespace std;
7+
8+
int const N = 2e6 + 1;
9+
int n, rng[N], dp[N][25];
10+
pair<int, int> a[N];
11+
vector<int> all;
12+
13+
int main() {
14+
scanf("%d", &n);
15+
for(int i = 0; i < n; ++i) {
16+
scanf("%d %d", &a[i].F, &a[i].S);
17+
all.push_back(a[i].F);
18+
all.push_back(a[i].F + 1);
19+
all.push_back(a[i].F - 1);
20+
all.push_back(a[i].S);
21+
all.push_back(a[i].S + 1);
22+
all.push_back(a[i].S - 1);
23+
}
24+
sort(all.begin(), all.end());
25+
all.resize(unique(all.begin(), all.end()) - all.begin());
26+
27+
for(int i = 0; i < n; ++i) {
28+
a[i].F = lower_bound(all.begin(), all.end(), a[i].F) - all.begin();
29+
a[i].S = lower_bound(all.begin(), all.end(), a[i].S) - all.begin();
30+
++rng[a[i].F];
31+
--rng[a[i].S + 1];
32+
}
33+
34+
for(int i = 1; i < all.size() + 10; ++i)
35+
rng[i] += rng[i - 1];
36+
37+
for(int i = 0; i < all.size() + 10; ++i)
38+
dp[i][0] = rng[i];
39+
40+
for(int j = 1; (1 << j) <= all.size() + 10; ++j)
41+
for(int i = 0; i + (1 << j) - 1 < all.size() + 10; ++i)
42+
dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
43+
44+
for(int i = 0; i < n; ++i) {
45+
int len = a[i].S - a[i].F + 1;
46+
int log = 0;
47+
while((1 << log) <= len)
48+
++log;
49+
--log;
50+
51+
int mn = min(dp[a[i].F][log], dp[a[i].F + len - (1 << log)][log]);
52+
if(mn > 1) {
53+
printf("%d\n", i + 1);
54+
return 0;
55+
}
56+
}
57+
58+
puts("-1");
59+
60+
return 0;
61+
}

CodeForces/864A. Fair Game.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long ll;
6+
7+
int const N = 1e5 + 1;
8+
int n, m, q, p, k, x, y, a[N];
9+
10+
int main() {
11+
scanf("%d", &n);
12+
for(int i = 0, tmp; i < n; ++i) {
13+
scanf("%d", &tmp);
14+
++a[tmp];
15+
}
16+
17+
int cnt = 0;
18+
for(int i = 0; i < 101; ++i) {
19+
if(a[i] != 0)
20+
++cnt;
21+
}
22+
23+
if(cnt == 2) {
24+
for(int i = 0; i < 101; ++i) {
25+
if(a[i] != 0) {
26+
if(x == 0)
27+
x = a[i], q = i;
28+
else
29+
y = a[i], p = i;
30+
}
31+
}
32+
33+
if(x == y) {
34+
puts("YES");
35+
printf("%d %d\n", q, p);
36+
} else {
37+
puts("NO");
38+
}
39+
} else {
40+
puts("NO");
41+
}
42+
43+
return 0;
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long ll;
6+
7+
int const N = 1e5 + 1;
8+
int n, m, q, p, k, x, y, a[N];
9+
string s, tmp = "";
10+
vector<string> all;
11+
12+
int dist(string t) {
13+
int fr[26] = { 0 };
14+
for(int i = 0; i < (int)t.length(); ++i)
15+
++fr[t[i] - 'a'];
16+
int ret = 0;
17+
for(int i = 0; i < 26; ++i)
18+
if(fr[i] != 0)
19+
++ret;
20+
return ret;
21+
}
22+
23+
int main() {
24+
cin >> n >> s;
25+
for(int i = 0; i < n; ++i) {
26+
if(islower(s[i]))
27+
tmp += s[i];
28+
else {
29+
if(!tmp.empty())
30+
all.push_back(tmp);
31+
tmp = "";
32+
}
33+
}
34+
if(!tmp.empty())
35+
all.push_back(tmp);
36+
37+
int res = 0;
38+
for(int i = 0; i < (int)all.size(); ++i)
39+
res = max(res, dist(all[i]));
40+
cout << res << endl;
41+
42+
return 0;
43+
}

CodeForces/864C. Bus.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long ll;
6+
7+
int const N = 1e5 + 1;
8+
int a, b, f, k;
9+
10+
int main() {
11+
scanf("%d %d %d %d", &a, &b, &f, &k);
12+
int tmpb = b;
13+
14+
int res = 0;
15+
for(int i = 0; i < k; ++i) {
16+
if(b < 0) {
17+
puts("-1");
18+
return 0;
19+
}
20+
21+
if(i % 2 == 0) {
22+
b -= f;
23+
if(b < 0) {
24+
puts("-1");
25+
return 0;
26+
}
27+
28+
if(i == k - 1) {
29+
if((a - f) > b) {
30+
b = tmpb;
31+
++res;
32+
}
33+
} else {
34+
if((a - f) * 2 > b) {
35+
b = tmpb;
36+
++res;
37+
}
38+
}
39+
40+
b -= (a - f);
41+
} else {
42+
b -= (a - f);
43+
if(b < 0) {
44+
puts("-1");
45+
return 0;
46+
}
47+
48+
if(i == k - 1) {
49+
if(f > b) {
50+
b = tmpb;
51+
++res;
52+
}
53+
} else {
54+
if(f * 2 > b) {
55+
b = tmpb;
56+
++res;
57+
}
58+
}
59+
60+
b -= f;
61+
}
62+
}
63+
64+
if(b < 0) {
65+
puts("-1");
66+
return 0;
67+
}
68+
69+
printf("%d\n", res);
70+
71+
return 0;
72+
}

CodeForces/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,10 @@
278278
- [862A. Mahmoud and Ehab and the MEX](http://codeforces.com/contest/862/problem/A)
279279
- [862B. Mahmoud and Ehab and the bipartiteness](http://codeforces.com/contest/862/problem/B)
280280
- [863A. Quasi-palindrome](http://codeforces.com/contest/863/problem/A)
281+
- [863B. Kayaking](http://codeforces.com/contest/863/problem/B)
281282
- [863C. 1-2-3](http://codeforces.com/contest/863/problem/C)
283+
- [863D. Yet Another Array Queries Problem](http://codeforces.com/contest/863/problem/D)
284+
- [863E. Turn Off The TV](http://codeforces.com/contest/863/problem/E)
285+
- [864A. Fair Game](http://codeforces.com/contest/864/problem/A)
286+
- [864B. Polycarp and Letters](http://codeforces.com/contest/864/problem/B)
287+
- [864C. Bus](http://codeforces.com/contest/864/problem/C)

Timus/1045. Funny Game.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <bits/stdc++.h>
2+
3+
typedef long long ll;
4+
5+
using namespace std;
6+
7+
int const N = 1e4 + 1;
8+
int n, k;
9+
vector<int> adj[N];
10+
bool vis[N];
11+
12+
int dfs(int u, int tr) {
13+
vis[u] = true;
14+
15+
for(int i = 0; i < adj[u].size(); ++i){
16+
int v = adj[u][i];
17+
if(!vis[v] && v != k){
18+
if(dfs(v, !tr))
19+
if(tr)
20+
return 1;
21+
else if(!tr)
22+
return 0;
23+
}
24+
}
25+
if(!tr)
26+
return 1;
27+
return 0;
28+
}
29+
int main() {
30+
cin >> n >> k;
31+
for(int i = 0;i < n-1; ++i){
32+
int a, b;
33+
cin >> a >> b;
34+
adj[a].push_back(b);
35+
adj[b].push_back(a);
36+
}
37+
38+
if(!dfs(k, true))
39+
cout << "First player loses\n";
40+
else {
41+
int yes = 10001;
42+
for(int i = 0; i < adj[k].size(); i++){
43+
memset(vis, 0, sizeof vis);
44+
int to = adj[k][i];
45+
if(dfs(to, 0))
46+
yes = min(yes, to);
47+
}
48+
cout << "First player wins flying to airport " << yes << endl;
49+
}
50+
51+
return 0;
52+
}

0 commit comments

Comments
 (0)