Idea: BledDest, preparation: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
n = int(input())
if n % 7 == 0:
print(n)
else:
ans = -1
for j in range(10):
if (n - n % 10 + j) % 7 == 0:
ans = n - n % 10 + j
print(ans)
Idea: BledDest, preparation: awoo and Neon
Tutorial
Tutorial is loading...
Solution (awoo)
for _ in range(int(input())):
s = input()
print(min((len(s) - 1) // 2, s.count('0'), s.count('1')))
Idea: BledDest, preparation: Neon
Tutorial
Tutorial is loading...
Solution (awoo)
for _ in range(int(input())):
hc, dc = map(int, input().split())
hm, dm = map(int, input().split())
k, w, a = map(int, input().split())
for i in range(k + 1):
nhc = hc + i * a
ndc = dc + (k - i) * w
if (hm + ndc - 1) // ndc <= (nhc + dm - 1) // dm:
print("YES")
break
else:
print("NO")
Idea: BledDest, preparation: Neon
Tutorial
Tutorial is loading...
Solution (Neon)
#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
int main() {
vector<int> d(N, N);
d[1] = 0;
for (int i = 1; i < N; ++i) {
for (int x = 1; x <= i; ++x) {
int j = i + i / x;
if (j < N) d[j] = min(d[j], d[i] + 1);
}
}
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> b(n), c(n);
for (int &x : b) cin >> x;
for (int &x : c) cin >> x;
int sum = 0;
for (int x : b) sum += d[x];
k = min(k, sum);
vector<int> dp(k + 1, 0);
for (int i = 0; i < n; ++i) {
for (int j = k - d[b[i]]; j >= 0; j--) {
dp[j + d[b[i]]] = max(dp[j + d[b[i]]], dp[j] + c[i]);
}
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
}
}
Idea: BledDest, preparation: awoo
Tutorial
Tutorial is loading...
Solution (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
struct edge{
int v, u, w;
};
vector<int> pr, rk;
int getp(int a){
return a == pr[a] ? a : pr[a] = getp(pr[a]);
}
bool unite(int a, int b){
a = getp(a), b = getp(b);
if (a == b) return false;
if (rk[a] < rk[b]) swap(a, b);
rk[a] += rk[b];
pr[b] = a;
return true;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
pr.resize(n);
rk.resize(n);
vector<edge> es(m);
forn(i, m){
scanf("%d%d%d", &es[i].v, &es[i].u, &es[i].w);
--es[i].v, --es[i].u;
es[i].w *= 2;
}
vector<int> ev(1, 0);
forn(i, m) forn(j, i + 1) ev.push_back((es[i].w + es[j].w) / 2);
sort(ev.begin(), ev.end());
ev.resize(unique(ev.begin(), ev.end()) - ev.begin());
vector<long long> base;
vector<int> cnt;
for (int x : ev){
sort(es.begin(), es.end(), [&x](const edge &a, const edge &b){
int wa = abs(a.w - x);
int wb = abs(b.w - x);
if (wa != wb) return wa < wb;
return a.w > b.w;
});
forn(i, n) pr[i] = i, rk[i] = 1;
long long cur_base = 0;
int cur_cnt = 0;
for (const auto &e : es) if (unite(e.v, e.u)){
cur_base += abs(e.w - x);
cur_cnt += x < e.w;
}
base.push_back(cur_base);
cnt.push_back(cur_cnt);
}
int p, k, a, b, c;
scanf("%d%d%d%d%d", &p, &k, &a, &b, &c);
int x = 0;
long long ans = 0;
forn(q, k){
if (q < p) scanf("%d", &x);
else x = (x * 1ll * a + b) % c;
int y = upper_bound(ev.begin(), ev.end(), 2 * x) - ev.begin() - 1;
ans ^= (base[y] + (n - 1 - 2 * cnt[y]) * 1ll * (2 * x - ev[y])) / 2;
}
printf("%lld\n", ans);
return 0;
}
Idea: BledDest, preparation: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, int> val;
#define x first
#define y second
const int N = 200043;
val operator +(const val& a, const val& b)
{
return make_pair(a.x + b.x, a.y + b.y);
}
val operator -(const val& a, const val& b)
{
return make_pair(a.x - b.x, a.y - b.y);
}
val T[4 * N];
val Tfull[4 * N];
int f[4 * N];
val getVal(int v)
{
if(f[v] == 1)
return Tfull[v] - T[v];
return T[v];
}
void push(int v)
{
if(f[v] == 1)
{
f[v] = 0;
T[v] = Tfull[v] - T[v];
f[v * 2 + 1] ^= 1;
f[v * 2 + 2] ^= 1;
}
}
void upd(int v)
{
Tfull[v] = Tfull[v * 2 + 1] + Tfull[v * 2 + 2];
T[v] = getVal(v * 2 + 1) + getVal(v * 2 + 2);
}
void setVal(int v, int l, int r, int pos, val cur)
{
if(l == r - 1)
{
f[v] = 0;
Tfull[v] = cur;
T[v] = cur;
}
else
{
push(v);
int m = (l + r) / 2;
if(pos < m)
setVal(v * 2 + 1, l, m, pos, cur);
else
setVal(v * 2 + 2, m, r, pos, cur);
upd(v);
}
}
void flipColor(int v, int l, int r, int L, int R)
{
if(L >= R) return;
if(l == L && r == R)
f[v] ^= 1;
else
{
push(v);
int m = (l + r) / 2;
flipColor(v * 2 + 1, l, m, L, min(m, R));
flipColor(v * 2 + 2, m, r, max(L, m), R);
upd(v);
}
}
val getVal(int v, int l, int r, int L, int R)
{
if(L >= R) return make_pair(0ll, 0);
if(l == L && r == R) return getVal(v);
int m = (l + r) / 2;
val ans = make_pair(0ll, 0);
push(v);
ans = ans + getVal(v * 2 + 1, l, m, L, min(R, m));
ans = ans + getVal(v * 2 + 2, m, r, max(L, m), R);
upd(v);
return ans;
}
int n;
vector<int> g[N];
int p[N], siz[N], d[N], nxt[N];
int tin[N], timer;
map<pair<int, int>, int> idx;
long long sum;
int cnt;
int active[N];
int active_cnt;
void dfs_sz(int v)
{
if (p[v] != -1)
{
auto it = find(g[v].begin(), g[v].end(), p[v]);
if (it != g[v].end())
g[v].erase(it);
}
siz[v] = 1;
for (int &u : g[v])
{
p[u] = v;
d[u] = d[v] + 1;
dfs_sz(u);
siz[v] += siz[u];
if (siz[u] > siz[g[v][0]])
swap(u, g[v][0]);
}
}
void dfs_hld(int v)
{
tin[v] = timer++;
for (int u : g[v])
{
nxt[u] = (u == g[v][0] ? nxt[v] : u);
dfs_hld(u);
}
}
// [l; r] inclusive
void flipSegment(int l, int r)
{
flipColor(0, 0, n, l, r + 1);
}
// [l; r] inclusive
val get(int l, int r)
{
return getVal(0, 0, n, l, r + 1);
}
void flipPath(int v, int u)
{
for (; nxt[v] != nxt[u]; u = p[nxt[u]])
{
if (d[nxt[v]] > d[nxt[u]]) swap(v, u);
flipSegment(tin[nxt[u]], tin[u]);
}
if (d[v] > d[u]) swap(v, u);
flipSegment(tin[v], tin[u]);
}
val getPath(int v, int u)
{
val res = make_pair(0ll, 0);
for (; nxt[v] != nxt[u]; u = p[nxt[u]])
{
if (d[nxt[v]] > d[nxt[u]]) swap(v, u);
// update res with the result of get()
res = res + get(tin[nxt[u]], tin[u]);
}
if (d[v] > d[u]) swap(v, u);
res = res + get(tin[v], tin[u]);
return res;
}
void activate_vertex(int x)
{
int id = 0;
if(p[x] != -1)
{
id = idx[make_pair(x, p[x])];
val v = getPath(0, p[x]);
flipPath(0, p[x]);
sum -= v.x;
cnt -= v.y;
v = getPath(0, p[x]);
sum += v.x;
cnt += v.y;
}
cnt++;
sum += id;
setVal(0, 0, n, tin[x], make_pair((long long)(id), 1));
active[x] = 1;
active_cnt++;
}
void init_hld(int root = 0)
{
d[root] = 0;
nxt[root] = root;
p[root] = -1;
timer = 0;
dfs_sz(root);
dfs_hld(root);
}
int currentSize[N];
int dfsSolution(int x, vector<int>& edges)
{
if(!active[x]) return 0;
currentSize[x] = 1;
for(auto y : g[x])
if(y != p[x])
currentSize[x] += dfsSolution(y, edges);
if(currentSize[x] % 2 == 1)
edges.push_back(idx[make_pair(x, p[x])]);
return currentSize[x];
}
void outputSolution()
{
vector<int> edges;
if(cnt * 2 == active_cnt)
{
dfsSolution(0, edges);
sort(edges.begin(), edges.end());
}
printf("%d", int(edges.size()));
for(auto x : edges) printf(" %d", x);
puts("");
fflush(stdout);
}
void processQuery(int v)
{
activate_vertex(v);
if(cnt * 2 == active_cnt)
printf("%lld\n", sum);
else
puts("0");
fflush(stdout);
}
int main()
{
scanf("%d", &n);
for(int i = 1; i < n; i++)
{
int x, y;
scanf("%d %d", &x, &y);
--x;
--y;
g[x].push_back(y);
g[y].push_back(x);
idx[make_pair(x, y)] = i;
idx[make_pair(y, x)] = i;
}
init_hld();
activate_vertex(0);
while(true)
{
int t;
scanf("%d", &t);
if(t == 3)
break;
if(t == 2)
outputSolution();
if(t == 1)
{
int v;
scanf("%d", &v);
--v;
processQuery(v);
}
}
}