Codeforces-831E Cards Sorting(树状数组)
来源:互联网 发布:剑灵24人本优化不卡 编辑:程序博客网 时间:2024/06/06 15:42
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.
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.
Print the total number of times Vasily takes the top card from the deck.
46 3 1 2
7
11000
1
73 3 3 3 3 3 3
7
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 7cards.
题意:n个牌,牌上有数,每次从牌顶抽取一张牌,如果这张牌是当前的最小值那么就把它扔掉,不然放到牌堆底,问需要多少次操作把牌扔光。
题解:树状数组
树状数组中记录位置i是否有牌,1为有牌,0位没有牌
首先将数字排序,把数字在原数组中的位置存入vector中,v[i][j]表示第i大的数中第j小的位置(因为有的数会重复出现)
不妨假设有sz个不同的数,从最小的数开始模拟
假如当前是数字i,上一个数的位置为last,我们先找出i在last后面的第一次出现的位置pos,最后一次出现的位置end
那么这一次会把last之后所有的排都抽一次,并且把在last之后的有数字i的牌都扔掉
接下来分两种情况:
①位置pos前面还有数字i:ans+=sum(last+1,n),last=0
②位置pos前面没有数字i:ans+=sum(last+1,end),last=end
其中sum(l,r)为位置[l,r]中还剩下的牌
#include<bits/stdc++.h>using namespace std;typedef long long LL;const int MX = 1e5 + 5;struct BTree { int sum[MX], Max; BTree (){ Max=MX; memset(sum,0,sizeof(sum)); } inline int lowbit(int x) { return x & (-x); } void add(int x, int val) { while(x <= Max) { sum[x] += val; x += lowbit(x); } } int tot(int x) { int ret = 0; while(x) { ret += sum[x]; x -= lowbit(x); } return ret; } void clear() { memset(sum, 0, sizeof(sum)); } int query(int l, int r) { if(l > r) return 0; return tot(r) - tot(l - 1); }}t;struct node{ int x,id;}a[MX];bool cmp(node p1,node p2){ if(p1.x!=p2.x) return p1.x<p2.x; return p1.id<p2.id;}vector<int>v[MX];int main(){ int n; //freopen("in.txt","r",stdin); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i].x),a[i].id=i; sort(a+1,a+n+1,cmp); int sz=0; v[++sz].push_back(a[1].id); for(int i=2;i<=n;i++) { if(a[i].x!=a[i-1].x) ++sz; v[sz].push_back(a[i].id); } for(int i=1;i<=n;i++) t.add(i,1); int last=0; LL ans=0; for(int i=1;i<=sz;i++){ int pos=upper_bound(v[i].begin(),v[i].end(),last)-v[i].begin(); int cnt=v[i].size(); int bk=v[i][cnt-1]; if(pos>0) ans+=t.query(last,n); else ans+=t.query(last+1,bk); for(int j=cnt-1;j>=pos;j--){ t.add(v[i][j],-1); v[i].pop_back(); } if(pos>0) i--,last=0; else last=bk==n?0:bk; } printf("%I64d\n",ans); return 0;}
- Codeforces-831E Cards Sorting(树状数组)
- Codeforces 831 E. Cards Sorting set+树状数组模拟
- 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 831 E Cards Sorting(bit+stl)
- Codeforces #831E: Cards Sorting 题解
- Codeforces 831-E.Cards Sorting(线段树)
- Codeforce#424E. Cards Sorting(脑洞+树状数组)
- Codeforces-830B:Cards Sorting(树状数组+线段树)
- Codeforces Round #424 (Div. 2)C. Jury Marks 暴力 D. Office Keys 二分 E. Cards Sorting 树状数组
- Codeforces 387E George and Cards(二分+树状数组)
- George and Cards - CodeForces 387E 树状数组
- Codeforces 387E George and Cards(二分+树状数组)
- Codeforces Round #424 Div2 E. Cards Sorting
- Codeforces Round #424 E. Cards Sorting
- Codeforces 387E George and Cards 树状数组 + 集合set查询
- Codeforces Round #227 (Div. 2)---E. George and Cards(贪心, 树状数组+set维护, 好题!)
- 测试初体验
- Bootstrap辅助类和响应式工具
- js事件应用--基础(事件对象、鼠标事件、键盘事件)
- Codeforces #831D: Office Keys 题解
- Keras学习---RNN模型建立篇
- Codeforces-831E Cards Sorting(树状数组)
- python sqlite3
- java技术之静态代理
- 冒泡排序
- Hadoop发行版
- 如何判断并求出一个整数x是否可以表示成n(n>=2)个连续正整数的和。
- 2017暑期集训 Day 3 搜索与并查集
- USACO-section1.5 Prime Palindromes[回文数][素数]
- 创建WebP图片|Android Studio 谷歌图像文件格式