Skip to content

Commit f3d5ef1

Browse files
committed
AtCoder & CodeForces tasks from contests
1 parent 9eff9bd commit f3d5ef1

20 files changed

+872
-1
lines changed

Diff for: .vscode/settings.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"type_traits": "cpp",
1515
"xtree": "cpp",
1616
"xutility": "cpp",
17-
"istream": "cpp"
17+
"istream": "cpp",
18+
"ios": "cpp",
19+
"optional": "cpp",
20+
"system_error": "cpp",
21+
"xlocnum": "cpp",
22+
"xtr1common": "cpp",
23+
"map": "cpp"
1824
}
1925
}

Diff for: AtCoder/A_-_Apple.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
int main() {
8+
int x, y;
9+
cin >> x >> y;
10+
int n;
11+
cin >> n;
12+
13+
if (x * 3 < y) {
14+
cout << n * x << endl;
15+
} else {
16+
cout << (n / 3) * y + (n % 3) * x << endl;
17+
}
18+
19+
cout << flush;
20+
return 0;
21+
}

Diff for: AtCoder/A_-_Middle_Letter.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
int main() {
8+
string s;
9+
cin >> s;
10+
cout << s[(s.size() / 2)] << endl;
11+
cout << flush;
12+
return 0;
13+
}

Diff for: AtCoder/B_-_Explore.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
int main() {
8+
ll n, m, t;
9+
cin >> n >> m >> t;
10+
vector<int> a(n-1);
11+
for (int i = 0; i < n-1; i++) {
12+
cin >> a[i];
13+
}
14+
vector<int> inc(n-1);
15+
for (int i = 0; i < m; i++) {
16+
int x, y;
17+
cin >> x >> y;
18+
x--;
19+
inc[x] += y;
20+
}
21+
bool ok = true;
22+
for (int i = 0; i < n-1; i++) {
23+
if (t - a[i] + inc[i] <= 0) {
24+
ok = false;
25+
break;
26+
}
27+
28+
t = t - a[i] + inc[i];
29+
}
30+
cout << (ok ? "Yes" : "No") << endl;
31+
cout << flush;
32+
return 0;
33+
}

Diff for: AtCoder/B_-_Modulo_Number.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
int main() {
8+
ll N;
9+
cin >> N;
10+
const ll MOD = 998244353;
11+
cout << ((N % MOD) + MOD) % MOD << endl;
12+
cout << flush;
13+
return 0;
14+
}

