Codeforces 830B - Cards Sorting 树状数组
1 second
256 megabytes
standard input
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.
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.
4
6 3 1 2
7
1
1000
1
7
3 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 7 cards.
题意:
有n标有数字的张牌,摞在桌子上,现在想要给牌排序排序方法是:每次抽出最上面的牌,如果他是剩余的这摞牌中最小的就把他拿出来,否则把他放到最下面。问拿完所有的牌的总次数是多少。
代码:
//可以看出每次都是O(n)一遍扔掉最小的(可能有多个相同的或不同的)之后剩余的牌变换次数+1(剩余的相对位置保持不变),直到没有牌为止,这是O(nn)的。
//可以用树状数组记录每个位置区间段中有多少张牌,当前扔掉的最小的牌的位置是递增的时(即在上一个被扔掉的牌的位置的右边)
//那么次数+1的牌就只有当前最小牌与上个被扔掉的牌的位置之间的牌,否则就是这个区间以外的区间的牌的次数+1,然后扔掉的牌
//从树状数组中去掉。O(nlogn)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
int A[maxn*+],n;
vector<int>v[maxn*];
void update(int id,int v)
{
while(id<=maxn*){
A[id]+=v;
id+=(id&(-id));
}
}
int query(int id)
{
int sum=;
while(id){
sum+=A[id];
id-=(id&(-id));
}
return sum;
}
int main()
{
scanf("%d",&n);
memset(A,,sizeof(A));
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
v[x].push_back(i);
update(i,);
}
int last=;
ll ans=;
for(int i=;i<=maxn;i++){
int Size=v[i].size(),tmp=-;
if(Size==) continue;
for(int j=;j<Size;j++){
if(v[i][j]<last)
tmp=v[i][j];
}
if(tmp==-){
ans+=query(v[i][Size-])-query(last);
last=v[i][Size-];
}
else{
ans+=query(maxn)-(query(last)-query(tmp));
last=tmp;
}
for(int j=;j<Size;j++)
update(v[i][j],-);
}
printf("%lld\n",ans);
return ;
}
Codeforces 830B - Cards Sorting 树状数组的更多相关文章
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组
E. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- CodeForces–830B--模拟,树状数组||线段树
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- hdu 2838 Cow Sorting (树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组
\(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Codeforces 650D - Zip-line(树状数组)
Codeforces 题目传送门 & 洛谷题目传送门 我怕不是个 nt--一开始忽略了"询问独立"这个条件--然后就一直在想有什么办法维护全局 LIS--心态爆炸 首先离散 ...
- Codeforces 1139F Dish Shopping 树状数组套平衡树 || 平衡树
Dish Shopping 将每个物品拆成p 和 s 再加上人排序. 然后问题就变成了, 对于一个线段(L - R), 问有多少个(li, ri)满足 L >= li && R ...
- HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用
这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...
随机推荐
- 使用经验风险最小化ERM方法来估计模型误差 开坑
虽然已经学习了许多机器学习的方法,可只有我们必须知道何时何处使用哪种方法,才能将他们正确运用起来. 那不妨使用经验最小化ERM方法来估计 . 首先: 其中, δ代表训练出错的概率 k代表假设类的个数 ...
- eclipse版本信息及操作系统
一.查看版本信息: 进入到eclipse安装目录下,有一个.eclipseproduct文件,用记事本打开,就可以知道版本了,后面version=的值就是版本 二.是否为32位操作系统: 找到ecli ...
- HTML页面模板代码
作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 HTML页面模板代码 常用的页面模板 & ...
- PSP表格记录功能
关于王者荣耀交流协会的PSP表格记录功能,就是针对我们平时做表格时候遇到问题的简化与解决.这部分功能可以记录我们开始时间,暂停时间,结束时间,并自动计算出各个时间段的净时间.只要你开始工作时点一下开始 ...
- 《JavaScript》JS中的常用方法attr(),splice()
1.jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到attr(),attr()有4个表达式. attr(属性名) ...
- 福大软工·第十一次作业-Alpha事后诸葛亮
福大软工·第十一次作业-Alpha事后诸葛亮 组长博客链接 本次作业博客链接 项目Postmortem 模板 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描 ...
- DataTable转List<T>集合
#region DataTable转List集合 +static IList<T> DataTableToList<T>(DataTable dt) where T : cla ...
- Jquery获取属性值
jq获取某个标签内的属性值:$("#TeamPerformanceYearUl li:eq(0)").attr('data') jq获取li或者td第一个属性(索引值从零开始)$( ...
- 在原有的基础之上,启用NAT模型
# 给虚拟主机实例添加一个网关 route add default gw 192.168.23.1 # 在宿主机打开网卡间转发功能 echo 1 > /proc/sys/net/ipv4/i ...
- 【Leetcode】113Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...