其他分享
首页 > 其他分享> > Cards Sorting(树状数组+更具题目的特定优化)

Cards Sorting(树状数组+更具题目的特定优化)

作者:互联网

思路:将同样大小的数为一组,这一组的某个特定值的位置,来更新ans。

特定值:last的左边的值的优先级高于右边的值的优先级。

树状数组维护这个区间里面有多少个没有被删的值。

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
————————————————
版权声明:本文为CSDN博主「Mitsuha_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Mitsuha_/article/details/78448537
View problem
#include <bits/stdc++.h>
using namespace std;
#define ri register int
#define M 100005

template <class G> void read(G &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
 } 
 
int n;
vector <int> p[M];
int c[M];
int qu(int a)
{
    int res=0;
    while(a>0)
    {
        res+=c[a];
        a-=(a&-a);
    }
    return res;
}
void up(int a,int b)
{
    while(a<=n)
    {
        c[a]+=b;
        a+=(a&-a);
    }
}
int main(){
    
    read(n);
    for(ri i=1;i<=n;i++) 
    {
        int a;read(a);
        p[a].push_back(i);
    }
    int last=0;long long ans=0;
    for(ri i=1;i<=n;i++)
    {
        up(i,1);
    }
    for(ri i=1;i<=100000;i++)
    {
        int m=p[i].size();if(!m) continue;
        int tmp=-1;
        for(ri j=0;j<p[i].size();j++)
        {
            int b=p[i][j];
            if(b<last)
            {
                tmp=b;
            }
        }
        if(tmp==-1)
        {
            ans+=qu(p[i][p[i].size()-1])-qu(last);
            last=p[i][p[i].size()-1];
        }
        else
        {
            ans+=qu(n)-qu(last-1)+qu(tmp); // attention last-1 very important
            last=tmp;
        }
        for(ri j=0;j<p[i].size();j++)
        {
            int b=p[i][j];
            up(b,-1);
        }
    }
    printf("%lld",ans);
    return 0;
}
View Code

 

标签:Sorting,deck,int,树状,number,cards,ch,Cards,card
来源: https://www.cnblogs.com/Lamboofhome/p/15971019.html