Skip to content

Commit 30cdb97

Browse files
committed
Solved alot of problems :3
1 parent e35f1dd commit 30cdb97

6 files changed

+211
-0
lines changed

CS Academy/82. City Break.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
int n, s;
7+
long long a[N];
8+
9+
long long calc(int u, int v) {
10+
if(u > v)
11+
swap(u, v);
12+
return min(a[v - 1] - a[u - 1], a[u - 1] + a[n] - a[v - 1]);
13+
}
14+
15+
int main() {
16+
scanf("%d %d", &n, &s);
17+
for(int i = 1; i <= n; ++i)
18+
scanf("%lld", a + i);
19+
20+
for(int i = 1; i <= n; ++i)
21+
a[i] += a[i - 1];
22+
23+
int a = s - 1, b = s + 1;
24+
if(a <= 0) a += n;
25+
if(b > n) b -= n;
26+
27+
long long x, y, sol = 0;
28+
for(int i = 0; i < n; ++i) {
29+
x = calc(s, a);
30+
y = calc(s, b);
31+
32+
if(x == y) {
33+
if(a < b) {
34+
s = a;
35+
--a;
36+
} else {
37+
s = b;
38+
++b;
39+
}
40+
41+
sol += x;
42+
} else if(x < y) {
43+
s = a;
44+
--a;
45+
sol += x;
46+
} else {
47+
s = b;
48+
++b;
49+
sol += y;
50+
}
51+
52+
if(a <= 0) a += n;
53+
if(b > n) b -= n;
54+
}
55+
56+
printf("%lld\n", sol);
57+
58+
return 0;
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
int n, m, a[N];
7+
long long t = 0;
8+
9+
int main() {
10+
scanf("%d %d", &n, &m);
11+
for(int i = 0; i < n; ++i)
12+
scanf("%d", a + i);
13+
14+
sort(a, a + n);
15+
for(int i = 0, cur; i < n; ++i) {
16+
cur = 0;
17+
while(i < n && a[i] <= m)
18+
t += a[i], ++cur, ++i;
19+
--i;
20+
m += cur;
21+
if(cur == 0)
22+
break;
23+
}
24+
25+
printf("%lld\n", (1ll * m * (m + 1)) / 2 - t);
26+
27+
return 0;
28+
}

CS Academy/86. Cookie Clicker.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 n, c, s, a[5], b[5], dp[100001][200];
6+
7+
int rec(int cok, int msk, int add) {
8+
if(cok >= c)
9+
return 0;
10+
11+
int &ret = dp[cok][msk];
12+
if(ret != -1)
13+
return ret;
14+
ret = 1e9;
15+
16+
ret = min(ret, rec(cok + s + add, msk, add) + 1);
17+
18+
for(int i = 0; i < n; ++i)
19+
if(((msk >> i) & 1) == 0 && cok >= a[i]) {
20+
ret = min(ret, rec(cok - a[i] + s, msk | (1 << i), add + b[i]) + 1);
21+
ret = min(ret, rec(cok - a[i], msk | (1 << i), add + b[i]));
22+
}
23+
24+
return ret;
25+
}
26+
27+
int main() {
28+
scanf("%d %d %d", &n, &c ,&s);
29+
for(int i = 0; i < n; ++i)
30+
scanf("%d %d", a + i, b + i);
31+
32+
memset(dp, -1, sizeof dp);
33+
printf("%d\n", rec(0, 0, 0));
34+
35+
return 0;
36+
}

CS Academy/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020
- [79. Cats and Dogs](https://csacademy.com/contest/round-79/task/cats-and-dogs/)
2121
- [80. Digits Permutation](https://csacademy.com/contest/round-80/task/ioi-selection/)
2222
- [80. IOI Selection](https://csacademy.com/contest/round-80/task/digits-permutation/)
23+
- [82. City Break](https://csacademy.com/contest/round-82/task/city-break/)
24+
- [83. Smallest Missing Numbers](https://csacademy.com/contest/round-83/task/smallest-missing-numbers/)
25+
- [86. Cookie Clicker](https://csacademy.com/contest/round-86/task/cookie-clicker/)

SPOJ/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@
6262
- [SUMFOUR - 4 values whose sum is 0](http://www.spoj.com/problems/SUMFOUR/)
6363
- [SUMITR - Sums in a Triangle](http://www.spoj.com/problems/SUMITR/)
6464
- [TOANDFRO - To and Fro](http://www.spoj.com/problems/TOANDFRO/)
65+
- [TRIP - Trip](https://www.spoj.com/problems/TRIP/)
6566
- [WEIRDFN - Weird Function](https://www.spoj.com/problems/WEIRDFN/)

SPOJ/TRIP - Trip.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Idea:
3+
- https://www.geeksforgeeks.org/print-longest-common-sub-sequences-lexicographical-order/
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
int const N = 1e2 + 1;
11+
int t, len, l1, l2, dp[N][N];
12+
char s1[N], s2[N];
13+
string s;
14+
vector<string> sol;
15+
16+
int rec(int i, int j) {
17+
if(i == l1 || j == l2)
18+
return 0;
19+
20+
int &ret = dp[i][j];
21+
if(ret != -1)
22+
return ret;
23+
ret = 0;
24+
25+
if(s1[i] == s2[j])
26+
return ret = rec(i + 1, j + 1) + 1;
27+
ret = max(ret, rec(i + 1, j));
28+
ret = max(ret, rec(i, j + 1));
29+
30+
return ret;
31+
}
32+
33+
void print(int i, int j, int l) {
34+
if(l == len) {
35+
sol.push_back(s);
36+
return;
37+
}
38+
39+
if(i == l1 || j == l2)
40+
return;
41+
42+
for(char c = 'a'; c <= 'z'; ++c) {
43+
bool ok = false;
44+
for(int ii = i; ii < l1; ++ii) {
45+
if(s1[ii] == c) {
46+
for(int jj = j; jj < l2; ++jj) {
47+
if(s2[jj] == c && rec(ii, jj) == len - l) {
48+
s += c;
49+
print(ii + 1, jj + 1, l + 1);
50+
s.pop_back();
51+
ok = true;
52+
break;
53+
}
54+
}
55+
}
56+
if(ok)
57+
break;
58+
}
59+
}
60+
}
61+
62+
int main() {
63+
scanf("%d", &t);
64+
while(t-- != 0) {
65+
scanf("%s\n%s", s1, s2);
66+
l1 = strlen(s1);
67+
l2 = strlen(s2);
68+
69+
memset(dp, -1, sizeof dp);
70+
len = rec(0, 0);
71+
72+
s = "";
73+
sol.clear();
74+
print(0, 0, 0);
75+
76+
for(int i = 0; i < sol.size(); ++i)
77+
printf("%s\n", sol[i].c_str());
78+
79+
if(t)
80+
puts("");
81+
}
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)