Skip to content

Commit d43201f

Browse files
committed
AGC problems
1 parent 914ab0b commit d43201f

14 files changed

+341
-0
lines changed

Atcoder/AGC 001-A.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int l[100];
5+
6+
int main() {
7+
int n;
8+
cin >> n;
9+
for (int i = 0; i < 2 * n; i++) cin >> l[i];
10+
sort(l, l + 2 * n);
11+
int ans = 0;
12+
for (int i = 0; i < 2 * n; i += 2) ans += l[i];
13+
cout << ans;
14+
return 0;
15+
}

Atcoder/AGC 001-B.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
ll lozenge(ll n, ll m) {
6+
if (!n) return -m;
7+
return m / n * 2 * n + lozenge(m % n, n);
8+
}
9+
10+
int main() {
11+
cin.tie(0)->sync_with_stdio(0);
12+
ll n, x;
13+
cin >> n >> x;
14+
if (x * 2 > n) x = n - x;
15+
cout << n + lozenge(x, n - x);
16+
return 0;
17+
}

Atcoder/AGC 002-A.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
int main() {
6+
cin.tie(0)->sync_with_stdio(0);
7+
int a, b;
8+
cin >> a >> b;
9+
if (a <= 0 && b >= 0) return cout << "Zero", 0;
10+
cout << ((min(b, 0) - min(a, -1)) % 2 ? "Positive" : "Negative");
11+
return 0;
12+
}

Atcoder/AGC 002-B.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
bool has_red[100001];
6+
int cnt[100001];
7+
8+
int main() {
9+
cin.tie(0)->sync_with_stdio(0);
10+
int n, m;
11+
cin >> n >> m;
12+
fill(cnt + 1, cnt + n + 1, 1);
13+
has_red[1] = true;
14+
while (m--) {
15+
int x, y;
16+
cin >> x >> y;
17+
if (has_red[x]) has_red[y] = true;
18+
cnt[x]--, cnt[y]++;
19+
if (!cnt[x]) has_red[x] = false;
20+
}
21+
cout << accumulate(has_red + 1, has_red + n + 1, 0);
22+
return 0;
23+
}

Atcoder/AGC 003-A.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
int main() {
6+
cin.tie(0)->sync_with_stdio(0);
7+
string s;
8+
cin >> s;
9+
bool nn = false, ee = false, ww = false, ss = false;
10+
for (char c : s) {
11+
if (c == 'N') nn = true;
12+
if (c == 'E') ee = true;
13+
if (c == 'W') ww = true;
14+
if (c == 'S') ss = true;
15+
}
16+
cout << ((nn ^ ss) || (ee ^ ww) ? "No" : "Yes");
17+
return 0;
18+
}

Atcoder/AGC 003-B.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
ll a[100001];
6+
7+
int main() {
8+
cin.tie(0)->sync_with_stdio(0);
9+
int n;
10+
cin >> n;
11+
for (int i = 0; i < n; i++) cin >> a[i];
12+
ll ans = 0;
13+
for (int i = 0; i < n; i++) {
14+
ans += a[i] / 2;
15+
if ((a[i] & 1) && a[i + 1]) {
16+
a[i + 1]--;
17+
ans++;
18+
}
19+
}
20+
cout << ans;
21+
return 0;
22+
}

Atcoder/AGC 003-C.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
int x[100001];
6+
7+
int main() {
8+
cin.tie(0)->sync_with_stdio(0);
9+
int n;
10+
cin >> n;
11+
map<int, int> pos;
12+
for (int i = 0; i < n; i++) {
13+
cin >> x[i];
14+
pos[x[i]] = i;
15+
}
16+
sort(x, x + n);
17+
int ans = 0;
18+
for (int i = 0; i < n; i += 2) if (abs(i - pos[x[i]]) & 1) ans++;
19+
cout << ans;
20+
return 0;
21+
}

Atcoder/AGC 017-D.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
vector<int> graph[100001];
6+
int grundy[100001];
7+
8+
void dfs(int node = 1, int parent = 0) {
9+
for (int i : graph[node]) if (i != parent) {
10+
dfs(i, node);
11+
grundy[node] ^= grundy[i] + 1;
12+
}
13+
}
14+
15+
int main() {
16+
cin.tie(0)->sync_with_stdio(0);
17+
int n;
18+
cin >> n;
19+
for (int i = 1; i < n; i++) {
20+
int u, v;
21+
cin >> u >> v;
22+
graph[u].push_back(v);
23+
graph[v].push_back(u);
24+
}
25+
dfs();
26+
cout << (grundy[1] ? "Alice" : "Bob");
27+
return 0;
28+
}

Atcoder/AGC 025-B.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
const ll MOD = 998244353;
6+
7+
ll inv(ll base) {
8+
ll ans = 1, pow = MOD - 2;
9+
while (pow) {
10+
if (pow & 1) ans = ans * base % MOD;
11+
base = base * base % MOD;
12+
pow >>= 1;
13+
}
14+
return ans;
15+
}
16+
17+
ll fact[300001];
18+
19+
ll choose(ll n, ll m) {
20+
return fact[n] * inv(fact[m]) % MOD * inv(fact[n - m]) % MOD;
21+
}
22+
23+
int main() {
24+
cin.tie(0)->sync_with_stdio(0);
25+
int n;
26+
ll a, b, k;
27+
cin >> n >> a >> b >> k;
28+
29+
fact[0] = 1;
30+
for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % MOD;
31+
32+
ll ans = 0;
33+
for (ll a_cnt = 0; a_cnt <= n && a * a_cnt <= k; a_cnt++) {
34+
ll b_sum = k - a * a_cnt;
35+
ll b_cnt = b_sum / b;
36+
if (b_sum % b != 0 || b_cnt > n) continue;
37+
ans = (ans + choose(n, a_cnt) * choose(n, b_cnt)) % MOD;
38+
}
39+
cout << ans;
40+
return 0;
41+
}

Atcoder/AGC 035-B.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> graph[100001];
5+
int visited[100001], odd[100001], timer = 1;
6+
vector<pair<int, int>> ans;
7+
8+
void dfs(int node, int parent = 0) {
9+
visited[node] = timer++;
10+
for (int i : graph[node]) if (i != parent) {
11+
if (!visited[i]) {
12+
dfs(i, node);
13+
if (odd[i]) {
14+
ans.push_back({i, node});
15+
odd[i] = 0;
16+
} else {
17+
ans.push_back({node, i});
18+
odd[node] ^= 1;
19+
}
20+
} else if (visited[node] > visited[i]) {
21+
ans.push_back({node, i});
22+
odd[node] ^= 1;
23+
}
24+
}
25+
}
26+
27+
int main() {
28+
cin.tie(0)->sync_with_stdio(0);
29+
int n, m;
30+
scanf("%d %d", &n, &m);
31+
while (m--) {
32+
int u, v;
33+
scanf("%d %d", &u, &v);
34+
graph[u].push_back(v);
35+
graph[v].push_back(u);
36+
}
37+
for (int i = 1; i <= n; i++) if (!visited[i]) dfs(i);
38+
if (accumulate(odd + 1, odd + n + 1, 0)) printf("-1");
39+
else for (pair<int, int> i : ans) printf("%d %d\n", i.first, i.second);
40+
return 0;
41+
}

0 commit comments

Comments
 (0)