竞选本题最史写法,5.43KB。场上拼尽全力调不出来,赛后借助了 DeepSeek 之力才调出来的。
首先考虑一个贪心策略:
我们认为最大值最大是优秀的,因为这样后面的糖葫芦就有更大的可能不需要合并。
假如有可以直接用的糖浆,那么就可以直接用这个糖浆制作。由于使用 更大的桶会让最大值减小,后面就会白白浪费次数,所以肯定优先使用 且 最小的桶。
假如不存在,我们希望最大值尽量大,而损耗都是 ,所以我们可以合并最大次大。如果还不够,就把第三大也合并进去,以此类推。
直接做的话,暴力找后继显然过不去。假如你用平衡树维护后继的话,可以做到 查询后继,但是复杂度不对。注意这里和下文说的后继都是非严格的。
为什么?考虑这样一个数据:初始有所有 ,第 次你需要制作 串长度为 的糖葫芦,你就会发现你每一次都需要考察所有的糖葫芦并且把它减一,这样就变成 了。
于是尝试优化。可以注意到,刚才我们其实都在对于同一个值的 做操作,容易想到去重,对于同一个 统一维护。那么这样做是不是就对了呢?是的,但是分类讨论和均摊分析都很复杂。于是尝试考察一些额外的性质,来简化代码。
我们可以注意到如下性质:由不止一桶糖浆合成的糖浆,只会有一桶。为什么?论证就是,假如合成了 两桶,令 ,那么从 中扔掉除了最初的那一桶以外的所有,然后合并到 上,这样一定是更优秀的。场上想到这一点的时候感觉很深刻啊。
所以我们只需要额外维护一个变量,代表用不止一桶糖浆合成出来的那个桶。那么我们在查询后继的时候,综合考虑合出来的桶以及原来就有的桶即可。
这极大的简化了分类讨论(虽然还是很史)。我们每一次找后继,直接从平衡树和额外的桶里面找到后继,然后分类讨论。
- 假如没有后继,那么就把最大值给合并到新的被融合的桶里面。假如这个桶的大小足以处理若干糖葫芦了,就处理若干糖葫芦,然后继续做。对于“处理”的步骤,可以在迭代过程中,使用后继来处理,所以在这里不赘述如何讨论。
- 假如有后继,分类讨论后继能否成功扣除完。
- 假如可以成功扣除完,那么每个数都需要减去一些值。设相等的后继一共 个,后继的值为 ,那么有 个 ,以及 个 。
- 否则,可以制作 个糖葫芦,所有的桶容量都变为 。
大概思路就是这些,实际上实现的时候需要的讨论还是多很多的,不多赘述了。接下来分析一下时间复杂度,显然需要分析的是平衡树操作次数。
对于每一次迭代,假如我们成功的做完了一批糖葫芦,那么平衡树中的节点数可能会增加 。这种操作只有 次,所以出现过的总节点数是 的。然后分析两种没有做完的情况。
第一种,是整体变为 的情况,在这里,每一次操作假设进行了 次,那么会合并 个节点,可以得到这一部分至多操作次数也是 的。第二种,是放到一个桶里。这种操作会使得平衡树里少一个节点,而每一轮最多一次不合并节点的操作,所以操作次数也至多是 的。可以得到,总共进行的平衡树操作是 大常数。
认为 同阶,时间复杂度 ,空间复杂度 。注意,平衡树由于节点会被拆开,所以平衡树中最多的节点数并不是 个,而是更多。开 能过,懒得分析了。注意多测清空。以及似乎存在一些莫名其妙的爆 long long 问题,所以我开了 __int128。最后常数巨大。
code
#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = (l); i <= (r); i ++ )
#define per(i, l, r) for(int i = (l); i >= (r); i -- )
#define int long long
#define endl '\n'
using namespace std;
const int N = 5e5 + 5;
using i128 = __int128;
i128 INF = ((i128)1 << 120);
int c, T, n, m;
i128 k, a[N], buc, ans;
namespace Splay {
struct node {
int fa, ch[2];
i128 val;
int siz, cnt;
void init(i128 _v, int _f) {
val = _v, fa = _f;
ch[0] = ch[1] = 0;
cnt = 1;
siz = 1;
}
} t[N];
int tot, root;
void pushup(int x) { t[x].siz = t[t[x].ch[0]].siz + t[t[x].ch[1]].siz + t[x].cnt; }
bool type(int x) { return t[t[x].fa].ch[1] == x; }
void clear() {
tot = root = 0;
t[0].fa = t[0].ch[0] = t[0].ch[1] = 0;
t[0].siz = t[0].cnt = 0;
}
void rotate(int x) {
int f = t[x].fa, g = t[f].fa;
bool ws = type(x);
int b = t[x].ch[ws ^ 1];
if (g) t[g].ch[type(f)] = x;
t[f].ch[ws] = b;
t[x].ch[ws ^ 1] = f;
t[x].fa = g;
t[f].fa = x;
if (b) t[b].fa = f;
pushup(f);
pushup(x);
}
void splay(int x) {
while(t[x].fa) {
if(t[t[x].fa].fa) {
if(type(t[x].fa) == type(x)) rotate(t[x].fa);
else rotate(x);
}
rotate(x);
}
root = x;
}
void insert(i128 x, int c) {
if (c == 0) return;
int u = root, f = 0;
while(u) {
f = u;
if(t[u].val == x) break;
u = t[u].ch[x > t[u].val];
}
if(u && t[u].val == x) {
t[u].cnt += c;
t[u].siz += c;
splay(u);
} else {
u = ++ tot;
if (f) t[f].ch[t[f].val < x] = u;
t[u].init(x, f);
t[u].cnt = t[u].siz = c;
splay(u);
}
}
pair<i128, int> pre(i128 x) {
int u = root, f = root;
i128 res = -INF;
int rec = 0;
while(u) {
if(t[u].val <= x) {
f = u;
res = t[u].val;
rec = t[u].cnt;
u = t[u].ch[1];
} else u = t[u].ch[0];
}
splay(f);
return make_pair(res, rec);
}
pair<i128, int> nex(i128 x) {
int u = root, f = root;
i128 res = INF;
int rec = 0;
while(u) {
if(t[u].val >= x) {
f = u;
res = t[u].val;
rec = t[u].cnt;
u = t[u].ch[0];
} else u = t[u].ch[1];
}
splay(f);
return make_pair(res, rec);
}
void del(i128 x, int c) {
if (c == 0) return;
int u = root;
while (u && t[u].val != x) u = t[u].ch[x > t[u].val];
if (!u) return;
splay(u);
if (t[u].cnt > c) {
t[u].cnt -= c;
pushup(u);
} else {
int L = t[u].ch[0], R = t[u].ch[1];
if (!L) {
root = R;
if (R) t[R].fa = 0;
} else if (!R) {
root = L;
t[L].fa = 0;
} else {
int p = L;
while (t[p].ch[1]) p = t[p].ch[1];
splay(p);
t[p].ch[1] = R;
t[R].fa = p;
pushup(p);
}
}
}
}
void print_i128(i128 x) {
if (x == 0) { cout << 0; return; }
bool neg = false;
if (x < 0) { neg = true; x = -x; }
string s;
while (x > 0) { s += '0' + (int)(x % 10); x /= 10; }
if (neg) s += '-';
reverse(s.begin(), s.end());
cout << s;
}
void solve() {
Splay::clear();
Splay::insert(INF, 1);
Splay::insert(-INF, 1);
buc = ans = 0;
long long tmp_k;
cin >> n >> m >> tmp_k;
k = tmp_k;
rep(i, 1, n) {
long long tmp_a;
cin >> tmp_a;
a[i] = tmp_a;
Splay::insert(a[i], 1);
}
bool ok = true;
rep(i, 1, m) {
long long tb, tc;
cin >> tb >> tc;
i128 b = tb, c = tc;
if (!ok) continue;
while(c > 0) {
pair<i128, int> s = Splay::nex(b);
bool hasS = (s.first != INF);
bool hasB = (buc >= b);
if (hasS || hasB) {
if (hasB && (!hasS || buc < s.first)) {
i128 use = c < (buc - b + 1) ? c : (buc - b + 1);
buc -= use;
c -= use;
} else {
i128 x = s.first;
int cnt = s.second;
i128 d = x - b + 1;
i128 total = (i128)cnt * d;
if (total >= c) {
int full = (int)(c / d);
i128 rem = c % d;
Splay::del(x, cnt);
if (b - 1 > 0 && full > 0) Splay::insert(b - 1, full);
if (rem > 0) {
i128 new_val = x - rem;
if (new_val > 0) Splay::insert(new_val, 1);
}
int remain = cnt - full - (rem > 0 ? 1 : 0);
if (remain > 0) Splay::insert(x, remain);
c = 0;
} else {
c -= total;
Splay::del(x, cnt);
if (b - 1 > 0) Splay::insert(b - 1, cnt);
}
}
continue;
}
if (buc > 0) {
Splay::insert(buc, 1);
buc = 0;
}
pair<i128, int> s1 = Splay::pre(INF - 1);
if (s1.first == -INF) { ok = false; break; }
i128 x1 = s1.first;
int cnt1 = s1.second;
i128 x2;
if (cnt1 >= 2) {
x2 = x1;
if (x1 + x2 <= k) { ok = false; break; }
Splay::del(x1, 2);
buc = x1 + x2 - k;
ans++;
} else {
Splay::del(x1, 1);
pair<i128, int> s2 = Splay::pre(INF - 1);
if (s2.first == -INF) { ok = false; break; }
x2 = s2.first;
if (x1 + x2 <= k) { ok = false; break; }
Splay::del(x2, 1);
buc = x1 + x2 - k;
ans++;
}
}
}
if (!ok) cout << -1 << endl;
else { print_i128(ans); cout << endl; }
}
signed main() {
ios :: sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> c >> T;
while(T -- ) solve();
return 0;
}