Skip to content

Commit dcae158

Browse files
committed
AtCoder practice solutions.
1 parent 98df1ca commit dcae158

7 files changed

+360
-1
lines changed

Diff for: AtCoder/A_Penalty_Kick.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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;
9+
cin >> N;
10+
for (int i = 0; i < N; i++) {
11+
cout << ((i + 1) % 3 == 0 ? 'x' : 'o');
12+
}
13+
cout << endl;
14+
return 0;
15+
}

Diff for: AtCoder/B_Farthest_Point.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
template<class T>
8+
struct Point {
9+
typedef Point P;
10+
T x, y;
11+
explicit Point(T x=0, T y=0) : x(x), y(y) {}
12+
bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }
13+
bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); }
14+
P operator+(P p) const { return P(x+p.x, y+p.y); }
15+
P operator-(P p) const { return P(x-p.x, y-p.y); }
16+
P operator*(T d) const { return P(x*d, y*d); }
17+
P operator/(T d) const { return P(x/d, y/d); }
18+
T dot(P p) const { return x*p.x + y*p.y; }
19+
T cross(P p) const { return x*p.y - y*p.x; }
20+
T cross(P a, P b) const { return (a-*this).cross(b-*this); }
21+
T dist2() const { return x*x + y*y; }
22+
double dist() const { return sqrt((double)dist2()); }
23+
// angle to x-axis in interval [-pi, pi]
24+
double angle() const { return atan2(y, x); }
25+
P unit() const { return *this/dist(); } // makes dist()=1
26+
P perp() const { return P(-y, x); } // rotates +90 degrees
27+
P normal() const { return perp().unit(); }
28+
// returns point rotated 'a' radians ccw around the origin
29+
P rotate(double a) const {
30+
return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }
31+
friend ostream& operator<<(ostream& os, P p) {
32+
return os << "(" << p.x << "," << p.y << ")"; }
33+
void GCD() {
34+
int GCD = gcd(x, y);
35+
x /= GCD; y /= GCD;
36+
}
37+
};
38+
39+
int main() {
40+
int N;
41+
cin >> N;
42+
vector<Point<int>> A(N);
43+
for (int i = 0; i < N; i++) {
44+
cin >> A[i].x >> A[i].y;
45+
}
46+
for (int i = 0; i < N; i++) {
47+
ll mx_dist = LLONG_MIN;
48+
int ans = -1;
49+
for (int j = 0; j < N; j++) {
50+
if (i == j) continue;
51+
if ((A[i]-A[j]).dist2() > mx_dist) {
52+
mx_dist = (A[i]-A[j]).dist2();
53+
ans = j;
54+
}
55+
}
56+
cout << ans + 1 << endl;
57+
}
58+
return 0;
59+
}

Diff for: AtCoder/C_Colorful_Beans.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+
int main() {
8+
map<int, int> mp;
9+
int N;
10+
cin >> N;
11+
for (int i = 0; i < N; i++) {
12+
int a, c;
13+
cin >> a >> c;
14+
auto it = mp.find(c);
15+
if (it == mp.end()) {
16+
mp[c] = a;
17+
} else {
18+
mp[c] = min(mp[c], a);
19+
}
20+
}
21+
int best = 0;
22+
for (auto [c, v]: mp) {
23+
if (v > best) {
24+
best = v;
25+
}
26+
}
27+
cout << best << endl;
28+
return 0;
29+
}

Diff for: AtCoder/F_Oddly_Similar.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma GCC optimize("O3")
2+
#pragma GCC optimize("unroll-loops")
3+
4+
#include <bits/stdc++.h>
5+
#define ll long long
6+
#define endl '\n'
7+
8+
using namespace std;
9+
10+
int A[2001][2001];
11+
12+
int main() {
13+
cin.tie(0)->sync_with_stdio(0);
14+
int N, M;
15+
cin >> N >> M;
16+
for (int i = 0; i < N; i++) {
17+
for (int j = 0; j < M; j++) {
18+
cin >> A[i][j];
19+
}
20+
}
21+
int ans = 0;
22+
for (int i = 0; i < N; i++) {
23+
for (int j = i + 1; j < N; j++) {
24+
int count = 0;
25+
for (int k = 0; k < M; k++) {
26+
if (A[i][k] == A[j][k]) count++;
27+
}
28+
ans += ((count & 1) > 0);
29+
}
30+
}
31+
cout << ans << endl;
32+
return 0;
33+
}

