Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)E. Cards Sorting

题意:有n个数字,我遍历过去,如果他是当前最小值,就删除,否则放到最后面去,问得遍历多少个数字,(直到所有数字消失

思路:我们保存每个数字的位置,这个数字是否能删除,如果他在上一个数字的最后一个位置后面就可以删除了,那么标记下+树状数组(我这里的y表示的就是上一个数删除的最后一个位置)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=1e5+10;
 5 
 6 int a[N];
 7 set<int >b[N];
 8 set<int >::iterator x,it;
 9 int n,xx;
10 
11 void add(int x,int y){
12     while(x<=n){
13         a[x]+=y;
14         x+=x&-x;
15     }
16 }
17 int get(int x){
18     int sum=0;
19     while(x){
20         sum+=a[x];
21         x-=x&-x;
22     }
23     return sum;
24 }
25 int main(){
26 
27     scanf("%d",&n);
28     for(int i=1;i<=n;i++){
29         scanf("%d",&xx);
30         b[xx].insert(i);
31         add(i,1);
32     }
33     int j=0,y=0;
34     ll ans=0;
35     for(int i=1;i<=100000;i++){
36         x=b[i].lower_bound(y);
37         for(it=x;it!=b[i].end();it++){
38             j++;
39             add(*it,-1);y=*it;
40         }
41         for(it=b[i].begin();it!=x;it++){
42             if(it==b[i].begin()){
43                 ans+=j+get(n);
44                 j=0;
45             }
46             add(*it,-1);j++;
47             y=*it;
48         }
49 
50     }
51     cout<<ans+j<<endl;
52 }

 

posted on 2017-07-15 10:24  hhhhx  阅读(112)  评论(0编辑  收藏  举报

导航