Skip to content

Commit 6b3e2d1

Browse files
committed
Solve alot of problems :3
1 parent b052bbb commit 6b3e2d1

38 files changed

+1341
-0
lines changed

AtCoder/097C - K-th Substring.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
set<string> st;
6+
7+
int main() {
8+
string s;
9+
cin >> s;
10+
int k;
11+
cin >> k;
12+
13+
for(char ch = 'a'; ch <= 'z'; ++ch) {
14+
for(int i = 0; i < s.length(); ++i)
15+
if(s[i] == ch)
16+
for(int j = i + 1; j <= i + 1 + 10; ++j)
17+
st.insert(s.substr(i, j - i));
18+
if(st.size() >= k)
19+
break;
20+
}
21+
22+
int cnt = 1;
23+
for(set<string>::iterator it = st.begin(); it != st.end(); ++it, ++cnt)
24+
if(cnt == k) {
25+
cout << *it << endl;
26+
return 0;
27+
}
28+
29+
return 0;
30+
}

AtCoder/097D - Equals.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
int n, m, p[N];
7+
bool vis[N];
8+
vector<vector<int> > g;
9+
vector<int> cur;
10+
11+
void DFS(int u) {
12+
vis[u] = true;
13+
cur.push_back(u);
14+
15+
for(int i = 0; i < g[u].size(); ++i)
16+
if(!vis[g[u][i]])
17+
DFS(g[u][i]);
18+
}
19+
20+
int main() {
21+
cin >> n >> m;
22+
g.resize(n);
23+
for(int i = 0; i < n; ++i)
24+
cin >> p[i], --p[i];
25+
for(int i = 0, a, b; i < m; ++i) {
26+
cin >> a >> b;
27+
--a, --b;
28+
g[a].push_back(b);
29+
swap(a, b);
30+
g[a].push_back(b);
31+
}
32+
33+
int res = 0;
34+
for(int i = 0; i < n; ++i) {
35+
if(!vis[i]) {
36+
DFS(i);
37+
38+
sort(cur.begin(), cur.end());
39+
for(int j = 0; j < cur.size(); ++j)
40+
if(binary_search(cur.begin(), cur.end(), p[cur[j]]))
41+
++res;
42+
cur.clear();
43+
}
44+
}
45+
46+
cout << res << endl;
47+
48+
return 0;
49+
}

