Cards Sorting
题目描述
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 $100000$, 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.
题意概述
有$n$张卡牌,第$i$张卡牌上的数字为$a_i$。每次从牌堆顶拿一张牌,如果它上面的数字是当前牌堆中最小的,则将它扔掉,否则将它放到牌堆底。问几次操作后牌堆为空。
数据范围:$1 \le n \le 10^5, \ 1 \le a_i \le 10^5$。
算法分析
先对卡牌上的数进行计数排序,接着从小到大处理:维护一个$now$指针,表示当前走到了第$now$个数;用树状数组维护当前剩余数字个数的前缀和,这样可以快速求出从第$i$个数走到第$j$个数所需要的次数。
代码
1 |
|