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.


数据范围:$1 \le n \le 10^5, \; 1 \le a_i \le 10^5$。




#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;
  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;

RegMs If

418 I'm a teapot

Leave a Reply

Your email address will not be published. Required fields are marked *