AtCoder/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
- [072B - OddString](http://abc072.contest.atcoder.jp/tasks/abc072_b)
1919
- [072C - Together](http://abc072.contest.atcoder.jp/tasks/arc082_a)
2020
- [072D - Derangement](http://abc072.contest.atcoder.jp/tasks/arc082_b)
21+
- [097C - K-th Substring](https://arc097.contest.atcoder.jp/tasks/arc097_a)
22+
- [097D - Equals](https://arc097.contest.atcoder.jp/tasks/arc097_b)

CS Academy/79. Cats and Dogs.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 2001;
6+
int n, m, cats[N];
7+
pair<int, int> a[N], b[N];
8+
9+
long long dist(pair<int, int> a, pair<int, int> b) {
10+
return (a.first - b.first) * (a.first - b.first) +
11+
(a.second - b.second) * (a.second - b.second);
12+
}
13+
14+
int main() {
15+
cin >> n >> m;
16+
for(int i = 0; i < n; ++i)
17+
cin >> a[i].first >> a[i].second;
18+
for(int i = 0; i < m; ++i)
19+
cin >> b[i].first >> b[i].second;
20+
21+
for(int i = 0; i < m; ++i) {
22+
long long best = 1e18, idx;
23+
for(int j = 0; j < n; ++j)
24+
if(best > dist(b[i], a[j]))
25+
idx = j, best = dist(b[i], a[j]);
26+
++cats[idx];
27+
}
28+
29+
int res = 0;
30+
for(int i = 0; i < n; ++i)
31+
res += cats[i] == 1;
32+
33+
cout << res << endl;
34+
35+
return 0;
36+
}

CS Academy/79. Milk and Bread.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int main() {
6+
int a, b, c;
7+
cin >> a >> b >> c;
8+
9+
if(a > b)
10+
swap(a, b);
11+
12+
cout << min(abs(c - b) + abs(b - a), abs(c - a) + abs(b - a)) << endl;
13+
14+
return 0;
15+
}

CS Academy/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
- [49. Odd Sum](https://csacademy.com/contest/round-49/task/odd-sum)
1616
- [59. No Repeat](https://csacademy.com/contest/round-59/task/no-repeat)
1717
- [59. Triangular Matrix](https://csacademy.com/contest/round-59/task/triangular-matrix)
18+
- [79. Milk and Bread](https://csacademy.com/contest/round-79/task/milk-and-bread/)
19+
- [79. Cats and Dogs](https://csacademy.com/contest/round-79/task/cats-and-dogs/)

CodeForces/17A. Noldbach problem.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const MAX = 1000001;
6+
bitset<MAX> prime;
7+
int n, k, primes[MAX], cnt;
8+
9+
void sieve_of_eratosthenes() {
10+
prime.set();
11+
12+
prime[0] = prime[1] = 0;
13+
for(int i = 4; i < MAX; i += 2)
14+
prime[i] = 0;
15+
16+
for(int i = 3; i * i < MAX; i += 2)
17+
if(prime[i])
18+
for(int j = i * i; j < MAX; j += (i << 1))
19+
prime[j] = 0;
20+
21+
for(int i = 0; i < MAX; ++i)
22+
if(prime[i])
23+
primes[cnt++] = i;
24+
}
25+
26+
int main() {
27+
sieve_of_eratosthenes();
28+
29+
scanf("%d %d", &n, &k);
30+
31+
int res = 0;
32+
for(int i = 0; i < cnt; ++i) {
33+
if(primes[i] > n)
34+
break;
35+
36+
for(int j = 1; j < i; ++j)
37+
if(primes[j - 1] + primes[j] + 1 == primes[i]) {
38+
++res;
39+
break;
40+
}
41+
}
42+
43+
if(res >= k)
44+
puts("YES");
45+
else
46+
puts("NO");
47+
48+
return 0;
49+
}

CodeForces/193A. Cutting Figure.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 51;
6+
int n, m;
7+
int dx[] = {0, 1, 0, -1};
8+
int dy[] = {1, 0, -1, 0};
9+
char s[N][N];
10+
bool vis[N][N];
11+
12+
void DFS(int i, int j) {
13+
vis[i][j] = true;
14+
15+
for(int k = 0, nx, ny; k < 4; ++k) {
16+
nx = i + dx[k];
17+
ny = j + dy[k];
18+
if(nx < 0 || nx >= n || ny < 0 || ny >= m || vis[nx][ny] || s[nx][ny] == '.')
19+
continue;
20+
DFS(nx, ny);
21+
}
22+
}
23+
24+
int main() {
25+
scanf("%d %d", &n, &m);
26+
for(int i = 0; i < n; ++i)
27+
scanf("%s", s[i]);
28+
29+
int tmp = 0;
30+
for(int i = 0; i < n; ++i)
31+
for(int j = 0; j < m; ++j)
32+
tmp += (s[i][j] == '#');
33+
if(tmp <= 2) {
34+
puts("-1");
35+
return 0;
36+
}
37+
38+
for(int i = 0; i < n; ++i)
39+
for(int j = 0; j < m; ++j)
40+
if(s[i][j] == '#') {
41+
memset(vis, false, sizeof vis);
42+
s[i][j] = '.';
43+
int comp = 0;
44+
for(int k = 0; k < n; ++k)
45+
for(int l = 0; l < m; ++l)
46+
if(!vis[k][l] && s[k][l] == '#') {
47+
++comp;
48+
DFS(k, l);
49+
}
50+
if(comp > 1) {
51+
puts("1");
52+
return 0;
53+
}
54+
s[i][j] = '#';
55+
}
56+
57+
puts("2");
58+
59+
return 0;
60+
}

CodeForces/964A. Splits.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main() {
6+
int n;
7+
cin >> n;
8+
9+
if(n == 1) {
10+
cout << 1 << endl;
11+
} else if(n % 2 == 0) {
12+
cout << n - (n / 2 - 1) << endl;
13+
} else {
14+
cout << n - (n / 2) << endl;
15+
}
16+
17+
return 0;
18+
}

CodeForces/964B. Messages.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, A, B, C, T, a[1001], fr[1001];
6+
7+
int main() {
8+
cin >> n >> A >> B >> C >> T;
9+
for(int i = 0; i < n; ++i) {
10+
cin >> a[i];
11+
++fr[a[i]];
12+
}
13+
sort(a, a + n);
14+
15+
long long r1 = A * n, r2 = 0;
16+
17+
for(int i = 0; i < n; ++i) {
18+
int diff = T - a[i];
19+
r2 += A - (B * diff);
20+
}
21+
22+
for(int i = 0, cnt = 0; i < T; ++i) {
23+
cnt += fr[i];
24+
r2 += (1ll * C * cnt);
25+
}
26+
27+
cout << max(r1, r2) << endl;
28+
29+
return 0;
30+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
int t, n, m, a[N];
7+
char s[N];
8+
9+
int main() {
10+
cin >> n;
11+
string s;
12+
cin >> s;
13+
14+
if(n == 1) {
15+
cout << s << endl;
16+
return 0;
17+
}
18+
19+
int z = 0, o = 0;
20+
for(int i = 0; i < s.length(); ++i)
21+
if(s[i] == '0')
22+
++z;
23+
else
24+
++o;
25+
26+
if(o > 0)
27+
cout << 1;
28+
for(int i = 0; i < z; ++i)
29+
cout << 0;
30+
cout << endl;
31+
32+
return 0;
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
long long t, n, m, a[N];
7+
long long k;
8+
char s[N];
9+
10+
int main() {
11+
cin >> n >> m >> k;
12+
13+
if(k < n) {
14+
++k;
15+
cout << k << ' ' << 1 << endl;
16+
return 0;
17+
}
18+
19+
if(k == n) {
20+
cout << n << ' ' << 2 << endl;
21+
return 0;
22+
}
23+
24+
k -= n;
25+
26+
long long tmp = m;
27+
m -= 2;
28+
29+
long long can = k / (m + 1);
30+
31+
long long x = n, y = 2;
32+
33+
if(can % 2 == 0) {
34+
x -= can;
35+
} else {
36+
x -= can;
37+
y = tmp;
38+
}
39+
40+
long long mod = k % (m + 1);
41+
42+
if(y == 2)
43+
y += mod;
44+
else
45+
y -= mod;
46+
47+
cout << x << ' ' << y << endl;
48+
49+
return 0;
50+
}

0 commit comments

Comments
 (0)