Diff for: AtCoder/C_-_Belt_Conveyor.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
int main() {
8+
int n, m;
9+
cin >> n >> m;
10+
vector<string> mat(n);
11+
for (int i = 0; i < n; i++) {
12+
cin >> mat[i];
13+
}
14+
vector<vector<bool>> visited(n, vector<bool>(m, false));
15+
int i = 0, j = 0;
16+
while (true) {
17+
if (visited[i][j]) {
18+
cout << -1 << endl;
19+
return 0;
20+
}
21+
22+
visited[i][j] = true;
23+
int ai = i, aj = j;
24+
if (mat[i][j] == 'R') {
25+
j++;
26+
} else if (mat[i][j] == 'L') {
27+
j--;
28+
} else if (mat[i][j] == 'U') {
29+
i--;
30+
} else if (mat[i][j] == 'D') {
31+
i++;
32+
}
33+
34+
if (i >= 0 && j >= 0 && i < n && j < m) {
35+
continue;
36+
} else {
37+
cout << ai+1 << " " << aj+1 << endl;
38+
break;
39+
}
40+
}
41+
cout << flush;
42+
return 0;
43+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
#define ll long long
8+
// [start,end)
9+
ll binary_search(function<bool(ll)> func, ll start, ll end) {
10+
/* func:int ->bool
11+
returns smallest int x where func(x) evaluates to true, searches in [start,end), it is assumed the values are false, .. , false, true ...
12+
*/
13+
14+
if (end <= start) return end; // has to be here, otherwise func(end-1) in next line could be a problem
15+
if (!func(end-1)) return end;
16+
while (end - start > 1) {
17+
ll mid = (start + end) / 2;
18+
if (func(mid)) end = mid; else start = mid;
19+
}
20+
21+
return (func(start) ? start : end);
22+
};
23+
24+
int main() {
25+
int n;
26+
ll P, Q, R;
27+
cin >> n >> P >> Q >> R;
28+
29+
ll sum = P + Q + R;
30+
vector<ll> a(n), pref(n);
31+
for (int i = 0; i < n; i++) {
32+
cin >> a[i];
33+
}
34+
35+
pref[0] = a[0];
36+
for (int i = 1; i < n; i++) {
37+
pref[i] = pref[i-1] + a[i];
38+
}
39+
40+
auto binary = [&](int x, int R, ll sum) {
41+
int initR = R;
42+
ll cur_pref = (x-1>=0 ? pref[x-1] : 0);
43+
int id = binary_search([&](ll id) {
44+
return (pref[id] - cur_pref >= sum);
45+
}, x, R);
46+
if (pref[id] - cur_pref == sum && id + 1 < initR) {
47+
return id+1;
48+
}
49+
return -1;
50+
};
51+
52+
bool ok = false;
53+
for (int x = 0; x < n-3; x++) {
54+
int w = binary(x, n, sum);
55+
if (w == -1) continue;
56+
int y = binary(x, w, P);
57+
if (y == -1) continue;
58+
int z = binary(y, w, Q);
59+
if (z == -1) continue;
60+
int ww = binary(z, w+1, R);
61+
if (ww == -1) continue;
62+
if (w == ww && x < y && y < z && z < w) {
63+
ok = true;
64+
// cout << x << " " << y << " " << z << " " << w << endl;
65+
break;
66+
}
67+
}
68+
69+
cout << (ok ? "Yes" : "No") << endl;
70+
cout << flush;
71+
return 0;
72+
}

Diff for: CodeForces/A_Burenka_Plays_with_Fractions.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
template<typename T>
8+
T gcd(T a, T b) {
9+
return (a == 0) ? b : gcd(b % a, a);
10+
}
11+
12+
void testCase() {
13+
ll a, b, c, d;
14+
cin >> a >> b >> c >> d;
15+
if (a * d == b * c) {
16+
cout << 0 << endl;
17+
} else {
18+
if (a % gcd(b, d) != 0 && c % gcd(b, d) != 0) {
19+
cout << 2 << endl;
20+
} else {
21+
cout << 1 << endl;
22+
}
23+
}
24+
}
25+
26+
int main() {
27+
int t;
28+
cin >> t;
29+
30+
while (t--) {
31+
testCase();
32+
}
33+
34+
cout << flush;
35+
return 0;
36+
}

Diff for: CodeForces/A_Chip_Game.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int n, m;
9+
cin >> n >> m;
10+
n--;
11+
m--;
12+
if ((((n % 2 == 1) && (m % 2 == 0)) || ((n % 2 == 0) && (m % 2 == 1))) && !((n % 2 == 1) && (m % 2 == 1))) {
13+
cout << "Burenka" << endl;
14+
} else {
15+
cout << "Tonya" << endl;
16+
}
17+
}
18+
19+
int main() {
20+
int t;
21+
cin >> t;
22+
23+
while (t--) {
24+
testCase();
25+
}
26+
27+
cout << flush;
28+
return 0;
29+
}

Diff for: CodeForces/A_Crossmarket.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int n, m;
9+
cin >> n >> m;
10+
11+
if (n == 1 && m == 1) {
12+
cout << 0 << endl;
13+
return;
14+
}
15+
16+
if (n > m) {
17+
cout << m + (m - 1) + (n - 1) << endl;
18+
} else {
19+
cout << n + (n - 1) + (m - 1) << endl;
20+
}
21+
}
22+
23+
int main() {
24+
int t;
25+
cin >> t;
26+
27+
while (t--) {
28+
testCase();
29+
}
30+
31+
cout << flush;
32+
return 0;
33+
}

Diff for: CodeForces/A_Wonderful_Permutation.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int n, k;
9+
cin >> n >> k;
10+
vector<int> a(n);
11+
for (int i = 0; i < n; i++) cin >> a[i];
12+
int count = 0;
13+
for (int i = 0; i < k; i++) {
14+
if (a[i] > k) {
15+
count++;
16+
}
17+
}
18+
cout << count << endl;
19+
}
20+
21+
int main() {
22+
int t;
23+
cin >> t;
24+
25+
while (t--) {
26+
testCase();
27+
}
28+
29+
cout << flush;
30+
return 0;
31+
}

0 commit comments

Comments
 (0)