Skip to content

Commit 7cadd59

Browse files
committed
CF and AtCoder solutions.
1 parent 13018d2 commit 7cadd59

10 files changed

+854
-0
lines changed

Diff for: AtCoder/D_-_Destroyer_Takahashi.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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, d;
9+
cin >> n >> d;
10+
vector<pair<int, int>> events(n);
11+
for (int i = 0; i < n; i++) {
12+
cin >> events[i].first >> events[i].second;
13+
}
14+
15+
sort(events.begin(), events.end(), [&](const pair<int, int> &a, const pair<int, int> &b) {
16+
return (a.second < b.second);
17+
});
18+
19+
int x = INT_MIN;
20+
int ans = 0;
21+
for (int i = 0; i < n; i++) {
22+
if (x + d - 1 < events[i].first) {
23+
ans++;
24+
x = events[i].second;
25+
}
26+
}
27+
28+
cout << ans << endl;
29+
return 0;
30+
}

Diff for: AtCoder/D_-_Neighbors.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
11+
vector<set<int>> adj(n);
12+
for (int i = 0; i < m; i++) {
13+
int u, v;
14+
cin >> u >> v;
15+
u--; v--;
16+
adj[u].insert(v);
17+
adj[v].insert(u);
18+
}
19+
20+
bool ok = true;
21+
for (int i = 0; i < n; i++) {
22+
if (adj[i].size() > 2) {
23+
ok = false;
24+
break;
25+
}
26+
}
27+
28+
vector<bool> visited(n, false);
29+
30+
for (int i = 0; i < n && ok; i++) {
31+
if (visited[i]) continue;
32+
visited[i] = true;
33+
if (adj[i].empty()) continue;
34+
35+
int L = *adj[i].begin();
36+
visited[L] = true;
37+
adj[i].erase(L);
38+
int prL = i;
39+
while (true) {
40+
adj[L].erase(prL);
41+
prL = L;
42+
if (adj[L].empty()) break;
43+
L = *adj[L].begin();
44+
if (visited[L]) {
45+
ok = false;
46+
break;
47+
}
48+
visited[L] = true;
49+
}
50+
51+
if (adj[i].empty()) continue;
52+
int R = *adj[i].begin();
53+
adj[i].erase(R);
54+
visited[R] = true;
55+
int prR = i;
56+
while (true) {
57+
adj[R].erase(prR);
58+
prR = R;
59+
if (adj[R].empty()) break;
60+
R = *adj[R].begin();
61+
if (visited[R]) {
62+
ok = false;
63+
break;
64+
}
65+
visited[R] = true;
66+
}
67+
}
68+
69+
cout << (ok ? "Yes" : "No") << endl;
70+
return 0;
71+
}

Diff for: AtCoder/D_-_Range_Count_Query.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+
int main() {
8+
int n;
9+
cin >> n;
10+
11+
vector<vector<int>> adj(n + 10);
12+
for (int i = 0; i < n; i++) {
13+
int x;
14+
cin >> x;
15+
adj[x].push_back(i);
16+
}
17+
18+
for (int i = 0; i <= n; i++) {
19+
sort(adj[i].begin(), adj[i].end());
20+
}
21+
22+
int q;
23+
cin >> q;
24+
for (int i = 0; i < q; i++) {
25+
int l, r, x;
26+
cin >> l >> r >> x;
27+
l--; r--;
28+
29+
int L = lower_bound(adj[x].begin(), adj[x].end(), l) - adj[x].begin();
30+
int R = upper_bound(adj[x].begin(), adj[x].end(), r) - adj[x].begin();
31+
cout << R - L << endl;
32+
}
33+
34+
cout << flush;
35+
return 0;
36+
}

Diff for: AtCoder/D_-_Trophy.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
ll x;
10+
cin >> n >> x;
11+
vector<pair<int, int>> v(n);
12+
for (int i = 0; i < n; i++) {
13+
cin >> v[i].first >> v[i].second;
14+
}
15+
ll cost_to = 0;
16+
ll mn = INT_MAX;
17+
ll best = (ll)v[0].first + (x * (ll)v[0].second);
18+
for (int i = 0; i < n; i++) {
19+
best = min(best, cost_to + mn * (x - i));
20+
cost_to = cost_to + v[i].first + v[i].second;
21+
mn = min(mn, (ll)v[i].second);
22+
}
23+
best = min(best, cost_to + mn * (x - n));
24+
cout << best << endl;
25+
cout << flush;
26+
return 0;
27+
}

