Skip to content

Commit f880aea

Browse files
committed
Solved problem 1015[A, B, C, D, E1] from codeforces
1 parent 7a3b5cc commit f880aea

6 files changed

+336
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Idea:
3+
- Implementation.
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
int const N = 1e2 + 1;
11+
int n, m, a[N], b[N];
12+
vector<int> sol;
13+
14+
int main() {
15+
scanf("%d %d", &n, &m);
16+
for(int i = 0; i < n; ++i)
17+
scanf("%d %d", a + i, b + i);
18+
19+
for(int i = 1; i <= m; ++i) {
20+
bool ok = true;
21+
for(int j = 0; j < n; ++j) {
22+
if(i >= a[j] && i <= b[j]) {
23+
ok = false;
24+
break;
25+
}
26+
}
27+
if(ok)
28+
sol.push_back(i);
29+
}
30+
31+
printf("%d\n", int(sol.size()));
32+
for(int i = 0; i < sol.size(); ++i)
33+
printf("%d ", sol[i]);
34+
puts("");
35+
36+
return 0;
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Idea:
3+
- Greedy.
4+
- In each time try to set the most last character in the
5+
string `s` to the same character in the same position in
6+
string `t`.
7+
*/
8+
9+
#include <bits/stdc++.h>
10+
11+
using namespace std;
12+
13+
int n;
14+
string s, t;
15+
vector<int> sol;
16+
17+
int main() {
18+
cin >> n;
19+
cin >> s >> t;
20+
21+
for(int i = n - 1; i >= 0; --i) {
22+
if(s[i] == t[i])
23+
continue;
24+
25+
int lst = -1;
26+
for(int j = 0; j < i; ++j)
27+
if(s[j] == t[i])
28+
lst = j;
29+
30+
for(int j = lst; j < i; ++j) {
31+
swap(s[j], s[j + 1]);
32+
sol.push_back(j + 1);
33+
}
34+
}
35+
36+
if(s != t) {
37+
puts("-1");
38+
return 0;
39+
}
40+
41+
printf("%d\n", int(sol.size()));
42+
for(int i = 0; i < sol.size(); ++i)
43+
printf("%d ", sol[i]);
44+
puts("");
45+
46+
return 0;
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Idea:
3+
- Greedy.
4+
- Sort the audios in decreasing order using the difference
5+
between `b` and `a`.
6+
*/
7+
8+
#include <bits/stdc++.h>
9+
10+
using namespace std;
11+
12+
int const N = 1e5 + 1;
13+
int n, m;
14+
long long t;
15+
pair<int, int> a[N];
16+
17+
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
18+
return (a.first - a.second) > (b.first - b.second);
19+
}
20+
21+
int main() {
22+
scanf("%d %d", &n, &m);
23+
for(int i = 0; i < n; ++i) {
24+
scanf("%d %d", &a[i].first, &a[i].second);
25+
t += a[i].first;
26+
}
27+
28+
long long need = t - m;
29+
30+
if(need <= 0) {
31+
puts("0");
32+
return 0;
33+
}
34+
35+
sort(a, a + n, cmp);
36+
37+
int res = 0;
38+
for(int i = 0; i < n; ++i) {
39+
if(need <= 0)
40+
break;
41+
++res;
42+
need -= a[i].first - a[i].second;
43+
}
44+
45+
if(need <= 0)
46+
printf("%d\n", res);
47+
else
48+
puts("-1");
49+
50+
return 0;
51+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Idea:
3+
- Greedy.
4+
- Try to finish `s` with the longest move you have `n - 1`,
5+
then greedly try to finish the remaining `k` steps.
6+
*/
7+
8+
#include <bits/stdc++.h>
9+
10+
using namespace std;
11+
12+
long long n, k, s;
13+
vector<int> sol;
14+
15+
int main() {
16+
scanf("%lld %lld %lld", &n, &k, &s);
17+
18+
if(s < k || s > k * (n - 1)) {
19+
puts("NO");
20+
return 0;
21+
}
22+
23+
sol.push_back(1);
24+
25+
bool ok = true;
26+
long long cur = 1, nxt[] = {1, n};
27+
while(s >= abs(cur - nxt[ok])) {
28+
s -= abs(cur - nxt[ok]);
29+
cur = nxt[ok];
30+
ok = !ok;
31+
sol.push_back(cur);
32+
--k;
33+
}
34+
35+
int step;
36+
if(s == k) {
37+
if(!ok) {
38+
step = -1;
39+
for(int i = cur - 1; k != 0; --k, i += step) {
40+
if(i == 0)
41+
i = 2, step = 1;
42+
if(i == n + 1)
43+
i = n - 1, step = -1;
44+
sol.push_back(i);
45+
}
46+
} else {
47+
step = 1;
48+
for(int i = cur + 1; k != 0; --k, i += step) {
49+
if(i == 0)
50+
i = 2, step = 1;
51+
if(i == n + 1)
52+
i = n - 1, step = -1;
53+
sol.push_back(i);
54+
}
55+
}
56+
} else if(s > k) {
57+
long long need = s - k + 1;
58+
s -= need;
59+
--k;
60+
if(ok) {
61+
step = 1;
62+
sol.push_back(sol.back() + need);
63+
for(int i = sol.back() + 1; k != 0; --k, i += step) {
64+
if(i == 0)
65+
i = 2, step = 1;
66+
if(i == n + 1)
67+
i = n - 1, step = -1;
68+
sol.push_back(i);
69+
}
70+
} else {
71+
step = -1;
72+
sol.push_back(sol.back() - need);
73+
for(int i = sol.back() - 1; k != 0; --k, i += step) {
74+
if(i == 0)
75+
i = 2, step = 1;
76+
if(i == n + 1)
77+
i = n - 1, step = -1;
78+
sol.push_back(i);
79+
}
80+
}
81+
} else {
82+
while(s < k) {
83+
sol.pop_back();
84+
s += abs(1 - n);
85+
++k;
86+
ok = !ok;
87+
}
88+
long long need = s - k + 1;
89+
s -= need;
90+
--k;
91+
if(ok) {
92+
step = 1;
93+
sol.push_back(sol.back() + need);
94+
for(int i = sol.back() + 1; k != 0; --k, i += step) {
95+
if(i == 0)
96+
i = 2, step = 1;
97+
if(i == n + 1)
98+
i = n - 1, step = -1;
99+
sol.push_back(i);
100+
}
101+
} else {
102+
step = -1;
103+
sol.push_back(sol.back() - need);
104+
for(int i = sol.back() - 1; k != 0; --k, i += step) {
105+
if(i == 0)
106+
i = 2, step = 1;
107+
if(i == n + 1)
108+
i = n - 1, step = -1;
109+
sol.push_back(i);
110+
}
111+
}
112+
}
113+
114+
puts("YES");
115+
for(int i = 1; i < sol.size(); ++i)
116+
printf("%d ", sol[i]);
117+
puts("");
118+
119+
return 0;
120+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Idea:
3+
- Greedy.
4+
- Start from each '*' and try to expand as you can.
5+
*/
6+
7+
#include <bits/stdc++.h>
8+
9+
using namespace std;
10+
11+
struct node {
12+
int x, y, c;
13+
node() {}
14+
node(int x, int y, int c) :
15+
x(x), y(y), c(c) {}
16+
};
17+
18+
int n, m, ii[4], jj[4];
19+
bool vis[101][101];
20+
char g[101][101];
21+
vector<node> sol;
22+
23+
bool check() {
24+
for(int i = 0; i < 4; ++i)
25+
if(ii[i] < 0 || ii[i] >= n || jj[i] < 0 || jj[i] >= m || g[ii[i]][jj[i]] != '*')
26+
return false;
27+
return true;
28+
}
29+
30+
int can(int i, int j) {
31+
int c = 0;
32+
if(g[i][j] != '*')
33+
return c;
34+
ii[0] = i + 1, jj[0] = j;
35+
ii[1] = i - 1, jj[1] = j;
36+
ii[2] = i, jj[2] = j + 1;
37+
ii[3] = i, jj[3] = j - 1;
38+
while(check()) {
39+
++c;
40+
for(int k = 0; k < 4; ++k)
41+
vis[ii[k]][jj[k]] = true;
42+
++ii[0];
43+
--ii[1];
44+
++jj[2];
45+
--jj[3];
46+
}
47+
if(c > 0)
48+
vis[i][j] = true;
49+
return c;
50+
}
51+
52+
int main() {
53+
scanf("%d %d", &n, &m);
54+
for(int i = 0; i < n; ++i)
55+
scanf("%s", g[i]);
56+
57+
for(int i = 0; i < n; ++i)
58+
for(int j = 0, cur; j < m; ++j) {
59+
cur = can(i, j);
60+
if(cur > 0)
61+
sol.push_back(node(i, j, cur));
62+
}
63+
64+
for(int i = 0; i < n; ++i)
65+
for(int j = 0; j < m; ++j)
66+
if(g[i][j] == '*' && !vis[i][j]) {
67+
puts("-1");
68+
return 0;
69+
}
70+
71+
printf("%d\n", int(sol.size()));
72+
for(int i = 0; i < sol.size(); ++i)
73+
printf("%d %d %d\n", sol[i].x + 1, sol[i].y + 1, sol[i].c);
74+
75+
return 0;
76+
}

CodeForces/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,8 @@
527527
- [1011B. Planning The Expedition](http://codeforces.com/contest/1011/problem/B)
528528
- [1011C. Fly](http://codeforces.com/contest/1011/problem/C)
529529
- [1011D. Rocket](http://codeforces.com/contest/1011/problem/D)
530+
- [1015A. Points in Segments](http://codeforces.com/contest/1015/problem/A)
531+
- [1015B. Obtaining the String](http://codeforces.com/contest/1015/problem/B)
532+
- [1015C. Songs Compression](http://codeforces.com/contest/1015/problem/C)
533+
- [1015D. Walking Between Houses](http://codeforces.com/contest/1015/problem/D)
534+
- [1015E1. Stars Drawing (Easy Edition)](http://codeforces.com/contest/1015/problem/E1)

0 commit comments

Comments
 (0)