码迷,mamicode.com
首页 > 编程语言 > 详细

cf 830B - Cards Sorting 树状数组

时间:2017-08-18 22:21:57      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:this   nbsp   cti   hold   sts   之间   always   style   否则   

B. Cards Sorting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.

Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn‘t know where this card (or these cards) is.

You are to determine the total number of times Vasily takes the top card from the deck.

Input

The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.

The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), where ai is the number written on the i-th from top card in the deck.

Output

Print the total number of times Vasily takes the top card from the deck.

Examples
input
4
6 3 1 2
output
7
input
1
1000
output
1
input
7
3 3 3 3 3 3 3
output
7
Note

In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.

 

题意:

有n标有数字的张牌,摞在桌子上,现在想要给牌排序排序方法是:每次抽出最上面的牌,如果他是剩余的这摞牌中最小的就把他拿出来,否则把他放到最下面。问拿完所有的牌的总次数是多少。

代码:

//可以看出每次都是O(n)一遍扔掉最小的(可能有多个相同的或不同的)之后剩余的牌变换次数+1(剩余的相对位置保持不变),直到没有牌为止,这是O(nn)的。
//可以用树状数组记录每个位置区间段中有多少张牌,当前扔掉的最小的牌的位置是递增的时(即在上一个被扔掉的牌的位置的右边)
//那么次数+1的牌就只有当前最小牌与上个被扔掉的牌的位置之间的牌,否则就是这个区间以外的区间的牌的次数+1,然后扔掉的牌
//从树状数组中去掉。O(nlogn)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100000;
int A[maxn*4+10],n;
vector<int>v[maxn*4];
void update(int id,int v)
{
    while(id<=maxn*4){
        A[id]+=v;
        id+=(id&(-id));
    }
}
int query(int id)
{
    int sum=0;
    while(id){
        sum+=A[id];
        id-=(id&(-id));
    }
    return sum;
}
int main()
{
    scanf("%d",&n);
    memset(A,0,sizeof(A));
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        v[x].push_back(i);
        update(i,1);
    }
    int last=0;
    ll ans=0;
    for(int i=1;i<=maxn;i++){
        int Size=v[i].size(),tmp=-1;
        if(Size==0) continue;
        for(int j=0;j<Size;j++){
            if(v[i][j]<last)
                tmp=v[i][j];
        }
        if(tmp==-1){
            ans+=query(v[i][Size-1])-query(last);
            last=v[i][Size-1];
        }
        else{
            ans+=query(maxn)-(query(last)-query(tmp));
            last=tmp;
        }
        for(int j=0;j<Size;j++)
            update(v[i][j],-1);
    }
    printf("%lld\n",ans);
    return 0;
}

 

cf 830B - Cards Sorting 树状数组

标签:this   nbsp   cti   hold   sts   之间   always   style   否则   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/7392223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!