---
title: Cards Sorting
tags:
  - Binary Indexed Tree
  - Implementation
id: "1200"
categories:
  - [OI, Common Skill]
  - [OI, Data Structure]
date: 2017-07-17 08:30:21
---

## 题目描述

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$个数所需要的次数。

## 代码

```cpp
#include <iostream>
#include <vector>
using namespace std;
struct binary_indexed_tree {
  long long n; vector<long long> a;
  void init(long long t) { n = t + 10, a.clear(), a.resize(n); }
  void add(long long p, long long t) {
    for (long long i = p; i < n; i += i & -i) a[i] += t;
  }
  long long sum(long long p) {
    long long ret = 0;
    for (long long i = p; i; i -= i & -i) ret += a[i];
    return ret;
  }
} tree;
long long n, t, now, ans, pre;
vector<long long> b[100001];
int main()
{
  cin >> n;
  tree.init(n);
  for (long long i = 1; i <= n; ++i) {
    cin >> t; tree.add(i, 1), b[t].push_back(i);
  }
  for (long long i = 1; i <= 100000; ++i) {
    if (!b[i].size()) continue; long long pnt = 0;
    while (pnt < b[i].size() && b[i][pnt] < now) ++pnt; --pnt;
    if (pnt < 0) pnt = b[i].size() - 1;
    if (b[i][pnt] > now) ans += tree.sum(b[i][pnt]) - tree.sum(now);
    else ans += tree.sum(n) - tree.sum(now) + tree.sum(b[i][pnt]);
    for (long long j = 0; j < b[i].size(); ++j) tree.add(b[i][j], -1);
    now = b[i][pnt];
  }
  cout << ans << endl;
  return 0;
}
```
