codeforces 830 B Cards Sorting

本文解析了一道Codeforces上的B级题目,该题要求计算一种特定排序操作的次数。通过使用线段树数据结构,文章详细介绍了如何高效地找到并删除序列中的最小元素,直至序列为空。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
}

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/7223505.html

内容概要:《中文大模型基准测评2025年上半年报告》由SuperCLUE团队发布,详细评估了2025年上半年中文大模型的发展状况。报告涵盖了大模型的关键进展、国内外大模型全景图及差距、专项测评基准介绍等。通过SuperCLUE基准,对45个国内外代表性大模型进行了六大任务(数学推理、科学推理、代码生成、智能体Agent、精确指令遵循、幻觉控制)的综合测评。结果显示,海外模型如o3、o4-mini(high)在推理任务上表现突出,而国内模型如Doubao-Seed-1.6-thinking-250715在智能体Agent和幻觉控制任务上表现出色。此外,报告还分析了模型性价比、效能区间分布,并对代表性模型如Doubao-Seed-1.6-thinking-250715、DeepSeek-R1-0528、GLM-4.5等进行了详细介绍。整体来看,国内大模型在特定任务上已接近国际顶尖水平,但在综合推理能力上仍有提升空间。 适用人群:对大模型技术感兴趣的科研人员、工程师、产品经理及投资者。 使用场景及目标:①了解2025年上半年中文大模型的发展现状与趋势;②评估国内外大模型在不同任务上的表现差异;③为技术选型和性能优化提供参考依据。 其他说明:报告提供了详细的测评方法、评分标准及结果分析,确保评估的科学性和公正性。此外,SuperCLUE团队还发布了多个专项测评基准,涵盖多模态、文本、推理等多个领域,为业界提供全面的测评服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值