Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)

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

46 3 1 2

output

7

input

11000

output

1

input

73 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.

【题意】从上往下遍历所有的卡片。如果当前卡片上的数字跟当前堆中的最小值相等,则将该卡片扔点,否则将该卡片放到最低端,问一共遍历多少次。

【分析】模拟一遍,从最小的开始找,二分出可以衔接上一次位置的当前起始位置,遍历到此轮最后一个地方,再遍历前面没有遍历的地方,用树状数组来统计。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N = 1e5+50;
int n;
int sum[N];
vector<int>vec[N];
void upd(int x,int add){
   for(int i=x;i<=100000;i+=i&(-i)){
       sum[i]+=add;
   }
}
int qry(int x){
    int ret=0;
    for(int i=x;i>=1;i-=i&(-i)){
        ret+=sum[i];
    }
    return ret;
}
int dis(int l,int r){
    if(l==-1)return qry(r);
    else if(l<r)return qry(r)-qry(l);
    else return qry(n)-qry(l)+qry(r);
}
int main(){
    scanf("%d",&n);
    for(int i=1,x;i<=n;i++){
        scanf("%d",&x);
        upd(i,1);
        vec[x].pb(i);
    }
    LL woqunimalegebi = 0;
    int pre=-1;
    for(int i=1;i<=100000;i++){
        if(!vec[i].size())continue;
        int pos=upper_bound(vec[i].begin(),vec[i].end(),pre)-vec[i].begin();
        for(int j=max(0,pos);j<vec[i].size();j++){
            woqunimalegebi+=dis(pre,vec[i][j]);
            pre=vec[i][j];
            upd(pre,-1);
        }
        for(int j=0;j<pos;j++){
            woqunimalegebi+=dis(pre,vec[i][j]);
            pre=vec[i][j];
            upd(pre,-1);
        }
    }
    printf("%lld\n",woqunimalegebi);
    return 0;
}
时间: 2024-10-20 09:52:45

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)的相关文章

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

D题fst了,生无可恋.第二场rated的CF,打得精神恍惚 A. Unimodal Array 题意:判断数列是否是单峰的. 像题意那样分为三个阶段随便判一判就好了 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; int n,x[105],part=1; bool f=1; int main() { scanf(&quo

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力

Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is a

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebo

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)E. Cards Sorting

题意:有n个数字,我遍历过去,如果他是当前最小值,就删除,否则放到最后面去,问得遍历多少个数字,(直到所有数字消失 思路:我们保存每个数字的位置,这个数字是否能删除,如果他在上一个数字的最后一个位置后面就可以删除了,那么标记下+树状数组(我这里的y表示的就是上一个数删除的最后一个位置) 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=1e5+10; 5 6 int a[

【Splay】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting

Splay要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护子树大小的函数. 找到位置以后,直接将左右子树交换即可.不需要打标记. 删除节点时,直接将其前驱(是指序列下标的前驱,就是将待删除节点Splay到根后,左子树的最右节点)Splay到根,将其后继(类似)Splay到根的儿子. 然后将后继的左儿子删除即可. 别忘了及时Maintain(); 这份代码的

【推导】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) A. Office Keys

选择的钥匙一定是连续的,人和钥匙一定从左到右连续对应. 就枚举钥匙区间即可. #include<cstdio> #include<algorithm> using namespace std; int Abs(int x){ return x<0 ? (-x) : x; } int n,K,p,a[1010],ans=2147483647,b[2010]; int main(){ scanf("%d%d%d",&n,&K,&p);

A. Office Keys (from Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) )

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 7 int a[150000]; 8 int b[150000]; 9 int dp[1005][1005]; 10 //dp[i][j] 前i个人从前j个药匙中到达终点的最小时间 11 12 int main() 13 { 14 in

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seate

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)D. High Load

题意:出n个点,其中k个叶子节点,问构造出的树最远的两个点最近是多少 思路:以一个点为中心,然后m个伸出,一层层扩散,(n-1)%m==k,如果k==0,即可以平分,长度就是2*(n-1)/m,如果取模为k==1,说明多出一个,+1,其他的话,就是最后一层补k个,但是最长的还是+2 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main(){ 5 int n,m; 6 cin>>n>>m; 7 int