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
4
6 3 1 2
output
7
input
1
1000
output
1
input
7
3 3 3 3 3 3 3
output
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、可以将数整体向左移,

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

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

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

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

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

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

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

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

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

  6. Codeforces Round #424 Div2 E. Cards Sorting

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

  7. Codeforces Round #424 E. Cards Sorting

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

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

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

  9. CodeForces 830B - Cards Sorting

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

随机推荐

  1. Hadoop 安装流程

    前言:因项目中需要数据分析,因而使用hadoop集群通过离线的方式分析数据 参考着网上的分享的文章实施整合的一篇文章,实施记录 安装流程: 1.设置各个机器建的ssh 无密码登陆 2.安装JDK 3. ...

  2. wget命令企业级应用参数详解

    wget -O /etc/yum.repos.d/CentOS-Base.repo --spider: 爬虫,检查网站是不是好的 -T: 指定超时时间 --tries=2  指定重试的次数 -q   ...

  3. Filter的过滤链理解

    一.Filter过滤链 web.xml配置了filter过滤器,在容器启动的时候执行了init()方法进行了初始化,然后在容器关闭的时候执行了destroy()方法销毁过滤器,在每次服务器接受请求的时 ...

  4. onmouse事件

    常用的鼠标事件:onmouseenter,onmouseleave,onmouseover,onmouseout,onmouseup,onmousedown,onmousewheel,onmousem ...

  5. Redis进阶实践之十八 使用管道模式加速Redis查询

    一.引言             学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中批量的增加数据,肯定是大批量的,为了这主题,我从新找起了解决方案.目前 ...

  6. DaTaX当成jar包当作第三方库启动的相关问题

    上一篇已经大致的将了本地状况下DaTaX的纯Java代码启动的过程 http://www.cnblogs.com/blogsofmy/p/8287637.html不了解的请点超链接 这次我们来说说文件 ...

  7. jq事件

    1,ready:当DOM载入就绪可以查询及操纵时绑定一个要执行的函数,在使用之前必须确保body元素的onload事件,,没有注册函数,否则不会触发ready函数. $(document).ready ...

  8. 进入PE后不显示硬盘的解决办法

    其实我很早之前就知道这个方法了,我虽然不知道原因,不过我是一个一个试出来的,转过来备忘, 内容介绍:经常使用PE的朋友相信都遇到过这样的问题,一些新购买的电脑可以正常把PE系统安装到U盘中,也可以正常 ...

  9. 设计模式——策略模式(C++实现)

    程序优化是用于消除程序中大量的if else这种判断语句 #include <iostream> #include <string> using namespace std; ...

  10. Java 英文面试题

    1. Q: What is HashMap and Map?A: Map is Interface and Hashmap is class that implements that. 2. Q: D ...