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. 【BZOJ2134】单位错选(数学期望,动态规划)

    [BZOJ2134]单位错选(数学期望,动态规划) 题面 BZOJ 题解 单独考虑相邻的两道题目的概率就好了 没了呀.. #include<iostream> #include<cs ...

  2. 【BZOJ4816】数字表格(莫比乌斯反演)

    [BZOJ4816]数字表格(莫比乌斯反演) 题面 BZOJ 求 \[\prod_{i=1}^n\prod_{j=1}^mf[gcd(i,j)]\] 题解 忽然不知道这个要怎么表示... 就写成这样吧 ...

  3. OpenAI dota2大战人类顶尖选手视频

    AI大战Dendi:http://www.bilibili.com/video/av13267474/?zw#quality=3 AI大战Sumail:http://www.bilibili.com/ ...

  4. 论文笔记(2):Deep Crisp Boundaries: From Boundaries to Higher-level Tasks

    ---------------------------------------------------------------------------------------------------- ...

  5. Session和Cookie总结

    一.Session和Cookie 1.Cookie 1.cookie创建于服务器,保存于浏览器,保存了特定网站操作记录和资料凭证的信息. 2.未设置cookie期限的时候,默认是关闭浏览器后cooki ...

  6. 读 《 Web 研发模式的演变 》与《Javascript:世纪机器语言》

       读了两篇文章,内心还是很震撼的,在这之前,我学习知识都是直接找教程,翻阅资料,写几个小demo,没有去了解我所学的东西的发展历程,<Web研发模式的演变>这篇文章讲述了web的前世今 ...

  7. PetaPoco批量插入数据

    VS添加完组件,自动生成的PetaPoco.cs文件中没有SqlBulkInsert这个方法,但是可以在里面添加,代码如下: /// <summary> /// BulkInsert // ...

  8. spring+springMVC 整合 MongoDB 实现注册登录

    发现一入手 MongoDB,便无法脱离,简要说一下,MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 也是在 Nosql 中我最喜欢的一种 ...

  9. linux的学习之路--(五)bash及其特性

    操作系统组成作用shell是离用户最近的程序 shell:外壳 两类 GUI:Gnome,KDE,Xfce CLI:sh, csh,ksh,bash(都是程序,就是功能支持的不同而已) 进程:在每个进 ...

  10. unix命令

    最近需要用到一些Unix的东西 ,就学习了下这个东西,简单记录下命令,方便以后查询! 1. ls这是最基本的档案指令. ls 的意义为 "list",也就是将某一个目录或是某一个档 ...