B. Cards sortingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Vasily has a deck of cards consisting of n cards. There is a integer on each of the cards, this integer is between 1 and inclusive. It's possible that some cards has the same integers on them.
Vasily decided to sort the cards. To does this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on t He cards in the deck and 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 is no cards in the deck. You can assume this Vasily always knows the minimum number written on some card in the remaining deck, but doesn ' t know wh Ere this card (or these cards) is.
You is to determine the total number of times Vasily takes the top card from the deck.
Input
The first line contains a single integer n (1≤ n ≤100)-the number of cards in the deck.
The second line contains a sequence of n integers a1, a2, ..., a n ( 1≤ ai ≤100), where ai is the number Writte N 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.
Examplesinput
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 number6 on it, puts it under the deck and then on the card with number3, 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. The cards from top to bottom is [2, 6, 3]. Then Vasily looks in the top card with number 2 and puts it away. The cards from top to bottom is [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at Card 3 and puts it away. Then there is a one card with number 6 on it, and Vasily looks at it and puts it away. Thus Vasily looks at 7 cards.
Test instructions
There are n marked with numbers of cards, stacked on the table, and now want to give the card sort sorting method is: Every time the top of the card, if he is the rest of the pile of the smallest of the hand to take him out, or put him to the bottom. Ask how many times the total number of cards have been taken out.
Code:
//It can be seen that each time O (n) is thrown away the smallest (there may be multiple identical or different) after the remaining number of card changes +1 (the remaining relative position remains unchanged), until there is no card, this is O (NN). //You can use a tree array to record how many cards are in each position interval segment, and the position of the smallest card currently thrown is incremented (that is, to the right of the position of the previous discarded card)//so the number of +1 cards is only the current minimum card and the position of the card was thrown away from the card, or the interval outside the number of cards +1, and then throw away the cards//Remove from the tree array. O (NLOGN)#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intmaxn=100000;inta[maxn*4+Ten],n;vector<int>v[maxn*4];voidUpdateintIdintv) { while(id<=maxn*4) {A[id]+=v; ID+ = (id& (-id)); }}intQueryintID) { intsum=0; while(ID) {sum+=A[id]; ID-= (id& (-id)); } returnsum;}intMain () {scanf ("%d",&N); memset (A,0,sizeof(A)); for(intI=1; i<=n;i++){ intx; scanf ("%d",&x); V[x].push_back (i); Update (I,1); } intlast=0; ll ans=0; for(intI=1; i<=maxn;i++){ intSize=v[i].size (), tmp=-1; if(size==0)Continue; for(intj=0; j<size;j++){ if(v[i][j]<Last ) TMP=V[i][j]; } if(tmp==-1) {ans+=query (v[i][size-1])-query (last); Last=v[i][size-1]; } Else{ans+=query (MAXN)-(last)-query (TMP)); Last=tmp; } for(intj=0; j<size;j++) Update (V[i][j],-1); } printf ("%lld\n", ans); return 0;}
CF 830b-cards sorting tree-like array