[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要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
随机推荐
- Python知识点汇总
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- Haproxy 使用block 阻止域名访问到某个子目录报403
配置教程如下: acl is_https_com hdr_beg(host) www.baidu.com #定义规则域名 acl api_block_url_web url_dir -i /web/ ...
- Web渗透基础小总结
Web渗透框架概述 主要组成: 1. web语言代码(脚本) 2. web程序 3. 数据库程序 Web语言常见几大类 1. HTML:超文本标记语言,标准通用编辑语言下的一个应用 2. PHP:超文 ...
- 攻防世界Mobile5 EasyJNI 安卓逆向CTF
EasyJNI 最近正好在出写JNI,正好看到了一道JNI相关的较为简单明了的CTF,就一时兴起的写了,不得不说逆向工程和正向开发确实是可以互补互相加深的 JNI JNI(Java Native In ...
- UIView绘制原理,异步绘制
绘制原理 首先看一幅流程图 UIView调用setNeedsDisplay方法后,实际上并没有发生当前视图的绘制工作,而是在之后的某一时机进行绘制工作,为什么会在之后的某一时机进行绘制工作呢? 当UI ...
- Fedora CoreOS 非LInux专业安装文章第一手
开篇一张图 Docker基本知识掌握后,又学习了"专有的系统平台",CoreOS; 之前一直Windows,学习Docker的同时练习了好多Linux知识,全是江湖路数,打个不同就 ...
- 前端实现html转pdf方法总结
最近要搞前端html转pdf的功能.折腾了两天,略有所收,踩了一些坑,所以做些记录,为后来的兄弟做些提示,也算是回馈社区.经过一番调(sou)研(suo)发现html导出pdf一般有这几种方式,各有各 ...
- vue+element 表单封成组件(2)
今天我们继续把时间选择器,多选框和单选框加上 父组件(在昨天的基础上增加): <template> <el-form :model="ruleForm" ref= ...
- PHP的json_encode和json_decode的区别
经常搞混的两个PHP函数: json_encode()是对变量进行json编码 json_encode()为要编码的值,且该函数只对utf8编码的数据有效 json_decode($json)对jso ...
- 【图文+视频新手也友好】Java一维数组详细讲解(内含练习题答案+详解彩蛋喔~)
目录 视频讲解: 一.数组的概述 二.一维数组的使用 三.Arrays工具类中的sort方法(sort方法用的多,我们具体讲一下) 四.数组中的常见异常 五.一维数组练习题 六.彩蛋(本期视频使用的P ...