Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting 树状数组+技巧

时间:2022-07-26
本文章向大家介绍Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting 树状数组+技巧,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

Copy

4
6 3 1 2

output

Copy

7

input

Copy

1
1000

output

Copy

1

input

Copy

7
3 3 3 3 3 3 3

output

Copy

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.

树状数组维护1-i区间的总数,每次拿走牌都是从小到大,所以可以从小到大遍历,当然先预处理出相同数值x在数组中的位置,按照与上一个数的相对位置排序,上一个数是上一个数字在数组中出现的最后一个位置,最后区间查询就好~

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[100005],n,pre=1,a[100005];
template <typename T> inline void read(T& x)
{
    x=0;char ch=getchar();ll f=1;
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x个加上val
vector<ll>v[100005];
inline bool cmp(ll a,ll b)
{
	return (a-pre+n)%n<(b-pre+n)%n;
}
int main()
{
	cin>>n;
	ll maxx=-inf,minn=inf;
	for(rg i=1;i<=n;i++)cin>>a[i],add(i,1),maxx=max(a[i],maxx),minn=min(minn,a[i]),v[a[i]].push_back(i);
	for(rg i=minn;i<=maxx;i++)
	{
		if(!v[i].size())continue;
		sort(v[i].begin(),v[i].end(),cmp);
		pre=v[i][v[i].size()-1];
	}
	ll tep=1,ans=0;
	/*for(rg i=0;i<v[3].size();i++)
	{
		cout<<v[3][i]<<" ";
	}
	cout<<endl;*/
	for(rg i=minn;i<=maxx;i++)
	{
		if(!v[i].size())continue;
		for(rg j=0;j<v[i].size();j++)
		{
			//cout<<v[i][j]<<" "<<tep<<endl;
			ll check=v[i][j];
			if(check>=tep)ans+=query(v[i][j])-query(tep-1);
			else ans+=query(n)-query(tep-1)+query(v[i][j]);
			tep=v[i][j];
			add(tep,-1);
			//cout<<ans<<endl;
		}
	}
	cout<<ans<<endl;
    return 0;
    
}