Bubble Sort (5775)

本文探讨了一个特定问题:给定一个1到n的排列,使用冒泡排序时,每个元素能移动到的最左位置和最右位置的差值。文章提供了两种不同的C++实现方案,并解释了如何利用树状数组来高效地解决问题。

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

  Bubble Sort

Problem Description
  P is a permutation of the integers from 1 to N(index starting from 1).
Here is the code of Bubble Sort in C++.

for(int i=1;i<=N;++i)
for(int j=N,t;j>i;—j)
if(P[j-1] > P[j])
t=P[j],P[j]=P[j-1],P[j-1]=t;

  After the sort, the array is in increasing order. ?? wants to know the absolute
values of difference of rightmost place and leftmost place for every number it reached.
 
Input
   The first line of the input gives the number of test cases T; T test cases follow.
   Each consists of one line with one integer N, followed by another line with a
permutation of the integers from 1 to N, inclusive.

limits
T <= 20
1 <= N <= 100000
N is larger than 10000 in only one case. 
 
Output
    For each test case output “Case #x: y1 y2 … yN” (without quotes), where x is
the test case number (starting from 1), and yi is the difference of rightmost place
and leftmost place of number i.
 

Sample Input

2
3
3 1 2
3
1 2 3
 

 

Sample Output
Case #1: 1 1 2
Case #2: 0 0 0
 
Hint
In first case, (3, 1, 2) -> (3, 1, 2) -> (1, 3, 2) -> (1, 2, 3)
the leftmost place and rightmost place of 1 is 1 and 2, 2 is 2 and 3, 3 is 1 and 3
In second case, the array has already in increasing order. So the answer of every number is 0.

 题意:

   给你一个1到n的排列,然后按照冒泡排序的移动方式,问每个i 能移动到的最左位置和最右位置的差是多少

 

   分析:在冒泡排序过程的变化情况。c会被其后面比c小的数字各交换一次,之后c就会只向前移动。

数组从右向左扫,树状数组维护一下得到每个值左边有多少个比其小的值通过转换得到每个值右边有多

少个比其小的值,加上原位置得到最右位置,最左位置为初始位置和最终位置的最小值。

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 100005;
int n;
int a[maxn], b[maxn],c[maxn],C[maxn];
void add(int x){
    while(x<=n) C[x]++,x+=(x&-x);
        //cout<<"x: "<<x<<"  C[x]: "<<C[x]<<endl,
}

int sum(int x){
    int ans=0;
    while(x>0)
        ans+=C[x],x-=(x&-x);
        //cout<<"ans: "<<ans<<endl;
    return ans;
}



int main()
{
    int t;
    int kase = 0;
    scanf("%d", &t);
    while(t--)
    {
     memset(c, 0, sizeof(c));
        memset(b, 0, sizeof(b));
        int i;
        scanf("%d", &n);
        for(i=1; i<=n; i++ )
        {
            scanf("%d", &a[i]);
            b[a[i]] = i;
           }
        memset(C,0,sizeof(C));
        for(int i=1;i<=n;i++){
            c[i]=sum(a[i]-1);
            add(a[i]);
           // cout<<c[i]<<".."<<endl;
        }

        sort(a+1,a+n+1);
        printf("Case #%d:", ++kase);
        for(i = 1; i <= n; i++ )
        {
            int x=i-c[b[i]]-1;
              printf(" %d", max(abs(b[i]+x-b[i]),abs(b[i]+x-i)));

        }
        printf("\n");
    }

    return 0;
}
//自己的绕了点弯路
View Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=101000;
int C[maxn],a[maxn],id[maxn],l[maxn],r[maxn],n;

void add(int x){
    while(x<=n)

        {
             C[x]++,x+=(x&-x);
             }
}

int sum(int x){
    int ans=0;
    while(x>0)
        ans+=C[x],x-=(x&-x);
    return ans;
}

int main(){
    int _;
    scanf("%d",&_);
    for(int case1=1;case1<=_;case1++){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),l[a[i]]=i;
        memset(C,0,sizeof(C));
        for(int i=n;i>=1;i--){
            r[a[i]]=i+sum(a[i]-1);
            add(a[i]);
        }
        printf("Case #%d: ",case1);
        for(int i=1;i<=n;i++){
            if(i!=n)
                printf("%d ",r[i]-min(l[i],i));
            else
                printf("%d\n",r[i]-min(l[i],i));
        }
    }
    return 0;
}
//网上的
View Code

 

 

转载于:https://www.cnblogs.com/fenhong/p/5717453.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、付费专栏及课程。

余额充值