Lydsy2457 双端队列(双端队列思想)
问题试求至少用几个双端队列可排序
那么我们不妨换过来思考,将数列排好序后,再来找队列个数
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct my{ int x,id; }; const int maxn=200000+10; my a[maxn]; int Max[maxn],Min[maxn]; bool cmp(const my &c,const my &d){ if(c.x==d.x) return c.id<d.id; return c.x<d.x; } int main(){ int n; scanf("%d",&n); for (int i=1;i<=n;i++){ scanf("%d",&a[i].x); a[i].id=i; } int top=1; sort(a+1,a+1+n,cmp); Min[1]=a[1].id; a[0].id=a[1].id; for (int i=1;i<=n;i++){ if(a[i].x!=a[i-1].x||i==1){ Max[top]=a[i-1].id; Min[++top]=a[i].id; } } int ans=0,h=0x3f3f3f3f; bool b=0; for (int i=1;i<=top;i++){ if(!b){ if(Max[i]<h) h=Min[i]; else { h=Max[i]; b=1; } } else{ if(Min[i]>h) h=Max[i]; else{ ans++; b=0; h=Min[i]; } } } printf("%d\n",ans); return 0; }