Skip to content

Commit 17ef12e

Browse files
committed
Solved alot of problems :3
1 parent a14451d commit 17ef12e

File tree

13 files changed

+571
-0
lines changed

13 files changed

+571
-0
lines changed

CodeForces Gyms/2013 USP Try-outs/B. Spy Duel.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
Idea:
3+
- Precalculate the probability of getting damage `d`
4+
from each attack, this can be done by calculating
5+
the probability using brute force.
6+
- Then using dynamic programming we can calculate the
7+
probability of `A` to win.
8+
- dp[rema][remb][p] represents:
9+
- rema: the remaining health of `A`
10+
- remb: the remaining health of `B`
11+
- p: the current player
12+
- In each turn in the dp we try all attacks using
13+
all possible damages.
14+
*/
15+
116
#include <bits/stdc++.h>
217

318
using namespace std;
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
Idea:
3+
- If there is less than two `I` points the answer is
4+
`No`.
5+
- Otherwise, for each two points check if the answer
6+
can be created from them, if the answer does not exist
7+
using only two points, add third point two the two points
8+
and check if the answer exist.
9+
- The center of the circle created from two points
10+
is the mid point between them.
11+
- The center of the circle created from three points
12+
is the circumcentre of them.
13+
*/
14+
15+
#include <bits/stdc++.h>
16+
17+
using namespace std;
18+
19+
struct point {
20+
double x, y;
21+
bool t;
22+
point() {}
23+
point(double x, double y, char c) : x(x), y(y) {
24+
if(c == 'I')
25+
t = 0;
26+
else
27+
t = 1;
28+
}
29+
};
30+
31+
int const N = 1e2 + 1;
32+
int n;
33+
point p[N];
34+
35+
double dist(point a, point b) {
36+
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
37+
}
38+
39+
void lineFromPoints(point P, point Q, double &a,
40+
double &b, double &c) {
41+
a = Q.y - P.y;
42+
b = P.x - Q.x;
43+
c = a*(P.x)+ b*(P.y);
44+
}
45+
46+
void perpendicularBisectorFromLine(point P, point Q,
47+
double &a, double &b, double &c) {
48+
point mid_point = point((P.x + Q.x)/2,
49+
(P.y + Q.y)/2, 'I');
50+
51+
c = -b*(mid_point.x) + a*(mid_point.y);
52+
53+
double temp = a;
54+
a = -b;
55+
b = temp;
56+
}
57+
58+
point lineLineIntersection(double a1, double b1, double c1,
59+
double a2, double b2, double c2) {
60+
double determinant = a1*b2 - a2*b1;
61+
if(determinant == 0)
62+
return point(FLT_MAX, FLT_MAX, 'I');
63+
else {
64+
double x = (b2*c1 - b1*c2)/determinant;
65+
double y = (a1*c2 - a2*c1)/determinant;
66+
return point(x, y, 'I');
67+
}
68+
}
69+
70+
point findCircumCenter(point P, point Q, point R) {
71+
double a, b, c;
72+
lineFromPoints(P, Q, a, b, c);
73+
74+
double e, f, g;
75+
lineFromPoints(Q, R, e, f, g);
76+
77+
perpendicularBisectorFromLine(P, Q, a, b, c);
78+
perpendicularBisectorFromLine(Q, R, e, f, g);
79+
80+
point circumcenter = lineLineIntersection(a, b, c, e, f, g);
81+
82+
return circumcenter;
83+
}
84+
85+
bool solve(point c, double r) {
86+
bool ok = true;
87+
for(int k = 0; k < n; ++k) {
88+
if(p[k].t == 0 && dist(c, p[k]) - 1e-9 > r) {
89+
ok = false;
90+
break;
91+
}
92+
93+
if(p[k].t == 1 && dist(c, p[k]) + 1e-9 <= r) {
94+
ok = false;
95+
break;
96+
}
97+
}
98+
99+
return ok;
100+
}
101+
102+
int main() {
103+
#ifndef ONLINE_JUDGE
104+
freopen("in", "r", stdin);
105+
#endif
106+
107+
scanf("%d", &n);
108+
int I, N;
109+
I = N = 0;
110+
for(int i = 0, x, y; i < n; ++i) {
111+
char c;
112+
scanf("%d %d %c", &x, &y, &c);
113+
p[i] = point(x, y, c);
114+
I += c == 'I';
115+
N += c == 'N';
116+
}
117+
118+
if(I <= 1) {
119+
puts("No");
120+
return 0;
121+
}
122+
123+
for(int i = 0; i < n; ++i) {
124+
for(int j = i + 1; j < n; ++j) {
125+
point c;
126+
c.x = (p[i].x + p[j].x) / 2.0;
127+
c.y = (p[i].y + p[j].y) / 2.0;
128+
double r = dist(p[i], c);
129+
130+
if(solve(c, r)) {
131+
puts("No");
132+
return 0;
133+
}
134+
135+
for(int k = j + 1; k < n; ++k) {
136+
point c = findCircumCenter(p[i], p[j], p[k]);
137+
double r = dist(p[i], c);
138+
139+
if(solve(c, r)) {
140+
puts("No");
141+
return 0;
142+
}
143+
}
144+
}
145+
}
146+
147+
puts("Yes");
148+
149+
return 0;
150+
}

