[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要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
随机推荐
- [Gem] AASM 狀態機
@(Ruby on Rails)[rails, gem] 1234 # AASM is a continuation of the acts-as-state-machine rails plugin ...
- Hackintosh Of Lenovo R720 15IKBN
Hackintosh Of Qftm 一个黑苹果爱好者的项目 定制:macOS Catalina 10.15.1 电脑配置 一键查看电脑配置(鲁大师.360驱动管理.Lenovo管家等) 规格 详细信 ...
- 达拉草201771010105《面向对象程序设计(java)》第十八周学习总结
达拉草201771010105<面向对象程序设计(java)>第十八周学习总结 实验十八 总复习 实验时间 2018-12-30 1.实验目的与要求 (1) 综合掌握java基本程序结构 ...
- node--fs
1.fs模块内置方法 1)stat 检测是文件还是目录 fs.stat(fileAddress,(err,stats)=>{ //err 出错信息 //stats.isFile() 该东西是文件 ...
- 从HTML标签开始
开始这一切吧! 没错,你没看错,我将从HTML标签开始我的整个系列文章.很基础吧?但是每个前端人都是从最简单的HTML标签开始的,都是从一个<html></html>开始整个前 ...
- 关于毕业五年PHP成长疑惑
1.PHP语法基础是否都会,比如异常捕捉,面向对象,数组操作语法,字符串操作,cookie,session,全局变量,超全局数组,防止sql注入,mysql预处理 2.MYSQL基础语法,字段设计,原 ...
- .Net Core项目中整合Serilog
前言:Serilog是.NET应用程序的诊断日志记录库.它易于设置,具有简洁的API,并且可以在所有最新的.NET平台上运行.尽管即使在最简单的应用程序中它也很有用,但当对复杂的,分布式的和异步的应用 ...
- 一次生产环境搭建11g RAC的记录
一.使用惠普3par工具配置共享存储 该部分可由惠普工作人员协助配置,只需将需求告知即可.如果想自己配置,惠普厂商会发送相关的软件工具以及操作手册给用户. 用putty登陆共享存储,使用showpd ...
- 群ping
找出单位内所有电脑手机 通常情况下,ping只能ping一个IP地址.一个网络值班只有255台电脑,除非是大的网络断,把子网掩码改了,可以扩充更多电脑.如: 如果我们要一次性检查内网所有机器,则可以输 ...
- 爬虫使用中间代理人 fiddl...,charles,mitmproxy 设置
一般的设置在网上就能找到(端口,ip啥的) 但是难点是关于安卓手机证书 在网上找到的几种方法,一种是在app源码中添加设置让手机app同意你下载安装的证书,另一种则是root_adb 安装证书 但是太 ...