您的位置:首页 > 其它

Codeforce#424E. Cards Sorting(脑洞+树状数组)

2017-07-18 14:51 155 查看
E. 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
73 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.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
int sum[maxn], n;
vector<int> v[maxn];
void modify(int x, int val)
{
while(x <= n)
{
sum[x] += val;
x += x & (-x);
}
}
int pre = 1;
bool cmp(int u, int v)
{
int u1 = (u - pre + n) % n;
int v1 = (v - pre + n) % n;
return u1 < v1;
}
int getsum(int x)
{
int ret = 0;
while(x > 0)
{
ret += sum[x];
x -= x & (-x);
}
return ret;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
int num;
scanf("%d", &num);
v[num].push_back(i);
modify(i, 1);
}
for(int i = 1; i <= 100000; i++)
if(v[i].size())
{
sort(v[i].begin(), v[i].end(), cmp);
pre = v[i][v[i].size()-1];
}
pre = 0;
ll ans = 0;
for(int i = 1; i <= 100000; i++)
{
if(!v[i].size()) continue;
for(int j = 0; j < v[i].size(); j++)
{
int now = v[i][j];
if(now > pre)
ans += getsum(now) - getsum(pre);
else
ans += getsum(n) - (getsum(pre) - getsum(now));
pre = now;
modify(now, -1);
}
}
printf("%I64d\n", ans);
return 0;
}


题意:有一堆牌,不断的拿第一张,如果这张牌是此时最小的牌,则拿掉,如果不是,则放到牌底

树状数组,每个点刚开始为1,拿走的置为0,无论怎么换,其实顺序是不变的,特殊修改一下即可,对于数值相同的牌,要有一个拿的顺序,由前面拿完(数值比他小且紧挨着的)后的位置决定,可以在排序的时候修改一下cmp。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: