[set]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个数,这些数可能重复,每次取出当前第一个数,如果这个数是这些数中的最小值,则扔掉,否则放到最后
思路:若取出的这个数是最小的,则这些数的总数要减1,否则要放到末尾,如果我们把这些数都取了一遍,会发现下一个取数的周期中原来的数相对位置是不变的,比如说123->231->312->123
若要取出一个数,则必须先把它前面的那些数取出来,若要取末尾那个数,则其前面那些数都要被取出,即取出了这些数的总个数
首先我们记录数对应的下标有哪些,每次要取出最小的那个,所以我们先排个序从最小的那个开始找
一开始总数是n,第一次必定会把当前所有的数都取一遍,所以设答案初始值为n,现在从第一位开始找,设mid=1
每次会找大于等于mid的下标,如果找不到,会返回end()地址(也就是说当前集合中不存在大于等于mid的那个数)
如果找不到了,比如说上一个mid=4,而这个元素总只有1的下标,说明我们已经找过一次当前的所以数了,要把总数加上重新从开头找
每次要删掉当前最小的那个,同时总数减一
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=1e5+;
int a[amn];
set<int> p[amn];
set<int>::iterator k;
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i];
p[a[i]].insert(i); ///记录数对应的下标有哪些
}
sort(a+,a++n); ///每次要取出最小的那个,所以我们先排个序从最小的那个开始找
ll tot=n,mid=,ans=n; ///一开始总数是n,第一次必定会把当前所有的数都取一遍,所以设答案初始值为n,现在从第一位开始找,设mid=1
for(int i=;i<=n;i++){
k=p[a[i]].lower_bound(mid); ///每次会找大于等于mid的下标,如果找不到,会返回end()地址(也就是说当前集合中不存在大于等于mid的那个数)
if(k==p[a[i]].end()){ ///如果找不到了,比如说上一个mid=4,而这个元素总只有1的下标,说明我们已经找过一次当前的所以数了,要把总数加上重新从开头找
ans+=tot; ///我们找过了所有数一遍,所以加上数的总个数
mid=; ///重新从第一位开始找
k=p[a[i]].lower_bound(mid);
}
mid=*k;
p[a[i]].erase(k); ///删掉当前最小的那个
tot--; ///同时总数减一
}
printf("%lld\n",ans);
}
/***
给你n个数,这些数可能重复,每次取出当前第一个数,如果这个数是这些数中的最小值,则扔掉,否则放到最后
若取出的这个数是最小的,则这些数的总数要减1,否则要放到末尾,如果我们把这些数都取了一遍,会发现下一个取数的周期中原来的数相对位置是不变的,比如说123->231->312->123
若要取出一个数,则必须先把它前面的那些数取出来,若要取末尾那个数,则其前面那些数都要被取出,即取出了这些数的总个数
首先我们记录数对应的下标有哪些,每次要取出最小的那个,所以我们先排个序从最小的那个开始找
一开始总数是n,第一次必定会把当前所有的数都取一遍,所以设答案初始值为n,现在从第一位开始找,设mid=1
每次会找大于等于mid的下标,如果找不到,会返回end()地址(也就是说当前集合中不存在大于等于mid的那个数)
如果找不到了,比如说上一个mid=4,而这个元素总只有1的下标,说明我们已经找过一次当前的所以数了,要把总数加上重新从开头找
每次要删掉当前最小的那个,同时总数减一
***/
[set]Codeforces 830B-Cards Sorting的更多相关文章
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces 830B - Cards Sorting
将每个数字的位置存进该数字的vector中 原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案 树状数组维护每个数字现在的位置和原位置之差 #inc ...
- AC日记——Cards Sorting codeforces 830B
Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- codeforces 830 B Cards Sorting
B. Cards Sorting http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...
- 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 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 830 B. Cards Sorting(线段树)
题目链接:http://codeforces.com/contest/830/problem/B 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...
- 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要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
随机推荐
- 印度IT产业今年裁员5.6万,自动化大潮下安有完卵
[腾讯科技编者按]业界媒体Quatz撰文指出,对于印度科技从业者来说,2017年是噩梦连连的一年.直到几年前,IT业都还是印度提供就业岗位最多的行业之一,但在今年,这个1600亿美元规模的行业裁掉了5 ...
- 安卓注解处理器-processor
最近在学习安卓开源框架发现,很多的开源框架都使用到了注解处理器,例如EventBus3.0.本文通过一个简单的Demo来介绍如何使用注解处理器.Demo链接为https://github.com/cu ...
- 安卓权威编程指南-笔记(第24章 Looper Handler 和 HandlerThread)
AsyncTask是执行后台线程的最简单方式,但它不适用于那些重复且长时间运行的任务. 1. Looper Android中,线程拥有一个消息队列(message queue),使用消息队列的线程叫做 ...
- js数据类型大全
声明变量的命名规范(标识符) 1.不能以数字开头,只能以字母或者¥或者_开头 2.js变量名称区分大小写 3.变量名不能含有关键字(this.if.for.while) 4.驼峰命名法 console ...
- 关于.net MVC中主视图和分部视图的数据共享遇到的问题
今天在开发web时因为调用到的分部视图需要有个隐藏域.然后因为当我们第一次调用分部视图时,是用 @Html.Partial("DetailDataPart")在主视图里把它嵌进去主 ...
- Pom.xml的依赖自动生成
1.第一种用引入jar包的方法 网盘链接:https://pan.baidu.com/s/10HNjNeZc1d5QrFNtvLPWBA 提取码:oako 以上是整个文件直接用idea打开即可 imp ...
- APPium+Python+iOS屏幕滑动方法对比
最近在学习appium自动化,对iOS手机进行滑动操作进行总结: 1.mobile:scroll;该方法在实际使用调用时,会滚动2次.执行时间很长. 向下滚动整个屏幕driver.execute_sc ...
- YAML语法使用,JSR303数据校验
YAML YAML是 "YAML Ain't a Markup Language" (YAML不是一种置标语言)的递归缩写 # yaml配置 server: prot: YAML语 ...
- python入门到放弃-基本数据类型之tuple元组
#概述 元组俗称不可变的列表,又称只读列表,是python的基本数据类型之一, 用()小括号表示,里面使用,逗号隔开 元组里面可以放任何的数据类型的数据,查询可以,循环可以,但是就是不能修改 #先来看 ...
- webpack安装问题
在sf进行安装webpack时候,报错如下: 解决方案:npm install webpack-cli -g,如图上