Diff for: BFS/AtCoder - D_-_Jumping_Takahashi_2.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
struct point {
8+
ll x, y, p;
9+
10+
void read() {
11+
cin >> x >> y >> p;
12+
}
13+
14+
ll man(const point& other) {
15+
return abs(x - other.x) + abs(y - other.y);
16+
}
17+
};
18+
19+
int main() {
20+
int n;
21+
cin >> n;
22+
vector<point> v(n);
23+
for (int i = 0; i < n; i++) {
24+
v[i].read();
25+
}
26+
27+
ll L = 0, R = 4e9 + 1000;
28+
while (R - L > 1) {
29+
ll mid = (L + R) / 2;
30+
31+
bool ok = false;
32+
for (int i = 0; i < n && !ok; i++) {
33+
queue<int> q;
34+
q.push(i);
35+
vector<bool> visited(n, false);
36+
visited[i] = true;
37+
while (!q.empty()) {
38+
int p = q.front();
39+
q.pop();
40+
for (int k = 0; k < n; k++) {
41+
if (k == p || visited[k]) continue;
42+
if (v[p].p * mid >= v[p].man(v[k])) {
43+
visited[k] = true;
44+
q.push(k);
45+
}
46+
}
47+
}
48+
49+
bool ac = true;
50+
for (int i = 0; i < n; i++) if (!visited[i]) ac = false;
51+
ok |= ac;
52+
}
53+
54+
if (ok) R = mid;
55+
else L = mid+1;
56+
}
57+
58+
for (int off = -5; off <= 5; off++) {
59+
ll mid = L + off;
60+
if (mid < 0) continue;
61+
bool ok = false;
62+
for (int i = 0; i < n && !ok; i++) {
63+
queue<int> q;
64+
q.push(i);
65+
vector<bool> visited(n, false);
66+
visited[i] = true;
67+
while (!q.empty()) {
68+
int p = q.front();
69+
q.pop();
70+
for (int k = 0; k < n; k++) {
71+
if (k == p || visited[k]) continue;
72+
if (v[p].p * mid >= v[p].man(v[k])) {
73+
visited[k] = true;
74+
q.push(k);
75+
}
76+
}
77+
}
78+
79+
bool ac = true;
80+
for (int i = 0; i < n; i++) if (!visited[i]) ac = false;
81+
ok |= ac;
82+
}
83+
if (ok) {
84+
cout << mid << endl;
85+
return 0;
86+
}
87+
}
88+
89+
cout << flush;
90+
return 0;
91+
}

Diff for: CodeForces/C_Make_it_Increasing.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+
int main() {
8+
int n;
9+
cin >> n;
10+
11+
ll ans = LLONG_MAX;
12+
vector<ll> v(n);
13+
for (int i = 0; i < n; i++) cin >> v[i];
14+
15+
for (int i = 0; i < n; i++) {
16+
ll cur = 0;
17+
ll right = 0;
18+
for (int j = i - 1; j >= 0; j--) {
19+
ll times = (right / v[j]) + 1;
20+
cur += times;
21+
right = times * v[j];
22+
}
23+
24+
right = 0;
25+
for (int j = i + 1; j < n; j++) {
26+
ll times = (right / v[j]) + 1;
27+
cur += times;
28+
right = times * v[j];
29+
}
30+
31+
ans = min(ans, cur);
32+
}
33+
34+
cout << ans << endl;
35+
return 0;
36+
}

Diff for: CodeForces/C_Mark_and_His_Unfinished_Essay.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
struct sub {
8+
ll L, R;
9+
ll jump;
10+
};
11+
12+
void testCase() {
13+
int n, copy, q;
14+
cin >> n >> copy >> q;
15+
string s;
16+
cin >> s;
17+
18+
vector<sub> v;
19+
v.push_back({ 0, n-1, 0 });
20+
21+
for (int i = 0; i < copy; i++) {
22+
ll L, R;
23+
cin >> L >> R;
24+
L--; R--;
25+
26+
v.push_back({
27+
v.back().R + 1,
28+
v.back().R + 1 + (R - L),
29+
L
30+
});
31+
}
32+
33+
for (int i = 0; i < q; i++) {
34+
ll K;
35+
cin >> K;
36+
K--;
37+
for (const auto& SUB: v) {
38+
if (K >= SUB.L && K <= SUB.R) {
39+
K = SUB.jump + K - SUB.L;
40+
while (K >= n) {
41+
for (int i = 0; i < v.size(); i++) {
42+
const auto& SUB = v[i];
43+
if (K >= SUB.L && K <= SUB.R) {
44+
K = SUB.jump + K - SUB.L;
45+
}
46+
}
47+
}
48+
cout << s[K] << endl;
49+
break;
50+
}
51+
}
52+
}
53+
}
54+
55+
int main() {
56+
int t;
57+
cin >> t;
58+
59+
while (t--) {
60+
testCase();
61+
}
62+
63+
cout << flush;
64+
return 0;
65+
}

0 commit comments

Comments
 (0)