Diff for: BFS/AtCoder - D_Medicines_on_Grid.cpp

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
int H, W;
8+
vector<vector<int>> adj;
9+
vector<int> dfs_visited;
10+
vector<string> A;
11+
12+
int mp(int x, int y) {
13+
return (x * W) + y;
14+
}
15+
16+
const int dx[4] = { 0, 0, -1, 1 };
17+
const int dy[4] = { -1, 1, 0, 0 };
18+
19+
inline bool valid(int x, int y, int w, int h) {
20+
return (x >= 0 && x < w && y >= 0 && y < h);
21+
}
22+
23+
map<pair<int, int>, int> points;
24+
25+
void bfs(int x, int y, int e) {
26+
if (e == 0) return;
27+
struct BFS {
28+
int x, y, e;
29+
};
30+
queue<BFS> q;
31+
q.push({ x, y, e });
32+
int me = mp(x, y);
33+
vector<vector<bool>> visited(H, vector<bool>(W, false));
34+
visited[x][y] = true;
35+
while (!q.empty()) {
36+
auto top = q.front();
37+
q.pop();
38+
for (auto dir = 0; dir < 4; dir++) {
39+
int nx = top.x + dx[dir];
40+
int ny = top.y + dy[dir];
41+
if (valid(nx, ny, H, W) && !visited[nx][ny] && A[nx][ny] != '#') {
42+
visited[nx][ny] = true;
43+
if (points.count({ nx, ny })) {
44+
adj[me].push_back(mp(nx, ny));
45+
}
46+
if (top.e - 1 > 0) {
47+
q.push({ nx, ny, top.e - 1 });
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
void dfs(int u) {
55+
dfs_visited[u] = true;
56+
for (auto v: adj[u]) {
57+
if (dfs_visited[v]) continue;
58+
dfs(v);
59+
}
60+
}
61+
62+
int main() {
63+
cin >> H >> W;
64+
A.resize(H);
65+
for (int i = 0; i < H; i++) {
66+
cin >> A[i];
67+
}
68+
int N;
69+
cin >> N;
70+
adj.resize(H * W + 100);
71+
dfs_visited.resize(H * W + 100);
72+
for (int i = 0; i < N; i++) {
73+
int X, Y, E;
74+
cin >> X >> Y >> E;
75+
X--; Y--;
76+
points[{ X, Y }] = E;
77+
}
78+
for (int i = 0; i < H; i++) {
79+
for (int j = 0; j < W; j++) {
80+
if (A[i][j] == 'T') {
81+
points[{ i, j }] = 0;
82+
}
83+
}
84+
}
85+
for (auto [p, e]: points) {
86+
bfs(p.first, p.second, e);
87+
}
88+
for (int i = 0; i < H; i++) {
89+
for (int j = 0; j < W; j++) {
90+
if (A[i][j] == 'S') {
91+
dfs(mp(i, j));
92+
}
93+
}
94+
}
95+
for (int i = 0; i < H; i++) {
96+
for (int j = 0; j < W; j++) {
97+
if (A[i][j] == 'T') {
98+
cout << (dfs_visited[mp(i, j)] ? "Yes" : "No") << endl;
99+
return 0;
100+
}
101+
}
102+
}
103+
return 0;
104+
}
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
vector<vector<int>> adj;
8+
9+
struct CentroidDecomposition {
10+
vector<vector<int>> G;
11+
vector<ll> C;
12+
vector<int> sz, pa;
13+
vector<ll> ans;
14+
vector<bool> checked;
15+
16+
CentroidDecomposition(int N) {
17+
G.resize(N);
18+
sz.resize(N);
19+
pa.resize(N);
20+
ans.resize(N);
21+
checked.resize(N);
22+
C.resize(N);
23+
for (int i = 0; i < N; ++i) {
24+
G[i].clear();
25+
}
26+
}
27+
void addEdge(int u, int v) {
28+
G[u].push_back(v);
29+
G[v].push_back(u);
30+
}
31+
int dfs(int u, int p) {
32+
sz[u] = 1;
33+
for (auto v : G[u]) {
34+
if (v == p || checked[v]) continue;
35+
sz[u] += dfs(v, u);
36+
}
37+
return sz[u];
38+
}
39+
int centroid(int u, int p, int n) {
40+
for (auto v : G[u]) {
41+
if (v == p || checked[v]) continue;
42+
if (sz[v] > n / 2) return centroid(v, u, n);
43+
}
44+
return u;
45+
}
46+
pair<ll, ll> dfs2(int u, int p, int c, int d) {
47+
// dis[c][u] = d;
48+
pair<ll, ll> result = { 0, C[u] };
49+
for (auto v : G[u]) {
50+
if (v == p || checked[v]) continue;
51+
auto ch = dfs2(v, u, c, d + 1);
52+
result.first += ch.first + ch.second;
53+
result.second += ch.second;
54+
}
55+
return result;
56+
}
57+
void dfs3(int u, int p, int c, pair<ll, ll> push) {
58+
push.first += push.second;
59+
ans[u] += push.first;
60+
for (auto v : G[u]) {
61+
if (v == p || checked[v]) continue;
62+
dfs3(v, u, c, push);
63+
}
64+
}
65+
void build(int u, int p) {
66+
int n = dfs(u, p);
67+
int c = centroid(u, p, n);
68+
if (p == -1) p = c;
69+
pa[c] = p;
70+
71+
vector<pair<ll, ll>> results(G[c].size());
72+
pair<ll, ll> sum = { 0, 0 };
73+
for (int i = 0; i < G[c].size(); i++) {
74+
auto v = G[c][i];
75+
if (v == p || checked[v]) continue;
76+
results[i] = dfs2(v, c, c, 1);
77+
results[i].first += results[i].second;
78+
sum.first += results[i].first;
79+
sum.second += results[i].second;
80+
}
81+
82+
ans[c] += sum.first;
83+
84+
for (int i = 0; i < G[c].size(); i++) {
85+
auto v = G[c][i];
86+
if (v == p || checked[v]) continue;
87+
pair<ll, ll> push = sum;
88+
push.first -= results[i].first;
89+
push.second -= results[i].second;
90+
push.second += C[c];
91+
dfs3(v, c, c, push);
92+
}
93+
94+
checked[c] = true;
95+
for (auto v : G[c]) {
96+
if (v == p || checked[v]) continue;
97+
build(v, c);
98+
}
99+
}
100+
};
101+
102+
int main() {
103+
int N;
104+
cin >> N;
105+
CentroidDecomposition cd(N + 3);
106+
for (int i = 0; i < N - 1; ++i) {
107+
int a, b;
108+
cin >> a >> b;
109+
cd.addEdge(a, b);
110+
}
111+
for (int i = 0; i < N; i++) {
112+
cin >> cd.C[i + 1];
113+
}
114+
cd.build(1, 0);
115+
ll ans = LLONG_MAX;
116+
for (int i = 1; i <= N; i++) ans = min(ans, cd.ans[i]);
117+
cout << ans << endl;
118+
return 0;
119+
}

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Competitive Programming Problems and Solutions
22

3-
![](https://img.shields.io/static/v1?label=Solutions&message=1251&color=brightgreen)
3+
![](https://img.shields.io/static/v1?label=Solutions&message=1257&color=brightgreen)
44

55
This repository is meant as a public place to store my competitive programming solutions to various of competitive programming problems on sites such as:
66
- [CodeForces](https://codeforces.com/)

0 commit comments

Comments
 (0)