bzoj1510[POI2006]Kra-The Disks*

bzoj1510[POI2006]Kra-The Disks

题意:

一个瓶子有n个节,每个节有一个宽度。现在要从上往下扔m个盘子,如果盘子的下一个位置宽度比该盘子的宽度小则盘子会停在这个位置。问最后一个盘子会停在那个位置。n,m≤300000。

题解:

首先利用单调栈去掉那些没用的节,之后对于每个盘子,二分大于等于它宽度的第一个节,尝试它能否再往下掉到无用的节,具体看代码。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define maxn 300010
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
12     return f*x;
13 }
14 int st[maxn],stn[maxn],top,n,m,sz[maxn];
15 int main(){
16     n=read(); m=read(); inc(i,1,n)sz[i]=read();
17     for(int i=n;i>=1;i--){while(top&&sz[i]<st[top])top--; st[++top]=sz[i]; stn[top]=n-i+1;}
18     int now=1;
19     inc(i,1,m){
20         int x=read(); int pos=lower_bound(st+now,st+top+1,x)-st;
21         if(pos>top){printf("%d",0); return 0;}
22         if(i==m)printf("%d",n-(stn[pos]-stn[pos-1]>1?stn[pos-1]+1:stn[pos])+1);
23         if(stn[pos]-stn[pos-1]>1)now=pos,stn[pos-1]++;else now=pos+1;
24     }
25     return 0;
26 }

20161115

原文地址:https://www.cnblogs.com/YuanZiming/p/6067446.html