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
output
input
output
input
output
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。
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。
相关文章推荐
- 树状数组详解
- 2016 10 28考试 dp 乱搞 树状数组
- nyoj 士兵杀敌(一)(树状数组)
- 树状数组
- POJ 2352 Stars 树状数组
- 【BZOJ1103】大都市meg(DFS序,树状数组)
- 浅谈数据结构-树状数组
- 【数据结构之树状数组】从零认识树状数组
- hdu 5997 rausen loves cakes [启发式合并+树状数组/线段数]【杂类+数据结构】
- BZOJ 1064: [Noi2008]假面舞会 图论, BFS, 脑洞
- [HAOI2004] 数列 - 树状数组
- BZOJ 4592 脑洞治疗仪
- ZJNU 1712 树状数组
- POJ2481 Cows 树状数组
- 历届试题 小朋友排队 (求逆序数,树状数组)
- POJ2155 树状数组
- Hdu 5700 区间交【二分+树状数组+前缀和】
- 2017年ZJUT校赛-Problem B: 平方2——树状数组
- POJ2155 - Matrix (二维树状数组)
- poj 2299 Ultra-QuickSort(归并排序)||(树状数组+离散化)