f
D. 长江灯会(lantern)
题目:https://oj.chocola.work/p/214(管理员可见)。标准程序默认读写 lantern.in、lantern.out;定义 LOCAL 后使用标准输入输出。
题意与突破口
环上
令
使用那个
因此,完整块结束在首个满足
的位置。因为每一步最少只下降
从右向左扫描,用数组记录每个整数余额值最近出现的位置,即可在
完整块的最终位置与使用新出现较大按钮的具体顺序无关:重复执行“把已亮较大按钮的贡献加到末端”得到的是唯一的单调闭包。贪心提供了其中一种执行顺序。
倍增多个完整块
预处理从位置
go[v][j][r]:结束位置。need[v][j][r]:开始时至少需要多少个数值为v 的按钮,才能保证每个块开始时都有一个可用的v 按钮。
单块的需求是
前一项是新获得的按钮数,后一项是半段完整块消耗的按钮数。于是
跳转位置照常复合。
不需要额外存储操作次数。设跳转结束位置为
因为每个块使用一个
回答询问
依次处理
从大到小尝试倍增跳转。一次跳转只有同时满足以下条件才执行:
- 结束位置严格小于本次终点
E 。 - 库存不少于
need。 - 剩余操作次数不少于
cost。
跳转后,较大按钮库存仍为
若仍有一个
- 只先按一次
v ,把末端增加v 并截到E 。 - 对新解锁的更大按钮递归执行同样的分层过程。
- 这次递归只处理
v+1,\ldots,4 ,层数严格增加,深度最多为4 。
这能精确停在完整块的中间。不能把剩余预算简单乘以
如果当前层库存已经为
正确性证明
最大按钮贪心具有交换证明,因此只需正确加速该贪心过程。
在第 cost 和库存变化均准确。
need 的合并式分别保证前半段能够执行,以及执行完前半段后库存足够执行后半段,所以倍增块不会在中途缺少按钮。预算和终点检查保证整段跳转不会越过本次允许的停止位置。
无法整段跳过最后一个完整块时,先按其首个
故算法与逐次最大值贪心的最终结果一致,也就是题目要求的最优结果。
复杂度与内存
设按钮数值上界
- 预处理:
O(Vn\log n) 。 - 每次询问:
O(V^3\log n) ;递归中至多有O(V^2) 个层处理,每次跳转还需更新至多V 种库存。由于V=4 固定,也就是常数较小的O(\log n) 。 - 空间:
O(Vn\log n) 。
程序存储三层跳转表、四层需求表,共七张倍增表。
部分分与核验
-
-
- 只有一种非零值:只需处理对应一层。
-
- 满分使用四层闭包与倍增。
25 组正式数据全部通过。另对长度不超过
C++17 标准程序
#include <bits/stdc++.h>
using namespace std;
struct FastIO {
static constexpr int S = 1 << 20;
char in[S], out[S]; int at=0, len=0, used=0;
char get() {
if (at==len) len=(int)fread(in,1,S,stdin),at=0;
return at<len ? in[at++] : 0;
}
int read() {
char c; do c=get(); while(c<=' '&&c);
int x=0; while(c>='0'&&c<='9') x=x*10+c-'0',c=get();
return x;
}
void write(int x) {
char s[16];int k=0;
do s[k++]=char('0'+x%10),x/=10;while(x);
if(used+k+1>=S) flush();
while(k)out[used++]=s[--k];
out[used++]='\n';
}
void flush(){fwrite(out,1,used,stdout);used=0;}
~FastIO(){flush();}
}io;
class Solver {
int n, N, size, LOG;
array<vector<int>,6> pref;
array<vector<int>,5> go, need;
int exact(int v,int x)const{return pref[v][x]-pref[v+1][x];}
int next(int v,int j,int x)const {
if(v==4)return min(N,x+4*(1<<j));
return go[v][(size_t)j*size+x];
}
int end, pos, budget;
array<int,5> available;
void moveTo(int target) {
for(int v=1;v<=4;++v)
available[v]+=exact(v,target)-exact(v,pos);
pos=target;
}
void advance(int low) {
for(int v=4;v>=low && pos<end && budget;--v) {
if(!available[v])continue;
for(int j=LOG-1;j>=0;--j) {
int to=next(v,j,pos);
if(to>=end)continue;
int blocks=1<<j;
int cost=blocks+pref[v+1][to]-pref[v+1][pos];
if(cost>budget || available[v]<need[v][(size_t)j*size+pos])continue;
// All buttons larger than v are exhausted at both boundaries.
available[v]-=blocks;
for(int w=1;w<=v;++w)
available[w]+=exact(w,to)-exact(w,pos);
pos=to;
budget-=cost;
}
if(available[v] && budget && pos<end) {
// The next complete block reaches the query end or exceeds its budget.
--available[v];--budget;
moveTo(min(end,pos+v));
if(budget && pos<end)advance(v+1);
return;
}
}
}
public:
explicit Solver(vector<int> a):n((int)a.size()-1),N(2*n),size(N+1),LOG(1) {
while((1<<LOG)<=n)++LOG;
for(int v=1;v<=5;++v)pref[v].assign(size,0);
for(int i=1;i<=N;++i)
for(int v=1;v<=4;++v)
pref[v][i]=pref[v][i-1]+(a[(i-1)%n+1]>=v);
vector<int> balance(size), nearest(4*N+16);
for(int v=1;v<=4;++v) {
need[v].assign((size_t)LOG*size,1);
if(v<4) {
go[v].resize((size_t)LOG*size);
balance[0]=0;
for(int i=1;i<=N;++i) {
int x=a[(i-1)%n+1];
balance[i]=balance[i-1]+(x>v?x:0)-1;
}
fill(nearest.begin(),nearest.end(),N);
int offset=N+8;
for(int x=N;x>=0;--x) {
go[v][x]=nearest[balance[x]-v+offset];
nearest[balance[x]+offset]=x;
}
}
for(int j=1;j<LOG;++j) {
int half=1<<(j-1);
size_t old=(size_t)(j-1)*size, now=(size_t)j*size;
for(int x=0;x<=N;++x) {
int mid=next(v,j-1,x);
if(v<4)go[v][now+x]=go[v][old+mid];
int delta=exact(v,mid)-exact(v,x)-half;
need[v][now+x]=max(need[v][old+x],need[v][old+mid]-delta);
}
}
}
}
int query(int s,int k,int p) {
end=s+n-1;pos=s+k-1;budget=p;
available.fill(0);
for(int v=1;v<=4;++v)
available[v]=exact(v,pos)-exact(v,s-1);
advance(1);
return end-pos;
}
};
int main() {
#ifndef LOCAL
freopen("lantern.in","r",stdin);
freopen("lantern.out","w",stdout);
#endif
int n=io.read(),q=io.read();
vector<int>a(n+1);for(int i=1;i<=n;++i)a[i]=io.read();
Solver solver(move(a));
while(q--) {
int s=io.read(),k=io.read(),p=io.read();
io.write(solver.query(s,k,p));
}
return 0;
}