Skip to content

Commit f13878c

Browse files
committed
Solve problems 779A, 779B, 779C and 779D from codeforces
1 parent eba4108 commit f13878c

5 files changed

+163
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int const N = 6;
7+
int n, a[N], b[N];
8+
9+
int main() {
10+
scanf("%d", &n);
11+
for(int i = 0, tmp; i < n; ++i) {
12+
scanf("%d", &tmp);
13+
++a[tmp];
14+
}
15+
for(int i = 0, tmp; i < n; ++i) {
16+
scanf("%d", &tmp);
17+
++b[tmp];
18+
}
19+
20+
int res = 0;
21+
for(int i = 1; i <= 5; ++i) {
22+
if((a[i] + b[i]) % 2 != 0) {
23+
puts("-1");
24+
return 0;
25+
}
26+
27+
int tmp = (a[i] + b[i]) >> 1;
28+
tmp = max(a[i], b[i]) - tmp;
29+
res += tmp;
30+
}
31+
32+
printf("%d\n", res / 2);
33+
34+
return 0;
35+
}

CodeForces/779B. Weird Rounding.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
using namespace std;
5+
6+
int k;
7+
char s[11];
8+
9+
int main() {
10+
scanf("%s %d", s, &k);
11+
12+
int len = strlen(s);
13+
int res = 0, del = 0;
14+
for(int i = len - 1; i >= 0; --i) {
15+
if(s[i] == '0')
16+
++res;
17+
else
18+
++del;
19+
if(res == k)
20+
break;
21+
}
22+
23+
if(res == k)
24+
printf("%d\n", del);
25+
else
26+
printf("%d\n", len - 1);
27+
28+
return 0;
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int const N = 2e5 + 1;
7+
int n, k;
8+
9+
struct node {
10+
int b, a, d;
11+
12+
bool operator<(const node &n) const {
13+
return d < n.d;
14+
}
15+
};
16+
17+
node a[N];
18+
19+
int main() {
20+
scanf("%d %d", &n, &k);
21+
for(int i = 0; i < n; ++i)
22+
scanf("%d", &a[i].b);
23+
for(int i = 0; i < n; ++i) {
24+
scanf("%d", &a[i].a);
25+
a[i].d = a[i].b - a[i].a;
26+
}
27+
sort(a, a + n);
28+
29+
int res = 0, i = 0;
30+
for(; i < k; ++i)
31+
res += a[i].b;
32+
for(; i < n && a[i].d <= 0; ++i)
33+
res += a[i].b;
34+
for(; i < n; ++i)
35+
res += a[i].a;
36+
37+
printf("%d\n", res);
38+
39+
return 0;
40+
}

CodeForces/779D. String Game.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <stdio.h>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <string.h>
5+
6+
using namespace std;
7+
8+
int const N = 2e5 + 1;
9+
int n, m, a[N];
10+
char s[N], p[N];
11+
vector<pair<int, char> > tmp;
12+
13+
bool can(int mid) {
14+
tmp.clear();
15+
16+
for(int i = mid; i < n; ++i)
17+
tmp.push_back(make_pair(a[i], s[a[i]]));
18+
sort(tmp.begin(), tmp.end());
19+
20+
for(int i = 0, j = 0; i < m; ++i) {
21+
while(j < (int)tmp.size() && tmp[j].second != p[i])
22+
++j;
23+
24+
if(j == (int)tmp.size())
25+
return false;
26+
27+
++j;
28+
}
29+
30+
return true;
31+
}
32+
33+
int main() {
34+
scanf("%s %s", s, p);
35+
n = strlen(s);
36+
m = strlen(p);
37+
for(int i = 0; i < n; ++i) {
38+
scanf("%d", a + i);
39+
--a[i];
40+
}
41+
42+
int l = 0, r = n, mid, res = 0;
43+
while(l <= r) {
44+
mid = (l + r) / 2;
45+
if(can(mid)) {
46+
l = mid + 1;
47+
res = mid;
48+
} else
49+
r = mid - 1;
50+
}
51+
52+
printf("%d\n", res);
53+
54+
return 0;
55+
}

CodeForces/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@
201201
- [757A. Gotta Catch Em' All!](http://codeforces.com/contest/757/problem/A)
202202
- [766A. Mahmoud and Longest Uncommon Subsequence](http://codeforces.com/contest/766/problem/A)
203203
- [766D. Mahmoud and a Dictionary](http://codeforces.com/contest/766/problem/D)
204+
- [779A. Pupils Redistribution](http://codeforces.com/contest/779/problem/A)
205+
- [779B. Weird Rounding](http://codeforces.com/contest/779/problem/B)
206+
- [779C. Dishonest Sellers](http://codeforces.com/contest/779/problem/C)
207+
- [779D. String Game](http://codeforces.com/contest/779/problem/D)
204208
- [780A. Andryusha and Socks](http://codeforces.com/problemset/problem/780/A)
205209
- [785A. Anton and Polyhedrons](http://codeforces.com/contest/785/problem/A)
206210
- [785B. Anton and Classes](http://codeforces.com/contest/785/problem/B)

0 commit comments

Comments
 (0)