ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Cards Sorting(树状数组+更具题目的特定优化)

2022-03-06 11:01:54  阅读:186  来源: 互联网

标签:Sorting deck int 树状 number cards ch Cards card


思路:将同样大小的数为一组,这一组的某个特定值的位置,来更新ans。

特定值:last的左边的值的优先级高于右边的值的优先级。

树状数组维护这个区间里面有多少个没有被删的值。

B. 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
4
6 3 1 2
output
7
input
1
1000
output
1
input
7
3 3 3 3 3 3 3
output
7
————————————————
版权声明:本文为CSDN博主「Mitsuha_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Mitsuha_/article/details/78448537
View problem
#include <bits/stdc++.h>
using namespace std;
#define ri register int
#define M 100005

template <class G> void read(G &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
 } 
 
int n;
vector <int> p[M];
int c[M];
int qu(int a)
{
    int res=0;
    while(a>0)
    {
        res+=c[a];
        a-=(a&-a);
    }
    return res;
}
void up(int a,int b)
{
    while(a<=n)
    {
        c[a]+=b;
        a+=(a&-a);
    }
}
int main(){
    
    read(n);
    for(ri i=1;i<=n;i++) 
    {
        int a;read(a);
        p[a].push_back(i);
    }
    int last=0;long long ans=0;
    for(ri i=1;i<=n;i++)
    {
        up(i,1);
    }
    for(ri i=1;i<=100000;i++)
    {
        int m=p[i].size();if(!m) continue;
        int tmp=-1;
        for(ri j=0;j<p[i].size();j++)
        {
            int b=p[i][j];
            if(b<last)
            {
                tmp=b;
            }
        }
        if(tmp==-1)
        {
            ans+=qu(p[i][p[i].size()-1])-qu(last);
            last=p[i][p[i].size()-1];
        }
        else
        {
            ans+=qu(n)-qu(last-1)+qu(tmp); // attention last-1 very important
            last=tmp;
        }
        for(ri j=0;j<p[i].size();j++)
        {
            int b=p[i][j];
            up(b,-1);
        }
    }
    printf("%lld",ans);
    return 0;
}
View Code

 

标签:Sorting,deck,int,树状,number,cards,ch,Cards,card
来源: https://www.cnblogs.com/Lamboofhome/p/15971019.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有