Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting 树状数组+技巧
B. 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
Copy
4
6 3 1 2
output
Copy
7
input
Copy
1
1000
output
Copy
1
input
Copy
7
3 3 3 3 3 3 3
output
Copy
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.
树状数组维护1-i区间的总数,每次拿走牌都是从小到大,所以可以从小到大遍历,当然先预处理出相同数值x在数组中的位置,按照与上一个数的相对位置排序,上一个数是上一个数字在数组中出现的最后一个位置,最后区间查询就好~
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[100005],n,pre=1,a[100005];
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x个加上val
vector<ll>v[100005];
inline bool cmp(ll a,ll b)
{
return (a-pre+n)%n<(b-pre+n)%n;
}
int main()
{
cin>>n;
ll maxx=-inf,minn=inf;
for(rg i=1;i<=n;i++)cin>>a[i],add(i,1),maxx=max(a[i],maxx),minn=min(minn,a[i]),v[a[i]].push_back(i);
for(rg i=minn;i<=maxx;i++)
{
if(!v[i].size())continue;
sort(v[i].begin(),v[i].end(),cmp);
pre=v[i][v[i].size()-1];
}
ll tep=1,ans=0;
/*for(rg i=0;i<v[3].size();i++)
{
cout<<v[3][i]<<" ";
}
cout<<endl;*/
for(rg i=minn;i<=maxx;i++)
{
if(!v[i].size())continue;
for(rg j=0;j<v[i].size();j++)
{
//cout<<v[i][j]<<" "<<tep<<endl;
ll check=v[i][j];
if(check>=tep)ans+=query(v[i][j])-query(tep-1);
else ans+=query(n)-query(tep-1)+query(v[i][j]);
tep=v[i][j];
add(tep,-1);
//cout<<ans<<endl;
}
}
cout<<ans<<endl;
return 0;
}
- CentOS7下Elasticsearch集群部署记录
- Flash/Flex学习笔记(27):摄像头/麦克风的视频/音量指示器
- Flash/Flex学习笔记(26):AS3自定义右键菜单
- Python27中Json对中文的处理
- Python在VSCode中进入交互界面调试
- CentOS7下单机部署RabbltMQ环境的操作记录
- Flash/Flex学习笔记(24):粒子效果
- 针对业务日志的监控报警设置
- 对广晟有色的数据分析
- Flash/Flex学习笔记(22):滤镜学习
- Python输出信息
- Flash/Flex学习笔记(21):利用colorTransform改变对象的颜色及透明度
- 网站访问状态和超时时间监控报警设置
- 为treeview添加客户端事件
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法