小w的仙人掌
边进行尺取操作时边维护背包
用两个栈,一个维护前缀,一个维护后缀
模拟l++,r++
如果前缀空了就把后缀一个个加入,因为是栈,所以正好可以反过来
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#define re register int
#define maxn 100050
#define inf 0x3f3f3f3f;
using namespace std;
//第一个栈维护前缀,第二个维护后缀
struct bag{
int f[maxn],id;
};
stack<bag>fr,be,all;
int n,m,k;
int l,r;
int ans=0x3f3f3f3f;
struct node{
int a,b;
}a[maxn];
void del(){
if(fr.empty){
//如果第一个为空,就将第二个全部放进去
//由于是栈所以正好反过来
bag temp;
temp.f[0]=0;
memset(temp.f,inf,sizeog(temp.f));
while(!be.empty()){
int x=be.top().id;
for(re i=w;i>=a[x].a;i--)
temp.f[i]=min(temp.f[i],temp.f[i-a[x].a]+a[x].b);
fr.push(temp);
be.pop();
}
}
fr.pop();
//用删除栈顶模拟l++
}
//用加入栈模拟r++
void insert(){
bag temp;
memset(temp.f,inf,sizeof(temp));
temp.f[0]=0;
if(!be.empty())temp=be.top();
bag tmp;
tmp=temp;
for(re i=w;i>=a[r].r;i--){
tmp.f[i]=min(tmp.f[i-a[r].r]+a[r].b,tmp.f[i]);
}
//同时`维护背包??
tmp.id=r;
be.push(tmp);
r++;
}
bool pd(){
bag temp1,temp2;
memset(temp1.f,inf,sizeof(temp1.f));
memset(temp2.f,inf,sizeof(temp2.f));
temp1.f[0]=0;
temp2.f[0]=0;
if(!fr.empty())temp1=fr.top();
if(!be.empty())temp2=be.top();
for(re i=0;i<=w;i++){
if(temp1.f[i]+temp2.f[w-i]<=j)return 1;
}
return 0;
}
int main(){
scanf("%d%d%d",&n,&m,&k);
for(re i=1;i<=n;i++)
{
int a1,a2;;
scanf("%d%d",&a1,&a2);
a[i].a=a1;
a[i].b=a2;
}
l=1,r=1;
for(re l=1;l<=n;l++){
//枚举坐端点
if(l>1)del();
while(!query()){
if(r>n){
if(ans==0x3f3f3f3f){
puts("-1");
}
else printf("%d\n",ans);
return 0;
}
insert();
}
ans=min(ans,r-l+1);
}
if(ans==0x3f3f3f3f)put("-1");
else printf("%d\n",ans);
return 0;
}