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

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.

参考自:http://www.cnblogs.com/yyf0309/p/7171184.html

题目大意 桌面上有一叠卡牌,每张卡牌上有些数。当拿起一张卡牌时,如果它上面的数是当前剩下的牌中的数(包括拿起的这一张)的最小值(假定这个人知道),就把它"扔掉",否则把它塞进这叠卡牌的最下面。问把所有卡牌都拿走花了多少次。

  我很好奇,是老外的数据结构弱吗,每次数据结构题基本都是E,F题(div 2。div 1我就不知道了)。

  然后不说多的废话了。树状数组维护卡牌是否被拿走(拿走为0,未拿走为1),线段树维护区间最小值。

  因为移动的话很麻烦,但是一轮后顺序又正常了,所以可以一轮一轮地计算。记录fin(已经拿走的牌的数量)和last(上个拿走的卡牌的下标)。

  ->当fin等于n时停止

    ->将last设为1

    ->找到last右侧的最小值和整叠卡牌的最小值,判断他们是否相等。

      *如果相等

        ->返回最小的最小值下标pos,然后树状数组统计last + 1到pos的和(这中间还有多少没拿走),更新答案。

        ->将树状数组这一位置为0,线段树中这一位改为inf(不方便delete,就这么干),fin 加 1,将last设为pos。

      *如果不相等

        ->统计last + 1到n的和(把这一轮中剩下的牌都拿完)

        ->break

CodeForces–830B--模拟,树状数组||线段树的更多相关文章

  1. 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树

    正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...

  2. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  3. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  4. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  6. [CSP-S模拟测试]:影魔(树状数组+线段树合并)

    题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...

  7. hdu 1166 敌兵布阵——(区间和)树状数组/线段树

    pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...

  8. 数据结构--树状数组&&线段树--基本操作

    随笔目的:方便以后对树状数组(BIT)以及基本线段树的回顾 例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 例题:hdu 1166 敌兵布阵 T ...

  9. BZOJ_1901_&_ZJU_2112_Dynamic_Rankings_(主席树+树状数组/线段树+(Treap/Splay))

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1901 给出一个长度为n的数列A,有m次询问,询问分两种:1.修改某一位置的值;2.求区间[l, ...

随机推荐

  1. 怎样测试nginx.conf配置文件的正确性

    方法: 使用 nginx -t 命令  nginx -t 如果一切正常, 则会显示:

  2. 几个主流浏览器 Window.open打开新窗口 、模拟a标签打开新窗口的 表现

    Window.open打开新窗口 1.常用浏览器打开新窗口(正常打开window.open)的的不同表现形式(PC/移动端) 2.Window.open在异步处理中打开(_blank) a标签在异步处 ...

  3. 日志(logging)与正则(re)模块

    logging模块 #日志:日常的流水 =>日志文件,将程序运行过程中的状态或数据进行记录,一般都是记录到日志文件中 #1.logging模块一共分为五个打印级别 debug.info.warn ...

  4. list 字符串拼接效率实验

    ist 字符串拼接有多种方法,我就其中常用三种做了实验,实验代码如下: 第一次是为了初始化静态方法,后面的才是效率比较. 结果如下: StringUtils join 方法用的是StringBuild ...

  5. Webstorm2017.3.3软件的安装使用

    下载 ▶进入jetbrains的官方网站点击download,即下载开始.官方网站链接:http://www.jetbrains.com/webstorm/ 安装 ▶双击刚下载完成的.exe文件开始进 ...

  6. DA_03_linux网络配置及其远程连接

    一: 修改Linux的基本配置 直接运行:setup,根据提示修改 1.修改主机名:vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=shizhan1 ...

  7. linux下搭建redis内网端口映射工具-rinetd

    最近在工作中使用到了redis缓存,用来提某些业务场景的计算速度和某些情况下的操作频率限制,客户端工具使用了redisDesktopmanager来管理,在本机操作测试的时候,是没有问题的.但是将系统 ...

  8. org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]

    错误: 在 IDEA 创建WEB项目之后,打印出的日志中总是出现一行警告信息: 12-May-2018 15:52:30.692 警告 [RMI TCP Connection(3)-127.0.0.1 ...

  9. 异步函数Demo

    private static async Task<String> IssueClientRequestAsync(string serverName, string message) { ...

  10. tnsnames.ora配置细节注意

    WXPT = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.250)(PORT = 1521) ...