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,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...
随机推荐
- IO多路复用(二) -- select、poll、epoll实现TCP反射程序
接着上文IO多路复用(一)-- Select.Poll.Epoll,接下来将演示一个TCP回射程序,源代码来自于该博文https://www.cnblogs.com/Anker/p/3258674.h ...
- 3.10-通过requests、BeautifulSoup、webbrowser模块的相关方法,爬取网页数据示例程序(一)
import requests,bs4res=requests.get('https://www.hao123.com/')print('res对象的类型:',type(res))res.raise_ ...
- Hexo博客 云服务器搭建
下载nodejs: https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.xz 解压:tar zxv 解压后编译: ...
- Linux的压缩/解压缩文件处理 zip & unzip
Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服 ...
- sublime text 多行代码注释快捷键
多行选择后按下ctrl+/ 选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中 ...
- Struts2文件的上传和下载实现
<一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...
- JVM初识、调优
JVM是按照运行时数据的存储结构来划分内存结构的,JVM在运行java时,将他们划分成几种不同格式的数据,分别存储在不同的区域,这些数据统一称为运行时数据,运行时数据包括java程序本身的数据信息和J ...
- spring 整合 struts2 + Hibernate application配置文件(基于注解)
下面是 application.xml 文件. <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- idea导出包含main函数的jar
1.首先打开File->project stucture->Artifacts 2.按照下图方式: 3.选择面main函数的所在的类,选择MAINFEST.MF问的生成路径 这里一定选择 ...
- 第206天:http协议终极详解---看这一篇就够了
HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...