[ARC210] 做题记录
_MengShang_ · · 个人记录
ARC210
前言:这场没有
ARC210A Always Increasing
感觉我的做法有点糖。
考虑先固定初始序列
每次修改后,判断修改后是否满足
注意到需要支持单点加,区间加,单点查,可以用
时间复杂度
const int maxn = 2e5 + 5;
int n, q; ll a[maxn], ans;
ll v[maxn];
inline void add(int x, int d) { for (; x <= n; x+= x & -x) v[x]+= d; }
inline ll qry(int x) { ll res = 0; for (; x; x-= x & -x) res+= v[x]; return res; }
int main()
{
read(n, q); rep(i, 1, n) a[i] = i, add(i, 1), ans+= i;
rep(i, 1, q)
{
int x, y; read(x, y); add(x, y); add(x + 1, -y);
if (x == n || qry(x) < qry(x + 1)) continue;
ll tmp = qry(x) + 1 - qry(x + 1); ans+= tmp * (n - x); add(x + 1, tmp);
}
write(ans); pc(0xa);
return 0;
}
Update:
看了 官方题解 的做法,是
ARC210B Remove Median Operations
考虑可重集合
考虑每次取中位数时,
|X|=n+1=k+1+k ,那么结论显然了。
于是考虑开
每次修改时,移动的元素是
细节见代码。
const int maxn = 2e5 + 5;
int n, m, q, a[maxn], b[maxn], c[maxn << 1], k; ll ans;
multiset<int> st1, st2, st3;
inline void change(int x, int y)
{
if (st1.find(x) != st1.end()) st1.erase(st1.find(x)), ans-= x;
else if (st2.find(x) != st2.end()) st2.erase(st2.find(x));
else st3.erase(st3.find(x)), ans-= x;
if (st2.size() && y <= *st2.begin()) st1.insert(y), ans+= y;
else if (st2.size() && y >= *prev(st2.end())) st3.insert(y), ans+= y;
else st2.insert(y);
while (st1.size() < n / 2) { auto tmp = st2.begin(); ans+= *tmp; st1.insert(*tmp); st2.erase(tmp); }
while (st1.size() > n / 2) { auto tmp = prev(st1.end()); ans-= *tmp; st2.insert(*tmp); st1.erase(tmp); }
while (st3.size() < n / 2) { auto tmp = prev(st2.end()); ans+= *tmp; st3.insert(*tmp); st2.erase(tmp); }
while (st3.size() > n / 2) { auto tmp = st3.begin(); ans-= *tmp; st2.insert(*tmp); st3.erase(tmp); }
}
int main()
{
read(n, m, q);
rep(i, 1, n) read(a[i]), c[++k] = a[i];
rep(i, 1, m) read(b[i]), c[++k] = b[i];
sort(c + 1, c + k + 1);
rep(i, 1, n / 2) st1.insert(c[i]), ans+= c[i];
rep(i, n / 2 + 1, n / 2 + m) st2.insert(c[i]);
rep(i, n / 2 + m + 1, n + m) st3.insert(c[i]), ans+= c[i];
rep(i, 1, q)
{
int t, x, y; read(t, x, y);
if (t == 1) { change(a[x], y); a[x] = y; }
else { change(b[x], y); b[x] = y; }
write(ans); pc(0xa);
}
return 0;
}
ARC210C Fair Coin Partition
考虑极限情况下,每个
几个贪心想法:
-
从高位开始贪,尽量使高位的数大。
-
分硬币时,尽量用面值大的去分,因为面值大的在低位会用不了。
于是就容易做了。细节见代码。
const int maxn = 2e5 + 105;
int n, m, N; ll a[maxn], abl[maxn], ans[maxn], cst;
inline void work()
{
read(n, m); N = n + 10;
rep(i, 1, N) a[i] = abl[i] = ans[i] = 0; cst = 0;
rep(i, 1, n) read(a[i]);
rep(i, 1, N) abl[i] = abl[i - 1] / 10 + a[i];
fep(i, N, 1)
{
ll k1 = min(a[i], cst * 10); a[i]-= k1; abl[i]-= cst * 10; cst = cst * 10 - k1;
ll k2 = abl[i] / m; ans[i] = k2;
cst+= max(0ll, k2 * m - a[i]);
}
int p = N; while (p && !ans[p]) --p; if (!p) return puts("0"), void();
fep(i, p, 1) write(ans[i]); pc(0xa);
}
int main() { int _; read(_); while (_--) work(); return 0; }