Skip to content

Commit 78e97a2

Browse files
committed
Solved problems 1016[A, B] from codeforces
1 parent cee0cc7 commit 78e97a2

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

CodeForces/1016A. Death Note.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Idea:
3+
- Math, Implementation.
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
int const N = 2e5 + 1;
11+
int n, m, a[N];
12+
13+
int main() {
14+
scanf("%d %d", &n, &m);
15+
for(int i = 0; i < n; ++i)
16+
scanf("%d", a + i);
17+
18+
int rem = m;
19+
for(int d = 0; d < n; ++d) {
20+
int cur = a[d], res = 0;
21+
if(cur < rem)
22+
rem -= cur;
23+
else if(cur == rem)
24+
++res, rem = m;
25+
else {
26+
cur -= rem;
27+
rem = m;
28+
++res;
29+
30+
if(cur < rem)
31+
rem -= cur;
32+
else if(cur == rem)
33+
++res, rem = m;
34+
else {
35+
res += cur / rem;
36+
cur = cur % rem;
37+
rem -= cur;
38+
}
39+
}
40+
41+
printf("%d ", res);
42+
}
43+
44+
puts("");
45+
46+
return 0;
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Idea:
3+
- Brute force to find all occurrences of string `t` in `s`,
4+
then use this information to answer the queries.
5+
*/
6+
7+
#include <bits/stdc++.h>
8+
9+
using namespace std;
10+
11+
int const N = 1e3 + 1;
12+
char s[N], t[N];
13+
int n, m, q;
14+
vector<int> all;
15+
16+
int main() {
17+
scanf("%d %d %d", &n, &m, &q);
18+
scanf("%s\n%s", s, t);
19+
20+
int slen = strlen(s);
21+
int tlen = strlen(t);
22+
23+
for(int i = 0; i + tlen - 1 < slen; ++i) {
24+
bool ok = true;
25+
for(int j = 0, k = i; j < tlen; ++j, ++k)
26+
if(s[k] != t[j]) {
27+
ok = false;
28+
break;
29+
}
30+
if(ok)
31+
all.push_back(i);
32+
}
33+
34+
for(int i = 0, a, b; i < q; ++i) {
35+
int res = 0;
36+
scanf("%d %d", &a, &b);
37+
--a, --b;
38+
for(int j = 0; j < all.size(); ++j)
39+
if(all[j] >= a && all[j] + tlen - 1 <= b)
40+
++res;
41+
printf("%d\n", res);
42+
}
43+
44+
return 0;
45+
}

CodeForces/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,5 @@
538538
- [1015D. Walking Between Houses](http://codeforces.com/contest/1015/problem/D)
539539
- [1015E1. Stars Drawing (Easy Edition)](http://codeforces.com/contest/1015/problem/E1)
540540
- [1015E2. Stars Drawing (Hard Edition)](http://codeforces.com/contest/1015/problem/E2)
541+
- [1016A. Death Note](http://codeforces.com/contest/1016/problem/A)
542+
- [1016B. Segment Occurrences](http://codeforces.com/contest/1016/problem/B)

0 commit comments

Comments
 (0)