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. 【洛谷1131】【ZJOI2007】时态同步

    题面 题目描述 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3-.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两 ...

  2. [Shoi2007]Vote 善意的投票

    题目描述 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来 ...

  3. chkconfig命令核心案列及核心原理

    chkconfig sshd on  设置sshd开机自启动 chkconfig sshd off  设置sshd开机不启动 chkconfig --level 35 sshd on    设置ssh ...

  4. sqlserver中压缩日志文件

    最近在转移数据,sqlserver的日志文件ldf,占用空间特别大,为了还原库,节省空间,所以压缩日志文件迫在眉睫.在网上找了一段代码: USE [master] GO ALTER DATABASE ...

  5. C++学习-5

    1.static_cast静态转换<>要转换的类型,不适用于指针转换 reinterpret_cast<char*>()指针类型的转换 涉及到const,必须用const_ca ...

  6. 属性动画 ValueAnimator 运行原理全解析

    最近下班时间都用来健身还有看书了,博客被晾了一段时间了,原谅我~~~~ 提问环节 好,废话不多说,之前我们已经分析过 View 动画 Animation 运行原理解析,那么这次就来学习下属性动画的运行 ...

  7. 直播-rtmp学习

    RTMP(实时消息传输协议),官方介绍如下: Adobe’s Real Time Messaging Protocol (RTMP), an application-level protocol de ...

  8. shell中的数字

    shell中的数字 author :headsen chen date :2017-10-18  15:01:42 个人原创,转载请注明作者,出处,否则依法追究法律责任 1,生成随机数(范围:0-32 ...

  9. ubuntu14.04行更新软件包

    ubuntu14.04行更新软件包 headsen  chen   2017-10-12 16:01:34 apt-get update对应的就是第一步. apt-get upgrade 与apt-g ...

  10. WBS

    Need 需求分析: 为了满足中老年人因工作忙碌而无暇阅读的痛苦,我们设计推广出一款听书软件.可以给中老年人带来的好处是不再受繁琐的听书软件的束缚,操作简单,携带便捷. Approach 实现方法: ...