Skip to content

Commit 5882520

Browse files
committed
Solve problems 449B and 551B from codeforces and 40. Erase Value and 40. Move the Bishop from csacademy
1 parent 920aab1 commit 5882520

6 files changed

+198
-0
lines changed

CS Academy/40. Erase Value.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int n, freq[1001];
7+
8+
int main() {
9+
scanf("%d", &n);
10+
int total = 0;
11+
for(int i = 0, tmp; i < n; ++i) {
12+
scanf("%d", &tmp);
13+
total += tmp;
14+
++freq[tmp];
15+
}
16+
17+
int res = 1e7;
18+
for(int i = 0; i < 1001; ++i)
19+
res = min(res, total - (freq[i] * i));
20+
21+
printf("%d\n", res);
22+
23+
return 0;
24+
}

CS Academy/40. Move the Bishop.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int main() {
6+
int x1, y1, x2, y2;
7+
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
8+
9+
if(x1 == x2 && y1 == y2) {
10+
puts("0");
11+
return 0;
12+
}
13+
14+
if(abs(x1 - x2) == abs(y1 - y2)) {
15+
puts("1");
16+
return 0;
17+
}
18+
19+
for(int i = x1 + 1, j = y1 + 1; i <= 8 && j <= 8; ++i, ++j)
20+
if(abs(i - x2) == abs(j - y2)) {
21+
puts("2");
22+
return 0;
23+
}
24+
for(int i = x1 + 1, j = y1 - 1; i <= 8 && j >= 0; ++i, --j)
25+
if(abs(i - x2) == abs(j - y2)) {
26+
puts("2");
27+
return 0;
28+
}
29+
for(int i = x1 - 1, j = y1 + 1; i >= 0 && j <= 8; --i, ++j)
30+
if(abs(i - x2) == abs(j - y2)) {
31+
puts("2");
32+
return 0;
33+
}
34+
for(int i = x1 - 1, j = y1 - 1; i >= 0 && j >= 0; --i, --j)
35+
if(abs(i - x2) == abs(j - y2)) {
36+
puts("2");
37+
return 0;
38+
}
39+
40+
puts("-1");
41+
42+
return 0;
43+
}

