题解:P10465 双端队列
Solution
因为一定有一个分界点是第一个进入新队列的,在整个队列中位于
性质:
双端队列中的数一定在
接着由于是要求组合后的队列非严格单调递增,所以先将序列排序。如果考虑对于原序列进行划分,那么会发现很困难。所以考虑对排序的序列划分。那么就是把排序的序列分成几段,使得每个序列从左到右的数在
如果有了重复的数,就会使得重复的数在
其实重复的数就相当于在排好序的数组中,我们可以任意改变重复数那一段每个数
假如目前的队列是在下降的,如果这一段数在
假如我们目前的队列是在上升的,如果这一段数在
我们这样安排重复的数在
Code
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
pair<int,int> a[N];
int ans,n;
signed main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i].first,a[i].second=i;sort(a+1,a+1+n);
ans=1;//先有一段
for(int i=1,value=-1,last=0x3f3f3f3f;i<=n;i++)
{
int j=i;
while(j+1<=n&&a[i].first==a[j+1].first) j++;
/* pair<int,int> sort
if(a[i].first==a[i+1].first)
a[i].second<a[i+1].second */
if(value==-1)
if(a[j].second<last) last=a[i].second;
else value=1,last=a[j].second;
else
if(a[i].second>last) last=a[j].second;
else ans++,value=-1,last=a[i].second;
i=j;
//i++;
}
cout<<ans;
return 0;
}