Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. 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张牌,如果当前不是最小值,将其放到最底下,否则拿掉,问最少需要多少步;
思路:就是模拟,然后对于求答案的时候需要更新,对于未拿走的牌,标记为1,否则为0,利用树状数组快速求区间和,即求到下张牌的距离;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; struct AYT
{
int tree[N];
int lowbit(int x)
{
return x&-x;
}
void update(int x,int c)
{
while(x<N)
{
tree[x]+=c;
x+=lowbit(x);
}
}
int query(int x)
{
int ans=;
while(x)
{
ans+=tree[x];
x-=lowbit(x);
}
return ans;
}
} tree; int a[N],n;
vector<int>pos[N];
int check(int i,int x)
{
int s=,e=pos[i].size()-,ans=-;
while(s<=e)
{
int mid=(s+e)>>;
if(pos[i][mid]>x)
{
ans=mid;
e=mid-;
}
else s=mid+;
}
return ans;
}
int dis(int s,int e)
{
if(s==-)return tree.query(e);
if(s<e)return tree.query(e)-tree.query(s);
return tree.query(n)-tree.query(s)+tree.query(e);
}
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
tree.update(i,);
pos[a[i]].push_back(i);
}
int pre=-;
LL ans=;
for(int i=; i<=; i++)
{
if(!pos[i].size())continue;
int v=check(i,pre);
for(int j=max(,v); j<pos[i].size(); j++)
{
ans+=dis(pre,pos[i][j]);
pre=pos[i][j];
tree.update(pre,-);
//cout<<pre<<" "<<ans<<endl;
}
for(int j=; j<v; j++)
{
ans+=dis(pre,pos[i][j]);
pre=pos[i][j];
tree.update(pre,-);
//cout<<pre<<" "<<ans<<endl;
} }
printf("%lld\n",ans);
return ;
}
Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组的更多相关文章
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
- 【Splay】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting
Splay要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) A 水 B stl C stl D 暴力 E 树状数组
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)A,B,C
A:链接:http://codeforces.com/contest/831/problem/A 解题思路: 从前往后分别统计递增,相等,递减序列的长度,如果最后长度和原序列长度相等那么就输出yes: ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力
题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
随机推荐
- spark与kafka集成进行实时 nginx代理 这种sdk埋点 原生日志实时解析 处理
日志格式202.108.16.254^A1546795482.600^A/cntv.gif?appId=3&areaId=8213&srcContId=2535575&area ...
- mxnet 查看 Sym shape
import mxnet as mximport numpy as npimport randomimport mxnet as mximport sysdata_shape = {'data':(6 ...
- 过滤特殊字符(包括过滤emoji表情)
/** * 过滤特殊字符 * @param $text * @return mixed */ public static function filterSpecialChars($text) { // ...
- 作为php了解一下共享内存的概念及优缺点
共享内存是一种在相同机器中两个正在运行的进程之间共享和传递数据的有效方式,不同进程之间共享的内存通常安排为同一段物理内存:顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存.一个进程可创建一 ...
- django ORM模型表的一对多、多对多关系、万能双下划线查询
一.外键使用 在 MySQL 中,如果使用InnoDB引擎,则支持外键约束.(另一种常用的MyIsam引擎不支持外键) 定义外键的语法为fieldname=models.ForeignKey(to_c ...
- bzoj3932 / P3168 [CQOI2015]任务查询系统(主席树+差分)
P3168 [CQOI2015]任务查询系统 看到第k小,就是主席树辣 对于每一段任务(a,b,k),在版本a的主席树+k,版本b+1的主席树-k 同一时间可能有多次修改,所以开个vector存操作, ...
- 20145326蔡馨熤《网络对抗》—— Web安全基础实践
20145326蔡馨熤<网络对抗>—— Web安全基础实践 1.实验后回答问题 (1)SQL注入攻击原理,如何防御. 原理: SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程 ...
- Windows 动态链接库DLL使用
转载:https://blog.csdn.net/heyabo/article/details/8721611 转载:https://www.cnblogs.com/jin521/p/5598529. ...
- USB通信基础知识
1 USB系统组成 主机:提供USB接口和接口管理功能的硬件.软件.固件的复合体.PC机或OTG设备,一个USB系统只能有一个主机 设备:1.集线器HUB:扩展主机接口,设备可以通过其接入主机 2. ...
- IDEA安装与破解
今天下午偶然在知乎上看到IDEA和eclipse的软件分析,所以装了一个IDEA,不过肯定是破解,不会购买激活码 IDEA官网:http://www.jetbrains.com/idea/ 安装教程: ...