Codeforces Round #424 (Div. 2) E. Cards Sorting(树状数组)
来源:互联网 发布:某酒店2000w数据 网盘 编辑:程序博客网 时间:2024/06/05 06:46
E. Cards Sorting
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
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.
题目;n张牌叠成一摞。初始最上面是1,每张牌有权值,每次对于最上面的牌来说,如果它是现在权值最小的,丢出去,否则将此牌放到最下面。问多少次操作后n张牌都没有了。
题解:我们先预处理出不同权值牌的所在位置,用树状数组记录每个位置是否有牌,用last标记上一次结束操作的位置。具体操作看代码。
代码:
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<vector>#include<queue>#include<algorithm>#include<math.h>#include<map>using namespace std;typedef long long int ll;typedef pair<int,int>pa;const int N=1e5+10;const int MOD=1e9+7;const ll INF=1e18;int read(){ int x=0; char ch = getchar(); while('0'>ch||ch>'9')ch=getchar(); while('0'<=ch&&ch<='9') { x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); } return x;}/************************************************************/struct node{ int x,id; bool operator <(const node &t)const { if(x!=t.x) return x<t.x; return id<t.id; }}a[N];int n;vector<int>p[N];int sum[N];int lowbit(int x){ return x&(-x);}int add(int x,int val){ while(x<=N) { sum[x]+=val; x+=lowbit(x); }}ll query(int x){ ll res=0; while(x) { res+=sum[x]; x-=lowbit(x); } return res;}int main(){ cin>>n; memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++) { cin>>a[i].x; a[i].id=i; } sort(a+1,a+1+n); int sz=0; p[++sz].push_back(a[1].id); for(int i=2;i<=n;i++) { if(a[i].x!=a[i-1].x) sz++; p[sz].push_back(a[i].id); } for(int i=1;i<=n;i++) add(i,1); ll ans=0; int last=0; for(int i=1;i<=sz;i++) { int pos=upper_bound(p[i].begin(),p[i].end(),last)-p[i].begin(); int cnt=p[i].size(); int en=p[i][cnt-1]; if(pos>0) ans+=query(n)-query(last); else ans+=query(en)-query(last); for(int j=cnt-1;j>=pos;j--) { add(p[i][j],-1); p[i].pop_back(); } if(pos>0) i--,last=0; else last=en==n?0:en; } cout<<ans<<endl;}
- Codeforces Round #424 (Div. 2) E. Cards Sorting(树状数组)
- Codeforces Round #424 (Div. 2) E. Cards Sorting(思维 树状数组)
- Codeforces Round #424 (Div. 2, )-树状数组|线段树-E. Cards Sorting
- Codeforces Round #424 (Div. 2)C. Jury Marks 暴力 D. Office Keys 二分 E. Cards Sorting 树状数组
- Codeforces-831E Cards Sorting(树状数组)
- Codeforces Round #227 (Div. 2)---E. George and Cards(贪心, 树状数组+set维护, 好题!)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E Cards Sorting
- Codeforces Round #424 Div2 E. Cards Sorting
- Codeforces Round #424 E. Cards Sorting
- Codeforces 831 E. Cards Sorting set+树状数组模拟
- Codeforce#424E. Cards Sorting(脑洞+树状数组)
- 树状数组Codeforces Round #301 (Div. 2) E - Infinite Inversions
- Codeforces Round #368 (Div. 2) E. Garlands (树状数组)
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组
- Codeforces Round #368 (Div. 2) E. Garlands(二维树状数组)
- Codeforces Round #423 (Div. 2) E. DNA Evolution(树状数组)
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组
- 【反序表+树状数组】Codeforces Round #441(Div.2)D[Sorting the Coins]题解
- CSS三大特性
- zabbix通过lykchat发送告警信息配置过程
- Android探秘系列——Android Studio工程解剖(1)
- Android 性能优化之使用MAT分析内存泄露问题
- CSS-选择器
- Codeforces Round #424 (Div. 2) E. Cards Sorting(树状数组)
- POJ 3253 Fence Repair(优先队列+思维贪心)(哈夫曼树算法)
- F
- 关于垂直切分Vertical Sharding的粒度
- SpringBoot学习——全局异常处理设置(返回JSON)
- leetcode 268. Missing Number
- 函数查询所有子节点
- 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式
- 【军师联盟】--鬼才(军师祭酒)郭嘉郭奉孝(如何成为领导最信任的人)