CodeForces/1043A. Elections.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 101;
6+
int n, a[N];
7+
8+
int main() {
9+
int mx = -1, t = 0;
10+
scanf("%d", &n);
11+
for(int i = 0; i < n; ++i)
12+
scanf("%d", a + i), t += a[i], mx = max(mx, a[i]);
13+
14+
for(int i = mx; i < 10000; ++i) {
15+
int cur = 0;
16+
for(int j = 0; j < n; ++j) {
17+
cur += i - a[j];
18+
}
19+
20+
if(cur > t) {
21+
printf("%d\n", i);
22+
return 0;
23+
}
24+
}
25+
26+
return 0;
27+
}

CodeForces/1043B. Lost Array.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1001;
6+
int n, a[N], x[N];
7+
8+
int main() {
9+
scanf("%d", &n);
10+
for(int i = 1; i <= n; ++i)
11+
scanf("%d", a + i);
12+
13+
vector<int> sol;
14+
for(int i = 1; i <= n; ++i) {
15+
x[i - 1] = a[i] - a[i - 1];
16+
bool ok = true;
17+
for(int j = 1, cnt = 0; j <= n; ++j, ++cnt) {
18+
if(x[cnt % i] + a[j - 1] != a[j]) {
19+
ok = false;
20+
break;
21+
}
22+
}
23+
if(ok)
24+
sol.push_back(i);
25+
}
26+
27+
printf("%d\n", int(sol.size()));
28+
for(int i = 0; i < sol.size(); ++i)
29+
printf("%d ", sol[i]);
30+
31+
return 0;
32+
}

