• CodeForces–830B--模拟,树状数组||线段树


    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

  • 相关阅读:
    python加速包numba并行计算多线程
    idea Exception in thread "http-apr-8080-exec-2" java.lang.OutOfMemoryError: PermGen space
    centos6.5 导入matplotlib报错 No module named '_tkinter
    pythonTensorFlow实现yolov3训练自己的目标检测探测自定义数据集
    ajax post请求json数据在spring-controller解析
    python keras YOLOv3实现目标检测
    mybatis 插入语句name no find
    python调用百度语音识别接口实时识别
    idea ssm框架搭建
    OpenCVSSDpython目标探测对象检测
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/7392007.html
Copyright © 2020-2023  润新知