BZOJ 2457 双端队列(思维

2457: [BeiJing2011]双端队列

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 582  Solved: 253
[Submit][Status][Discuss]

Description

 
       Sherry现在碰到了一个棘手的问题,有N个整数需要排序。
       Sherry手头能用的工具就是若干个双端队列。
      
她需要依次处理这N个数,对于每个数,Sherry能做以下两件事:
1.新建一个双端队列,并将当前数作为这个队列中的唯一的数;
2.将当前数放入已有的队列的头之前或者尾之后。
 
对所有的数处理完成之后,Sherry将这些队列排序后就可以得到一个非降的序列。

Input

第一行包含一个整数N,表示整数的个数。接下来的N行每行包含一个整数Di,其中Di表示所需处理的整数。

Output

其中只包含一行,为Sherry最少需要的双端队列数。

Sample Input


6
3
6
0
9
6
3

Sample Output


2

HINT

 

100%的数据中N≤200000。

 
 
代码如下:
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 2e5+5;
struct node{
    int x;
    int id;
}a[maxn];
bool cmp(node a,node b){
    if(a.x==b.x){
        return a.id<b.id;
    }
    return a.x<b.x;
}
 
int cnt;
int ans;
int Max[maxn];
int Min[maxn];
 
int main(){
#ifndef ONLINE_JUDGE
    FIN
#endif
    int n;
    cnt=ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i].x);
        a[i].id=i;
    }
    sort(a+1,a+n+1,cmp);
    //分成相等的块
    for(int i=1;i<=n;i++){
        if(a[i].x!=a[i-1].x||i==1){
            Max[cnt]=a[i-1].id;
            Min[++cnt]=a[i].id;
        }
    }
    Max[cnt]=a[n].id;
    //for(int i=1;i<=cnt;i++){
    //  cout<<Max[i]<<" ";
    //}
    //cout<<endl;
    //for(int i=1;i<=cnt;i++){
    //  cout<<Min[i]<<" ";
    //}
    //cout<<endl;
    int h=99999999;
    int flag=1;
    //flag表示当前的波的走势
    for(int i=1;i<=cnt;i++){
        if(!flag){
            if(h>Max[i]) h=Min[i];   //这里是递增的序
            else h=Max[i],flag=1;   //更新到谷峰
        }else{
            if(h<Min[i]) h=Max[i];
            else ans++,h=Min[i],flag=0;  //谷底
            //找不同的块,这里不同 就++  更新h
        }
        //cout<<flag<<" "<<h<<endl;
    }
    //cout<<endl;
    printf("%d\n",ans);
    return 0;
}
View Code

 

posted @ 2018-08-10 09:15  buerdepepeqi  阅读(235)  评论(0编辑  收藏  举报