Skip to content

Commit 5942bb8

Browse files
committed
Solved problems 1009[A, B, D] from codeforces
1 parent 4e11470 commit 5942bb8

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1001;
6+
int n, m, c[N], a[N];
7+
8+
int main() {
9+
scanf("%d %d", &n, &m);
10+
for(int i = 0; i < n; ++i)
11+
scanf("%d", c + i);
12+
for(int i = 0; i < m; ++i)
13+
scanf("%d", a + i);
14+
15+
int res = 0;
16+
for(int i = 0, cur = 0; i < n; ++i) {
17+
if(a[cur] >= c[i])
18+
++cur, ++res;
19+
if(cur >= m)
20+
break;
21+
}
22+
23+
printf("%d\n", res);
24+
25+
return 0;
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
char s[N], sol[N];
7+
8+
int main() {
9+
scanf("%s", s);
10+
int len = strlen(s);
11+
12+
int cnt = 0;
13+
for(int i = 0; i < len; ++i)
14+
cnt += s[i] == '1';
15+
16+
for(int i = 0, j = 0; i < len; ++i)
17+
if(s[i] != '1')
18+
sol[j++] = s[i];
19+
20+
for(int i = 0; i < len; ++i)
21+
if(s[i] == '2')
22+
break;
23+
else if(s[i] != '1')
24+
putchar('0');
25+
26+
for(int i = 0; i < cnt; ++i)
27+
putchar('1');
28+
29+
bool ok = false;
30+
for(int i = 0; i < len; ++i) {
31+
if(s[i] == '2')
32+
ok = true;
33+
if(ok && s[i] != '1')
34+
putchar(s[i]);
35+
}
36+
37+
puts("");
38+
39+
return 0;
40+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
int n, m, v;
7+
pair<int, int> phi[N];
8+
set<pair<int, int> > e;
9+
10+
void print(int x, int &cnt) {
11+
for(int i = 1; cnt > 0 && i <= n; ++i)
12+
if(i != x && __gcd(x, i) == 1) {
13+
if(e.count({min(x, i), max(x, i)}) == 1)
14+
continue;
15+
e.insert({min(x, i), max(x, i)});
16+
printf("%d %d\n", x, i), --cnt;
17+
}
18+
}
19+
20+
int main() {
21+
scanf("%d %d", &n, &m);
22+
23+
for(int i = 1; i <= n; ++i)
24+
phi[i].first = phi[i].second = i;
25+
26+
for(int p = 2; p <= n; ++p)
27+
if(phi[p].first == p) {
28+
phi[p].first = p - 1;
29+
30+
for(int i = 2 * p; i <= n; i += p)
31+
phi[i].first = (phi[i].first / p) * (p - 1);
32+
}
33+
34+
long long to = 0;
35+
for(int i = 1; i <= n; ++i)
36+
to += phi[i].first;
37+
38+
if(to - 1 < m || m < n - 1) {
39+
puts("Impossible");
40+
return 0;
41+
}
42+
43+
puts("Possible");
44+
45+
for(int i = n; i > 0; --i)
46+
if(phi[i].first == phi[i].second - 1) {
47+
v = phi[i].second;
48+
break;
49+
}
50+
51+
for(int i = 1; i <= n; ++i) {
52+
if(i == v)
53+
continue;
54+
55+
printf("%d %d\n", v, i);
56+
e.insert({min(v, i), max(v, i)});
57+
}
58+
m -= (n - 1);
59+
60+
sort(phi + 1, phi + n + 1);
61+
reverse(phi + 1, phi + n + 1);
62+
63+
for(int i = 1; m > 0 && i <= n; ++i)
64+
print(phi[i].second, m);
65+
66+
return 0;
67+
}

CodeForces/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,6 @@
488488
- [1005C. Summarize to the Power of Two](http://codeforces.com/contest/1005/problem/C)
489489
- [1005D. Polycarp and Div 3](http://codeforces.com/contest/1005/problem/D)
490490
- [1005E1. Median on Segments (Permutations Edition)](http://codeforces.com/contest/1005/problem/E1)
491+
- [1009A. Game Shopping](http://codeforces.com/contest/1009/problem/A)
492+
- [1009B. Minimum Ternary String](http://codeforces.com/contest/1009/problem/B)
493+
- [1009D. Relatively Prime Graph](http://codeforces.com/contest/1009/problem/D)

0 commit comments

Comments
 (0)