The problem is to find at least a few deques that are sortable
Then we might as well think about it, after sorting the sequence, and then find the number of queues
#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{ years ++ ; b=0; h = Min [i]; } } } printf("%d\n",ans); return 0; }