Skip to content

Commit 07e2ee5

Browse files
committed
Solve alot of problems :3
1 parent 346fc81 commit 07e2ee5

14 files changed

+774
-3
lines changed

CodeForces/706C. Hard problem.cpp

Lines changed: 60 additions & 0 deletions
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 = 1e5 + 1;
6+
int n, a[N];
7+
long long dp[N][2];
8+
vector<string> s, sr;
9+
10+
long long rec(int idx, bool rv) {
11+
if(idx == n)
12+
return 0;
13+
14+
long long &ret = dp[idx][rv];
15+
if(ret != -1)
16+
return ret;
17+
ret = 2e18;
18+
19+
if(rv) {
20+
if(s[idx] >= sr[idx - 1])
21+
ret = min(ret, rec(idx + 1, 0));
22+
23+
if(sr[idx] >= sr[idx - 1])
24+
ret = min(ret, rec(idx + 1, 1) + a[idx]);
25+
26+
return ret;
27+
}
28+
29+
if(s[idx] >= s[idx - 1])
30+
ret = min(ret, rec(idx + 1, 0));
31+
32+
if(sr[idx] >= s[idx - 1])
33+
ret = min(ret, rec(idx + 1, 1) + a[idx]);
34+
35+
return ret;
36+
}
37+
38+
int main() {
39+
cin >> n;
40+
for(int i = 0; i < n; ++i)
41+
cin >> a[i];
42+
43+
string tmp;
44+
for(int i = 0; i < n; ++i) {
45+
cin >> tmp;
46+
s.push_back(tmp);
47+
reverse(tmp.begin(), tmp.end());
48+
sr.push_back(tmp);
49+
}
50+
51+
memset(dp, -1, sizeof dp);
52+
long long res = min(rec(1, 0), rec(1, 1) + a[0]);
53+
54+
if(res >= 2e18)
55+
cout << -1 << endl;
56+
else
57+
cout << res << endl;
58+
59+
return 0;
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main() {
6+
int n;
7+
cin >> n;
8+
9+
vector<string> all;
10+
11+
bool ok = false;
12+
while(n-- != 0) {
13+
string s;
14+
cin >> s;
15+
16+
if(!ok && s[0] == 'O' && s[1] == 'O') {
17+
s[0] = s[1] = '+';
18+
ok = true;
19+
} else if(!ok && s[3] == 'O' && s[4] == 'O') {
20+
s[3] = s[4] = '+';
21+
ok = true;
22+
}
23+
24+
all.push_back(s);
25+
}
26+
27+
if(ok) {
28+
cout << "YES" << endl;
29+
for(int i = 0; i < all.size(); ++i)
30+
cout << all[i] << endl;
31+
} else
32+
cout << "NO" << endl;
33+
34+
return 0;
35+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n;
6+
long long g[501][501], rows[501], cols[501], md, sd, x, y;
7+
8+
int main() {
9+
cin >> n;
10+
11+
if(n == 1) {
12+
cout << 1 << endl;
13+
return 0;
14+
}
15+
16+
for(int i = 0; i < n; ++i) {
17+
for(int j = 0; j < n; ++j) {
18+
cin >> g[i][j];
19+
20+
if(g[i][j] == 0)
21+
x = i, y = j;
22+
23+
rows[i] += g[i][j];
24+
cols[j] += g[i][j];
25+
if(i == j)
26+
md += g[i][j];
27+
if(i + j + 1 == n)
28+
sd += g[i][j];
29+
}
30+
}
31+
32+
long long sum = 0;
33+
for(int i = 0; i < n; ++i)
34+
if(x != i) {
35+
sum = rows[i];
36+
break;
37+
}
38+
39+
if(x != y && md != sum) {
40+
cout << -1 << endl;
41+
return 0;
42+
}
43+
44+
if(x + y + 1 != n && sd != sum) {
45+
cout << -1 << endl;
46+
return 0;
47+
}
48+
49+
for(int i = 0; i < n; ++i) {
50+
if(i != x && rows[i] != sum) {
51+
cout << -1 << endl;
52+
return 0;
53+
}
54+
55+
if(i != y && cols[i] != sum) {
56+
cout << -1 << endl;
57+
return 0;
58+
}
59+
}
60+
61+
g[x][y] = sum - rows[x];
62+
63+
if(g[x][y] <= 0 || cols[y] + g[x][y] != sum) {
64+
cout << -1 << endl;
65+
return 0;
66+
}
67+
68+
if(x == y && md + g[x][y] != sum) {
69+
cout << -1 << endl;
70+
return 0;
71+
}
72+
73+
if(x + y + 1 == n && sd + g[x][y] != sum) {
74+
cout << -1 << endl;
75+
return 0;
76+
}
77+
78+
cout << g[x][y] << endl;
79+
80+
return 0;
81+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, m, k, a[101], p[101][101];
6+
long long dp[101][105][101];
7+
8+
long long rec(int idx, int lst, int cur) {
9+
if(idx == n) {
10+
if(cur == k)
11+
return 0;
12+
return 2e14;
13+
}
14+
15+
long long &ret = dp[idx][lst + 1][cur];
16+
if(ret != -1)
17+
return ret;
18+
ret = 2e14;
19+
20+
if(idx == 0) {
21+
if(a[idx] != -1)
22+
return ret = rec(idx + 1, a[idx], cur + 1);
23+
24+
for(int i = 0; i < m; ++i)
25+
ret = min(ret, rec(idx + 1, i, cur + 1) + p[idx][i]);
26+
27+
return ret;
28+
}
29+
30+
if(a[idx] != -1)
31+
return ret = rec(idx + 1, a[idx], cur + (lst != a[idx]));
32+
33+
for(int i = 0; i < m; ++i)
34+
ret = min(ret, rec(idx + 1, i, cur + (lst != i)) + p[idx][i]);
35+
36+
return ret;
37+
}
38+
39+
void build_path(int idx, int lst, int cur) {
40+
if(idx == n) {
41+
return;
42+
}
43+
44+
long long opt = rec(idx, lst, cur);
45+
46+
if(idx == 0) {
47+
if(a[idx] != -1) {
48+
build_path(idx + 1, a[idx], cur + 1);
49+
return;
50+
}
51+
52+
for(int i = 0; i < m; ++i) {
53+
if(rec(idx + 1, i, cur + 1) + p[idx][i] == opt) {
54+
cout << i +1 << ' ';
55+
build_path(idx + 1, i, cur + 1);
56+
return;
57+
}
58+
}
59+
}
60+
61+
if(a[idx] != -1) {
62+
cout << a[idx] + 1 << ' ';
63+
build_path(idx + 1, a[idx], cur + (lst != a[idx]));
64+
return;
65+
}
66+
67+
for(int i = 0; i < m; ++i) {
68+
if(opt == rec(idx + 1, i, cur + (lst != i)) + p[idx][i]) {
69+
cout << i+ 1 << ' ';
70+
build_path(idx + 1, i, cur + (lst != i));
71+
return;
72+
}
73+
}
74+
}
75+
76+
int main() {
77+
cin >> n >> m >> k;
78+
int zeros = 0;
79+
for(int i = 0; i < n; ++i) {
80+
cin >> a[i];
81+
zeros += (a[i] == 0);
82+
}
83+
84+
for(int i = 0; i < n; ++i)
85+
for(int j = 0; j < m; ++j)
86+
cin >> p[i][j];
87+
88+
for(int i = 0; i < n; ++i)
89+
--a[i];
90+
91+
memset(dp, -1, sizeof dp);
92+
long long ans = rec(0, a[0], 0);
93+
94+
if(ans >= 2e14)
95+
cout << -1 << endl;
96+
else
97+
cout << ans << endl;
98+
99+
return 0;
100+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 2e5 + 1, M = 1e9 + 7;
6+
int n, low[N], idx[N], comp[N], sol[N], dfs, id;
7+
bool in[N];
8+
vector<int> st;
9+
vector<vector<int> > g;
10+
11+
void DFS(int v) {
12+
low[v] = idx[v] = dfs++;
13+
in[v] = true;
14+
st.push_back(v);
15+
16+
for(int i = 0; i < g[v].size(); i++)
17+
if(idx[g[v][i]] == -1) {
18+
DFS(g[v][i]);
19+
low[v] = min(low[v], low[g[v][i]]);
20+
} else if(in[g[v][i]])
21+
low[v] = min(low[v], low[g[v][i]]);
22+
23+
if(low[v] == idx[v]) {
24+
int node;
25+
do {
26+
node = st.back();
27+
in[node] = false;
28+
st.pop_back();
29+
++comp[id];
30+
} while(node != v);
31+
32+
id++;
33+
}
34+
}
35+
36+
int fst(int base, int power) {
37+
int ret = 1;
38+
39+
while(power > 0) {
40+
if(power % 2 == 0) {
41+
power /= 2;
42+
base = 1ll * base * base % M;
43+
} else {
44+
--power;
45+
ret = 1ll * ret * base % M;
46+
47+
power /= 2;
48+
base = 1ll * base * base % M;
49+
}
50+
}
51+
52+
return ret;
53+
}
54+
55+
int main() {
56+
cin >> n;
57+
g.resize(n);
58+
for(int i = 0; i < n; ++i) {
59+
int tmp;
60+
cin >> tmp;
61+
g[i].push_back(--tmp);
62+
}
63+
64+
memset(idx, -1, sizeof idx);
65+
for(int i = 0; i < n; ++i)
66+
if(idx[i] == -1)
67+
DFS(i);
68+
69+
for(int i = 0; i < id; ++i)
70+
sol[i] = (fst(2, comp[i]) - (comp[i] == 1 ? 0 : 2) + M) % M;
71+
72+
long long res = 1;
73+
for(int i = 0; i < id; ++i)
74+
res = res * sol[i] % M;
75+
76+
cout << res << endl;
77+
78+
return 0;
79+
}

0 commit comments

Comments
 (0)