Cards Sorting
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print the total number of times Vasily takes the top card from the deck.

Examples
input

Copy
4
6 3 1 2
output

Copy
7
input

Copy
1
1000
output

Copy
1
input

Copy
7
3 3 3 3 3 3 3
output

Copy
7
Note

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的更多相关文章

  1. Codeforces 830B - Cards Sorting 树状数组

    B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. CodeForces 830B - Cards Sorting

    将每个数字的位置存进该数字的vector中 原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案 树状数组维护每个数字现在的位置和原位置之差 #inc ...

  3. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  4. codeforces 830 B Cards Sorting

    B. Cards Sorting  http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...

  5. 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 ...

  6. 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 ...

  7. codeforces 830 B. Cards Sorting(线段树)

    题目链接:http://codeforces.com/contest/830/problem/B 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...

  8. Codeforces Round #424 Div2 E. Cards Sorting

    我只能说真的看不懂题解的做法 我的做法就是线段树维护,毕竟每个数的顺序不变嘛 那么单点维护 区间剩余卡片和最小值 每次知道最小值之后,怎么知道需要修改的位置呢 直接从每种数维护的set找到现在需要修改 ...

  9. Codeforces Round #424 E. Cards Sorting

    题目大意:给你一堆n张牌(数字可以相同),你只能从上面取牌,如果是当前牌堆里面最小的值则拿走, 否则放到底部,问你一共要操作多少次. 思路:讲不清楚,具体看代码.. #include<bits/ ...

  10. 【Splay】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting

    Splay要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...

随机推荐

  1. OpenGL Panorama Player

    JMGL_PANO star_war_eve source 1 star_war_eve source 2 1. 介绍 JMGL_PANO 是Justin开源的一个全景视频播放器(Github).基于 ...

  2. XX系统测试总结报告

    XX系统测试总结报告 1        引言 1.1  编写目的 编写该测试总结报告主要有以下几个目的 1.  通过对测试结果的分析,得到对软件质量的评价 2.   分析测试的过程,产品,资源,信息, ...

  3. 烘焙ID贴图

    ID贴图(ID Map)的作用主要就是用来区分同一个模型中不同的区块,具体的用法查看此文.下面介绍几种不同的方式来烘焙ID贴图,用到的工具分别是Blender和Substance Painter. 在 ...

  4. React Docs(1)

    安装 React在codepen上提供了一个Hello,World项目事例,只需打开网站,即可尝试React.另外还提供了一个html文件的Hello,World项目,项目中引用CDN的react.j ...

  5. JMeter接口测试-计数器

    前言 在测试注册接口的时候,需要批量注册账号时,每注册一个并且需要随时去修改数据,比较繁琐,除了使用随机函数生成账号,我们还可以使用计数器来进行批量注册. 一:添加配置元件-计数器 二:注册10个账号 ...

  6. percona-toolkit 之 【pt-query-digest】介绍

    背景: 做为一个MySQL DBA,分析慢查询是日常主要的工作之一,之前一直使用mysqlsla作为分析慢查询的, 因为简单并且也能满足自己对慢查询分析的要求,对于另一个工具pt-query-dige ...

  7. 自定义 ---UICollectionViewLayout-正N变形居中布局

    1. 自定义UICollectionLayout ---- 正三角形居中布局 支持多个图形的自动布局 2. 自定义UICollectionLayout ---- 正方形居中布局 滚动展示的区域 3.  ...

  8. 在Shadow DOM使用原生模板

    原生模板的优势 延迟了资源加载 延迟了加载和处理模板所引用的资源的时机,这样,用户就能够在模板中使用任意多的资源,却不阻碍页面的渲染. 延迟了渲染内容 无论模板在什么位置,浏览器不会把模板中的内容直接 ...

  9. Java基础--数组的定义

    1.数组的定义 数组:一组能够储存相同数据类型值的变量的集合. 2.数组的赋值方式 (1)使用默认的初始值来初始化数组中的每一个元素 语法:数组元素类型[]数组名 = new数组元素类型[数组中元素的 ...

  10. new Date在IE下面兼容问题

    昨天碰到一个bug,用art-template模板进行渲染时候,周视图任务展示失败,都是暂无任务,我以为是模板兼容问题,但最开始我用的时候记得就是IE8的兼容性问题,引入es5-shim.min.js ...