给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点
标签:长度 must family alt pen esc sample out print
给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点
* Line 1: Two integers N and M.
* Lines 2..M+1: Line i+1 contains a_i and b_i.
* Line 1: The maximum possible number of spotted cows on FJ‘s farm, or -1 if there is no possible solution.
#include<cstdio> #include<cstring> #include<algorithm> using std::min; using std::max; const int M=250007,inf=0x3f3f3f3f; int read(){ int ans=0,f=1,c=getchar(); while(c<‘0‘||c>‘9‘){if(c==‘-‘) f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){ans=ans*10+(c-‘0‘); c=getchar();} return ans*f; } int n,m,x,y; int l[M],r[M],f[M]; int q[M],ql=1,qr; int main(){ n=read(); m=read(); for(int i=1;i<=n+1;i++) r[i]=i-1; for(int i=1;i<=m;i++){ x=read(); y=read(); r[y]=min(r[y],x-1); l[y+1]=max(l[y+1],x); } for(int i=n;i;i--) r[i]=min(r[i],r[i+1]); for(int i=2;i<=n+1;i++) l[i]=max(l[i],l[i-1]); f[qr=1]=0; for(int i=1;i<=n+1;i++){ for(int k=r[i-1]+1;k<=r[i];k++){ while(ql<=qr&&f[q[qr]]<=f[k]) qr--; q[++qr]=k; } while(ql<=qr&&q[ql]<l[i]) ql++; if(ql>qr) f[i]=-inf; else f[i]=f[q[ql]]+1; } if(f[n+1]>=0) printf("%d\n",f[n+1]-1); else printf("-1\n"); return 0; }
bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp
标签:长度 must family alt pen esc sample out print
原文地址:http://www.cnblogs.com/lyzuikeai/p/7566740.html