CodeForces/1043C. Smallest Word.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+
string s, tmp;
6+
vector<int> sol;
7+
8+
int main() {
9+
cin >> s;
10+
11+
for(int i = 0; i < s.length(); ++i) {
12+
if(i + 1 < s.length() && s[i] == 'b' && s[i + 1] == 'a') {
13+
reverse(s.begin(), s.begin() + i);
14+
sol.push_back(i);
15+
continue;
16+
}
17+
18+
if(i + 1 < s.length() && s[i] == 'a' && s[i + 1] == 'b' ||
19+
i == s.length() - 1 && s[i] == 'a') {
20+
reverse(s.begin(), s.begin() + i);
21+
sol.push_back(i);
22+
}
23+
}
24+
25+
for(int i = 0; i < s.length(); ++i) {
26+
if(binary_search(sol.begin(), sol.end(), i))
27+
printf("1 ");
28+
else
29+
printf("0 ");
30+
}
31+
puts("");
32+
33+
return 0;
34+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1, M = 1e9 + 9;
6+
int n, m, a[N][10], p, idxs[N][10];
7+
long long hash_value[N][10], p_pow[N], ip_pow[N];
8+
9+
long long fst(long long b, long long p) {
10+
if(p == 0)
11+
return 1;
12+
if(p == 1)
13+
return b;
14+
15+
long long ret = fst(b, p >> 1) % M;
16+
ret = ret * ret % M;
17+
18+
if(p & 1)
19+
ret = ret * b % M;
20+
21+
return ret;
22+
}
23+
24+
void compute_hash(int idx) {
25+
hash_value[1][idx] = (1ll * a[1][idx] * p_pow[1]) % M;
26+
for(int i = 2; i <= n; ++i)
27+
hash_value[i][idx] = (hash_value[i - 1][idx] + (1ll * a[i][idx] * p_pow[i]) % M) % M;
28+
}
29+
30+
bool can(int mid, int v) {
31+
if(idxs[v][0] + mid - 1 > n)
32+
return false;
33+
long long cur = (1ll * (((hash_value[idxs[v][0] + mid - 1][0] - hash_value[idxs[v][0] - 1][0]) + M) % M) * ip_pow[idxs[v][0]]) % M;
34+
for(int i = 0; i < m; ++i) {
35+
if(idxs[v][i] + mid - 1 > n)
36+
return false;
37+
if((1ll * (((hash_value[idxs[v][i] + mid - 1][i] - hash_value[idxs[v][i] - 1][i]) + M) % M) * ip_pow[idxs[v][i]]) % M != cur)
38+
return false;
39+
}
40+
return true;
41+
}
42+
43+
int calc(int v) {
44+
int ret = 0, l, r, mid, res;
45+
46+
l = 1, r = N, mid, res = 0;
47+
while(l <= r) {
48+
mid = (l + r) >> 1;
49+
if(can(mid, v))
50+
res = mid, l = mid + 1;
51+
else
52+
r = mid - 1;
53+
}
54+
ret += res;
55+
56+
return ret;
57+
}
58+
59+
int main() {
60+
p = 100003;
61+
p_pow[1] = ip_pow[1] = 1;
62+
for(int i = 2; i < N; ++i) {
63+
p_pow[i] = (1ll * p_pow[i - 1] * p) % M;
64+
ip_pow[i] = fst(p_pow[i], M - 2);
65+
}
66+
67+
scanf("%d %d", &n, &m);
68+
for(int i = 0; i < m; ++i) {
69+
for(int j = 1; j <= n; ++j) {
70+
scanf("%d", &a[j][i]);
71+
idxs[a[j][i]][i] = j;
72+
}
73+
compute_hash(i);
74+
}
75+
76+
long long res = 0;
77+
for(int i = 1; i <= n; ++i)
78+
res += calc(i);
79+
80+
printf("%lld\n", res);
81+
82+
return 0;
83+
}

CodeForces/1077A. Frog Jumping.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int t, a, b, k;
6+
7+
int main() {
8+
scanf("%d", &t);
9+
while(t-- != 0) {
10+
scanf("%d %d %d", &a, &b, &k);
11+
12+
int aa = (k + 1) / 2;
13+
int bb = k / 2;
14+
15+
long long res = 1ll * a * aa - 1ll * b * bb;
16+
printf("%lld\n", res);
17+
}
18+
19+
return 0;
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e2 + 1;
6+
int n, a[N];
7+
8+
int main() {
9+
scanf("%d", &n);
10+
for(int i = 0; i < n; ++i)
11+
scanf("%d", a + i);
12+
13+
int res = 0;
14+
for(int i = 1; i < n - 1; ++i)
15+
if(a[i] == 0 && a[i - 1] == 1 && a[i + 1] == 1)
16+
a[i + 1] = 0, ++res;
17+
18+
printf("%d\n", res);
19+
20+
return 0;
21+
}

0 commit comments

Comments
 (0)