首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting 树状数组+技巧

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

作者头像
glm233
发布2020-09-28 17:39:53
发布2020-09-28 17:39:53
46800
代码可运行
举报
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
复制
4
6 3 1 2

output

Copy

代码语言:javascript
代码运行次数:0
运行
复制
7

input

Copy

代码语言:javascript
代码运行次数:0
运行
复制
1
1000

output

Copy

代码语言:javascript
代码运行次数:0
运行
复制
1

input

Copy

代码语言:javascript
代码运行次数:0
运行
复制
7
3 3 3 3 3 3 3

output

Copy

代码语言:javascript
代码运行次数:0
运行
复制
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在数组中的位置,按照与上一个数的相对位置排序,上一个数是上一个数字在数组中出现的最后一个位置,最后区间查询就好~

代码语言:javascript
代码运行次数:0
运行
复制
#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;
    
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档