Idea: fcspartakm, preparation: fcspartakm
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
n, a, b = map(int, input().split())
c = n // 3
d = n % 3
print(min((c + 1) * b, c * b + d * a, n * a))
Idea: BledDest, preparation: Neon
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
s = input()
pref2 = 0
suf = 0
for x in s:
if x == '1' or x == '3':
suf += 1
ans = pref2 + suf
for x in s:
if x == '2':
pref2 += 1
if x == '1' or x == '3':
suf -= 1
ans = max(ans, pref2 + suf)
print(len(s) - ans)
2230C - Arrange the Numbers in a Circle
Idea: BledDest, preparation: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
n = int(input())
c = list(map(int, input().split()))
s = sum(c)
ones = 0
slots = 0
for x in c:
if x == 1:
ones += 1
else:
slots += x // 2 - 1
if ones == n - 1:
slots += 1
wasted = max(0, ones - slots)
if s - wasted < 3:
print(0)
else:
print(s - wasted)
Tutorial
Tutorial is loading...
Solution (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto &x : a) cin >> x, --x;
for (auto &x : b) cin >> x, --x;
vector<int> pa(n + 1, n), pb(n + 1, n), dp(n + 1, n);
long long ans = 0;
for (int i = n - 1; i >= 0; --i) {
pa[a[i]] = i;
pb[b[i]] = i;
if (a[i] == b[i]) {
int x = a[i] + 1;
if (pa[x] == pb[x]) {
dp[i] = dp[pa[x]];
} else {
dp[i] = min(pa[x], pb[x]);
}
}
if (pa[0] != pb[0]) {
ans += min(pa[0], pb[0]) - i;
} else {
ans += dp[pa[0]] - i;
}
}
cout << ans << '\n';
}
}
Idea: adedalic, preparation: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
import bisect
import sys
input = sys.stdin.readline
INF = int(1e9) + 7
A = int(1e6) + 3
n = int(input())
a = [list(map(int, input().split())) for i in range(2)]
minVal = [[INF] * A for i in range(2)]
pMin = [[INF] * A for i in range(2)]
sMin = [[INF] * A for i in range(2)]
for k in range(2):
for i in range(n):
minVal[k][a[k][i]] = min(minVal[k][a[k][i]], a[k ^ 1][i])
for v in range(A - 1):
pMin[k][v + 1] = min(pMin[k][v], minVal[k][v])
for v in range(A - 2, -1, -1):
sMin[k][v] = min(sMin[k][v + 1], minVal[k][v])
mid = 0
for i in range(n):
if a[0][mid] + a[1][mid] > a[0][i] + a[1][i]:
mid = i
m = int(input())
t = [list(map(int, input().split())) for i in range(3)]
def I(val, l, r):
if val < l:
return 0
return min(val, r)
ans = []
for id in range(m):
cur = I(a[0][mid], t[0][id], t[0][id] + t[2][id]) + I(a[1][mid], t[1][id], t[1][id] + t[2][id])
for k in range(2):
pos_l = t[k][id]
pos_r = min(A - 1, t[k][id] + t[2][id])
if pMin[k][pos_l] < INF:
cur = min(cur, I(pMin[k][pos_l], t[k ^ 1][id], t[k ^ 1][id] + t[2][id]))
if sMin[k][pos_r] < INF:
cur = min(cur, pos_r + I(sMin[k][pos_r], t[k ^ 1][id], t[k ^ 1][id] + t[2][id]))
ans.append(cur)
print("\n".join(map(str, ans)))
Idea: BledDest, preparation: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
#include<bits/stdc++.h>
using namespace std;
const int N = 200043;
const int K = 3;
const int LN = 20;
vector<int> g[N];
int p[N];
pair<int, int> dp_down[N][K];
int dp_up[N];
int q_index;
int ans[N];
int f[N];
void upd_down(int idx, pair<int, int> p)
{
if(dp_down[idx][K - 1] < p)
{
dp_down[idx][K - 1] = p;
int j = K - 1;
while(j != 0)
{
if(dp_down[idx][j - 1] < dp_down[idx][j])
{
swap(dp_down[idx][j - 1], dp_down[idx][j]);
j--;
}
else break;
}
}
}
void upd(pair<int, int>& p, int x)
{
if(p.second < x) p.second = x;
if(p.first < p.second) swap(p.first, p.second);
}
int get_cur(int x)
{
pair<int, int> cur_ans = {0, 0};
for(int i = 0; i < K; i++)
if(dp_down[x][i].second != f[x])
upd(cur_ans, dp_down[x][i].first);
if(f[x] != -1)
upd(cur_ans, dp_up[x]);
return cur_ans.second + 1;
}
int get_full(int x)
{
pair<int, int> cur_ans = {0, 0};
for(int i = 0; i < K; i++)
upd(cur_ans, dp_down[x][i].first);
upd(cur_ans, dp_up[x]);
return cur_ans.second + 1;
}
void dfs1(int x, int parent = -1)
{
p[x] = parent;
for(auto y : g[x])
{
if(y > q_index) continue;
if(y != parent)
{
dfs1(y, x);
upd_down(x, make_pair(get_cur(y), y));
}
}
}
void dfs2(int x)
{
if(p[x] != -1)
dp_up[x] = get_cur(p[x]);
for(auto y : g[x])
{
if(y > q_index) continue;
if(y != p[x])
{
f[x] = y;
dfs2(y);
f[x] = -1;
}
}
}
int get_ans(int x)
{
q_index = x + 2;
for(int i = 1; i <= q_index; i++)
{
dp_up[i] = 0;
for(int j = 0; j < K; j++)
dp_down[i][j] = {0, -1};
p[i] = -1;
f[i] = -1;
}
dfs1(1);
dfs2(1);
int ans = 1;
for(int i = 1; i <= q_index; i++)
ans = max(ans, get_full(i));
return ans;
}
void rec(int l, int r, int L, int R)
{
if(l >= r) return;
if(L == R)
{
for(int i = l; i < r; i++)
ans[i] = L;
return;
}
int mid = (l + r) / 2;
ans[mid] = get_ans(mid);
rec(l, mid, L, ans[mid]);
rec(mid + 1, r, ans[mid], R);
}
void solve()
{
int q;
cin >> q;
for(int i = 0; i < q; i++)
{
int x;
cin >> x;
g[x].push_back(i + 2);
g[i + 2].push_back(x);
}
rec(0, q, 1, LN);
for(int i = 0; i < q; i++)
cout << ans[i] << " ";
cout << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
for(int i = 0; i < t; i++)
solve();
return 0;
}