CS Academy/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
- [26. Limited Vocabulary](https://csacademy.com/contest/round-26/#task/limited-vocabulary)
66
- [26. Odd Pair Sums](https://csacademy.com/contest/round-26/#task/odd-pair-sums)
77
- [27. Backpack Packing](https://csacademy.com/contest/round-27/#task/backpack-packing)
8+
- [40. Erase Value](https://csacademy.com/contest/round-40/task/erase-value)
9+
- [40. Move the Bishop](https://csacademy.com/contest/round-40/task/move-the-bishop)

CodeForces/449B. Jzzhu and Cities.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdio.h>
2+
#include <vector>
3+
#include <queue>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
int const N = 1e5 + 1;
9+
int n, m, k, res;
10+
long long cost[N];
11+
bool vis[N];
12+
vector<vector<pair<int, int> > > g;
13+
priority_queue<pair<pair<long long, int>, bool> > q;
14+
15+
void Dijkstra(int src) {
16+
fill(cost, cost + N, 1e18);
17+
q.push(make_pair(make_pair(0, src), false));
18+
cost[src] = 0;
19+
20+
while(!q.empty()) {
21+
int v = q.top().first.second;
22+
long long c = -q.top().first.first;
23+
bool train = q.top().second;
24+
q.pop();
25+
26+
if(cost[v] <= c && train) {
27+
++res;
28+
continue;
29+
} else if(cost[v] > c && train)
30+
cost[v] = c;
31+
32+
if(vis[v])
33+
continue;
34+
vis[v] = true;
35+
36+
for(int i = 0, u; i < (int)g[v].size(); ++i) {
37+
u = g[v][i].first;
38+
if(1LL * c + g[v][i].second < cost[u]) {
39+
cost[u] = 1LL * c + g[v][i].second;
40+
q.push(make_pair(make_pair(-cost[u], u), false));
41+
}
42+
}
43+
}
44+
}
45+
46+
int main() {
47+
scanf("%d %d %d", &n, &m, &k);
48+
g.resize(n);
49+
50+
for(int i = 0, a, b, c; i < m; ++i) {
51+
scanf("%d %d %d", &a, &b, &c);
52+
--a, --b;
53+
g[a].push_back(make_pair(b, c));
54+
g[b].push_back(make_pair(a, c));
55+
}
56+
57+
for(int i = 0, a, c; i < k; ++i) {
58+
scanf("%d %d", &a, &c);
59+
--a;
60+
q.push(make_pair(make_pair(-c, a), true));
61+
}
62+
63+
Dijkstra(0);
64+
65+
printf("%d\n", res);
66+
67+
return 0;
68+
}
69+

CodeForces/551B. ZgukistringZ.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <algorithm>
4+
#include <memory.h>
5+
6+
using namespace std;
7+
8+
int main() {
9+
string a, b, c;
10+
cin >> a >> b >> c;
11+
12+
int fa[26] = {0}, fb[26] = {0}, fc[26] = {0};
13+
14+
for(int i = 0; i < (int)a.length(); ++i)
15+
++fa[a[i] - 'a'];
16+
for(int i = 0; i < (int)b.length(); ++i)
17+
++fb[b[i] - 'a'];
18+
for(int i = 0; i < (int)c.length(); ++i)
19+
++fc[c[i] - 'a'];
20+
21+
int cb = 0, cc = 0;
22+
23+
for(int i = 0; i < 1e5 + 1; ++i) {
24+
int tb = 1e9, tc = 1e9;
25+
26+
bool ok = true;
27+
for(int j = 0; j < 26; ++j)
28+
if(fb[j] != 0 && fa[j] < 1LL * fb[j] * i) {
29+
ok = false;
30+
break;
31+
}
32+
33+
if(!ok)
34+
tb = 0;
35+
else
36+
tb = i;
37+
38+
for(int j = 0; j < 26; ++j)
39+
if(fc[j] != 0)
40+
tc = min(1LL * tc, max(0LL, (fa[j] - (1LL * fb[j] * tb)) / fc[j]));
41+
42+
if(tb + tc > cb + cc)
43+
cb = tb, cc = tc;
44+
}
45+
46+
for(int i = 0; i < cb; ++i) cout << b;
47+
for(int i = 0; i < cc; ++i) cout << c;
48+
49+
for(int i = 0; i < 26; ++i) {
50+
fa[i] -= (fb[i] * cb) + (fc[i] * cc);
51+
for(int j = 0; j < fa[i]; ++j)
52+
cout << char(i + 'a');
53+
}
54+
cout << endl;
55+
56+
return 0;
57+
}
58+

CodeForces/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
- [441C. Valera and Tubes](http://codeforces.com/contest/441/problem/C)
9595
- [443A. Anton and Letters](http://codeforces.com/problemset/problem/443/A)
9696
- [448A. Rewards](http://codeforces.com/problemset/problem/448/A)
97+
- [449B. Jzzhu and Cities](http://codeforces.com/contest/449/problem/B)
9798
- [451A. Game With Sticks](http://codeforces.com/contest/451/problem/A)
9899
- [452A. Eevee](http://codeforces.com/problemset/problem/452/A)
99100
- [454A. Little Pony and Crystal Mine](http://codeforces.com/problemset/problem/454/A)
@@ -126,6 +127,7 @@
126127
- [546C - Soldier and Cards](http://codeforces.com/problemset/problem/546/C)
127128
- [546A. Soldier and Bananas](http://codeforces.com/contest/546/problem/A)
128129
- [550C. Divisibility by Eight](http://codeforces.com/problemset/problem/550/C)
130+
- [551B. ZgukistringZ](http://codeforces.com/contest/551/problem/B)
129131
- [552C. Vanya and Scales](http://codeforces.com/contest/552/problem/C)
130132
- [573A. Bear and Poker](http://codeforces.com/contest/573/problem/A)
131133
- [577B. Modulo Sum](http://codeforces.com/contest/577/problem/B)

0 commit comments

Comments
 (0)