zoukankan      html  css  js  c++  java
  • codeforces 830 B 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 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
    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 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.

    题意:

    n个数,

    1、可以将数整体向左移,

    2、如果最左边的数在剩余的数中最小,可以删去最左边的数

    两个操作的花费都是1,问将所有的数删去所需要的代价

    线段树

    记录当前序列的最右端R在哪儿

    R左边的数表示实际移到了后面,R右边的数实际在前面

    移动带来的线段树中节点大小的改变,通过记录节点大小解决

    每次查询在R前面查一次,在R后面查一次

    注意如果前面后面的值相等,选R后面的

    #include<cstdio>
    #include<algorithm>
    #define N 100001
    
    #ifdef WIN32
    #define LL "%I64d"
    #else
    #define LL "%lld"
    #endif
    
    using namespace std;
    int n,R,tmp1,tmp2,opl,opr;
    long long ans;
    int sum[N<<2],minn[N<<2],pos[N<<2],mid[N<<2],a[N];
    void up(int k)
    {
        sum[k]=sum[k<<1]+sum[k<<1|1];
        minn[k]=min(minn[k<<1],minn[k<<1|1]);
        if(minn[k]==minn[k<<1]) pos[k]=pos[k<<1];
        else pos[k]=pos[k<<1|1];
    }
    void build(int k,int l,int r)
    {
        if(l==r) 
        {
            scanf("%d",&a[l]);
            minn[k]=a[l]; sum[k]=1; pos[k]=l;
            return;
        }
        mid[k]=l+r>>1;
        build(k<<1,l,mid[k]);
        build(k<<1|1,mid[k]+1,r);
        up(k);
    }
    int find_minn(int k,int l,int r)
    {
        if(l>=opl && r<=opr) return pos[k];
        if(opr<=mid[k]) return find_minn(k<<1,l,mid[k]);
        else if(opl>mid[k]) return find_minn(k<<1|1,mid[k]+1,r);
        {
            int t1=find_minn(k<<1,l,mid[k]);
            int t2=find_minn(k<<1|1,mid[k]+1,r);
            if(a[t1]<=a[t2]) return t1;
            return t2;
        }
    }
    int query(int k,int l,int r)
    {
        if(l>=opl && r<=opr) return sum[k];
        if(opr<=mid[k]) return query(k<<1,l,mid[k]);
        else if(opl>mid[k]) return query(k<<1|1,mid[k]+1,r);
        return query(k<<1,l,mid[k])+query(k<<1|1,mid[k]+1,r);
    }
    void delet(int k,int l,int r)
    {
        if(l==r)
        {
            sum[k]=0; pos[k]=l;
            minn[k]=a[l]=N+5;
            return;
        }
        if(opl<=mid[k]) delet(k<<1,l,mid[k]);
        else delet(k<<1|1,mid[k]+1,r);
        up(k);
    }
    int main()
    {
        scanf("%d",&n);
        build(1,1,n);
        R=n+1;
        for(int i=1;i<=n;i++)
        {
            tmp1=tmp2=0;
            if(R)
            {
                opl=1; opr=R-1; 
                tmp1=find_minn(1,1,n);
            }
            if(R<n)
            {
                opl=R+1; opr=n;
                tmp2=find_minn(1,1,n);
            }
            if( (tmp1 && tmp2 && a[tmp2]<a[tmp1]) || (tmp2 && !tmp1) )    
            {
                if(tmp2>R) opl=R+1,opr=tmp2,ans+=query(1,1,n);
                else 
                {
                    if(R<n) opl=R+1,opr=n,ans+=query(1,1,n);
                    opl=1,opr=tmp2,ans+=query(1,1,n);
                }    
                opl=tmp2; delet(1,1,n); R=tmp2;
            }
            else if(tmp1 && tmp2 && a[tmp1]==a[tmp2]) 
            {
                opl=R+1,opr=tmp2,ans+=query(1,1,n);
                opl=tmp2;delet(1,1,n);  R=tmp2;
            }
            else 
            {
                if(tmp1>R) opl=R+1,opr=tmp1,ans+=query(1,1,n);
                else 
                { 
                    if(R<n) opl=R+1,opr=n,ans+=query(1,1,n);
                    opl=1,opr=tmp1,ans+=query(1,1,n);
                }
                opl=tmp1; delet(1,1,n); R=tmp1;
            }
        }
        printf(LL,ans);
    }
  • 相关阅读:
    linux 命令汇总
    vue搭建环境并创建项目
    CentOS中利用Docker安装RabbitMQ
    CentOS中利用Docker安装Redis
    CentOS双机中Docker下安装Mysql并配置互为主从模式
    CentOS下挂载数据盘
    bootstrap 自适应和响应式布局的区别
    xampp lampp 改变网页root目录的方法
    vue 自适应 Responsive 设计
    vue的二维码生成器
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7223505.html
Copyright © 2011-2022 走看看