Skip to content

Commit 7a3b5cc

Browse files
committed
Solved problems 1011[A, B, C, D] from codeforces
1 parent 62ed4e1 commit 7a3b5cc

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

CodeForces/1011A. Stages.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Idea:
3+
- Implementation.
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
int k, fr[26];
11+
string s;
12+
13+
int main() {
14+
int n;
15+
cin >> n >> k >> s;
16+
17+
for(int i = 0; i < s.length(); ++i)
18+
++fr[s[i] - 'a'];
19+
20+
int best = 1e9;
21+
for(int i = 0; i < 26; ++i) {
22+
int cur = 0, cost = 0, j = i;
23+
while(j < 26) {
24+
if(cur == k)
25+
break;
26+
if(fr[j] != 0)
27+
++cur, cost += (j + 1), j += 2;
28+
else
29+
++j;
30+
}
31+
if(cur == k && cost < best)
32+
best = cost;
33+
}
34+
35+
if(best == 1e9) {
36+
cout << -1 << endl;
37+
return 0;
38+
}
39+
cout << best << endl;
40+
41+
return 0;
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Idea:
3+
- Brute force.
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
int n, m, fr[100];
11+
12+
bool can(int mid) {
13+
int need = n;
14+
for(int i = 0; i < 100; ++i)
15+
need -= fr[i] / mid;
16+
return need <= 0;
17+
}
18+
19+
int main() {
20+
scanf("%d %d", &n, &m);
21+
for(int i = 0, tmp; i < m; ++i) {
22+
scanf("%d", &tmp);
23+
++fr[--tmp];
24+
}
25+
26+
int res = 0;
27+
for(int i = 1; i <= 100; ++i)
28+
if(can(i))
29+
res = i;
30+
31+
printf("%d\n", res);
32+
33+
return 0;
34+
}

CodeForces/1011C. Fly.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Idea:
3+
- Binay search on the minimum value you can add to `m`
4+
to travel over all planets and go back to Earth.
5+
*/
6+
7+
#include <bits/stdc++.h>
8+
9+
using namespace std;
10+
11+
int const N = 1e3 + 10;
12+
int n, m, a[N], b[N];
13+
14+
bool can(long double mid) {
15+
long double cur = 1.0 * m + mid;
16+
for(int i = 0; i < n; ++i) {
17+
cur -= 1.0 * cur / a[i];
18+
cur -= 1.0 * cur / b[i + 1];
19+
}
20+
return cur >= 1.0 * m;
21+
}
22+
23+
int main() {
24+
scanf("%d %d", &n, &m);
25+
for(int i = 0; i < n; ++i)
26+
scanf("%d", a + i);
27+
for(int i = 0; i < n; ++i)
28+
scanf("%d", b + i);
29+
a[n] = a[0];
30+
b[n] = b[0];
31+
32+
double sol = 2e18;
33+
34+
long double l = 1, r = 1e18, mid, res = -1;
35+
for(int i = 0; i < 100; ++i) {
36+
mid = (l + r) / 2.0;
37+
if(can(mid))
38+
res = mid, r = mid;
39+
else
40+
l = mid;
41+
}
42+
43+
cout << fixed << setprecision(10) << res << endl;
44+
45+
return 0;
46+
}

CodeForces/1011D. Rocket.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Idea:
3+
- If we have the sequence `p`, then we can do a Binary Search
4+
to find the value of `x`.
5+
- To find the sequence `p` we can ask `n` times for the same
6+
number `1` and save the answers, if 1 is not the answer
7+
and the rocket says `x < y`, then it is lie.
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
int m, n, tmp, p[31];
15+
16+
int main() {
17+
scanf("%d %d", &m, &n);
18+
19+
for(int i = 0; i < n; ++i) {
20+
puts("1");
21+
fflush(stdout);
22+
scanf("%d", &tmp);
23+
if(tmp == 0)
24+
return 0;
25+
p[i] = tmp;
26+
}
27+
28+
for(int i = 0; i < n; ++i)
29+
if(p[i] == -1)
30+
p[i] = 0;
31+
32+
int l = 1, r = m, mid, res, cnt = 0;
33+
while(l <= r) {
34+
mid = (l + r) / 2;
35+
36+
printf("%d\n", mid);
37+
fflush(stdout);
38+
scanf("%d", &tmp);
39+
40+
if(tmp == 0)
41+
return 0;
42+
43+
if(p[cnt] == 1) {
44+
if(tmp == -1)
45+
r = mid - 1, res = mid;
46+
else
47+
l = mid + 1, res = mid;
48+
} else {
49+
if(tmp == -1)
50+
l = mid + 1, res = mid;
51+
else
52+
r = mid - 1, res = mid;
53+
}
54+
55+
++cnt;
56+
cnt %= n;
57+
}
58+
59+
printf("%d\n", res);
60+
fflush(stdout);
61+
scanf("%d", &tmp);
62+
63+
return 0;
64+
}

CodeForces/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,7 @@
523523
- [1009A. Game Shopping](http://codeforces.com/contest/1009/problem/A)
524524
- [1009B. Minimum Ternary String](http://codeforces.com/contest/1009/problem/B)
525525
- [1009D. Relatively Prime Graph](http://codeforces.com/contest/1009/problem/D)
526+
- [1011A. Stages](http://codeforces.com/contest/1011/problem/B)
527+
- [1011B. Planning The Expedition](http://codeforces.com/contest/1011/problem/B)
528+
- [1011C. Fly](http://codeforces.com/contest/1011/problem/C)
529+
- [1011D. Rocket](http://codeforces.com/contest/1011/problem/D)

0 commit comments

Comments
 (0)