codeforces 830 B Cards Sorting
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、可以将数整体向左移,
2、如果最左边的数在剩余的数中最小,可以删去最左边的数
两个操作的花费都是1,问将所有的数删去所需要的代价
线段树
记录当前序列的最右端R在哪儿
R左边的数表示实际移到了后面,R右边的数实际在前面
移动带来的线段树中节点大小的改变,通过记录节点大小解决
每次查询在R前面查一次,在R后面查一次
注意如果前面后面的值相等,选R后面的
#include<cstdio>
#include<algorithm>
#define N 100001 #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif using namespace std;
int n,R,tmp1,tmp2,opl,opr;
long long ans;
int sum[N<<],minn[N<<],pos[N<<],mid[N<<],a[N];
void up(int k)
{
sum[k]=sum[k<<]+sum[k<<|];
minn[k]=min(minn[k<<],minn[k<<|]);
if(minn[k]==minn[k<<]) pos[k]=pos[k<<];
else pos[k]=pos[k<<|];
}
void build(int k,int l,int r)
{
if(l==r)
{
scanf("%d",&a[l]);
minn[k]=a[l]; sum[k]=; pos[k]=l;
return;
}
mid[k]=l+r>>;
build(k<<,l,mid[k]);
build(k<<|,mid[k]+,r);
up(k);
}
int find_minn(int k,int l,int r)
{
if(l>=opl && r<=opr) return pos[k];
if(opr<=mid[k]) return find_minn(k<<,l,mid[k]);
else if(opl>mid[k]) return find_minn(k<<|,mid[k]+,r);
{
int t1=find_minn(k<<,l,mid[k]);
int t2=find_minn(k<<|,mid[k]+,r);
if(a[t1]<=a[t2]) return t1;
return t2;
}
}
int query(int k,int l,int r)
{
if(l>=opl && r<=opr) return sum[k];
if(opr<=mid[k]) return query(k<<,l,mid[k]);
else if(opl>mid[k]) return query(k<<|,mid[k]+,r);
return query(k<<,l,mid[k])+query(k<<|,mid[k]+,r);
}
void delet(int k,int l,int r)
{
if(l==r)
{
sum[k]=; pos[k]=l;
minn[k]=a[l]=N+;
return;
}
if(opl<=mid[k]) delet(k<<,l,mid[k]);
else delet(k<<|,mid[k]+,r);
up(k);
}
int main()
{
scanf("%d",&n);
build(,,n);
R=n+;
for(int i=;i<=n;i++)
{
tmp1=tmp2=;
if(R)
{
opl=; opr=R-;
tmp1=find_minn(,,n);
}
if(R<n)
{
opl=R+; opr=n;
tmp2=find_minn(,,n);
}
if( (tmp1 && tmp2 && a[tmp2]<a[tmp1]) || (tmp2 && !tmp1) )
{
if(tmp2>R) opl=R+,opr=tmp2,ans+=query(,,n);
else
{
if(R<n) opl=R+,opr=n,ans+=query(,,n);
opl=,opr=tmp2,ans+=query(,,n);
}
opl=tmp2; delet(,,n); R=tmp2;
}
else if(tmp1 && tmp2 && a[tmp1]==a[tmp2])
{
opl=R+,opr=tmp2,ans+=query(,,n);
opl=tmp2;delet(,,n); R=tmp2;
}
else
{
if(tmp1>R) opl=R+,opr=tmp1,ans+=query(,,n);
else
{
if(R<n) opl=R+,opr=n,ans+=query(,,n);
opl=,opr=tmp1,ans+=query(,,n);
}
opl=tmp1; delet(,,n); R=tmp1;
}
}
printf(LL,ans);
}
codeforces 830 B Cards Sorting的更多相关文章
- codeforces 830 B. Cards Sorting(线段树)
题目链接:http://codeforces.com/contest/830/problem/B 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...
- 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 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- AC日记——Cards Sorting codeforces 830B
Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)
Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #424 Div2 E. Cards Sorting
我只能说真的看不懂题解的做法 我的做法就是线段树维护,毕竟每个数的顺序不变嘛 那么单点维护 区间剩余卡片和最小值 每次知道最小值之后,怎么知道需要修改的位置呢 直接从每种数维护的set找到现在需要修改 ...
- Codeforces Round #424 E. Cards Sorting
题目大意:给你一堆n张牌(数字可以相同),你只能从上面取牌,如果是当前牌堆里面最小的值则拿走, 否则放到底部,问你一共要操作多少次. 思路:讲不清楚,具体看代码.. #include<bits/ ...
- 【Splay】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting
Splay要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
- CodeForces 830B - Cards Sorting
将每个数字的位置存进该数字的vector中 原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案 树状数组维护每个数字现在的位置和原位置之差 #inc ...
随机推荐
- ssm整合快速入门程序(一)
整合基础说明 spring 是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Jav ...
- [HDU5799]This world need more Zhu
题面戳我 题意: 给定一棵树,m次操作,每次询问某一棵子树中,或者是某一条路径上,出现次数为a的所有数字之和与出现次数为b的所有数字之和的gcd 原题表述:the \(\gcd\) of the su ...
- call是什么?一次说个明白
首先简单粗暴的从例子中看概念 var a = {}; function foo() { this.name = "hello"; this.age = 100 } foo.call ...
- MSIL实用指南-创建字段
本篇讲解怎么创建字段,主要是在修饰符的创建上. 创建字段的方法是TypeBuilder.DefineField,传入字段名称.字段类型.字段修饰符等参数,返回一个FieldBuilder对象.先看这一 ...
- SignalR Self Host+MVC等多端消息推送服务(1)
一.概述 由于项目需要,最近公司项目里有个模块功能,需要使用到即时获得审批通知:原本的设计方案是使用ajax对服务器进行定时轮询查询,刚刚开始数据量和使用量不大的时候还好,后来使用量的增加和系统中各种 ...
- 【.NetCore】基于jenkins以及gitlab的持续编译及发布
前沿 其实本来是想把标题叫做持续集成的,只是后来看看研究出的内容,就只有发布这一个动作,自动化测试等内容也未涉及到,所以改名叫持续编译及发布应该更加贴切吧? 问题背景 其实目前我们传统方式上的发布方式 ...
- IPFS家族(二)
go-ipfs IPFS协议的go语言实现,ipfs的核心协议,最新版是v0.4.13 下载地址:https://dist.ipfs.io/#go-ipfs 源代码地址:https://github. ...
- Java 缩写总结
1.JVM:Java Virtual Machine(Java虚拟机)的缩写. 它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. Java语言的一个非常重要的特点就是与 ...
- Axis1.4之定制发布服务
将axis1.4_home\webapps目录下的axis文件夹拷贝到tomcat_home\webapps目录下.然后在tomcat_home\webapps\axis\WEB-INF\lib下添加 ...
- STL --> find()和find_if()
find()和find_if() 一.find()函数 find(first, end, value); // 返回区间[first,end)中第一个值等于value的元素的位置.如果没有找到匹配元素 ...