nyoj322 sort 归并排序,树状数组

Sort

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
输入
The input consists of T number of test cases.(<0T<1000) Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.
输出
For each case, output the minimum times need to sort it in ascending order on a single line.
样例输入
2
3
1 2 3
4
4 3 2 1
样例输出
0
6

如果按冒泡排序这些O(n^2)肯定会超时,所以需要找一种更快的方法 --------归并排序。

归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。这题还可以用树状数组来做

法一:用归并排序做
#include<stdio.h>
int a[1000001],b[1000001]; /*合并排序结果先保存到b中*/
int merge(int a[],int low,int mid,int high)
{
    int i=low,j=mid+1,k=low;
    int count=0;/*计数器*/ 
    while((i<=mid)&&(j<=high))/*部分合并*/
    {
        if(a[i]<=a[j])
        {
            b[k++]=a[i++];
        }
        else
        {
            b[k++]=a[j++];
            count+=(mid-i+1);
        }
    }
    while(i<=mid)/*转储剩余部分*/
    {
        b[k++]=a[i++];
    }
    while(j<=high)
    {
        b[k++]=a[j++];
        
    }
    for(i=low;i<=high;++i)/*把b中的值复制给a*/
    {
        a[i]=b[i];
    }
    return count;
}
int sort(int a[],int low,int high)
{
    int x,y,z;
    int mid=(high+low)/2;
    int i=low,j=mid+1;
    if(low>=high)
    {
        return 0;
    }
    x=sort(a,low,mid);
    y=sort(a,mid+1,high);
    z=merge(a,low,mid,high);
    return (x+y+z);
}
int main()
{
    int ncases,n,i;
    scanf("%d",&ncases);
    while(ncases--)
    {
        scanf("%d",&n);
        for(i=0;i<=n-1;i++)
        {
            scanf("%d",&a[i]);
        }
        printf("%d\n",sort(a,0,n-1));
    }
    return 0;
}
 
   

 法二:用树状数组

 不知道什么是树状数组的  就先看看树状数组吧。。链接:http://dongxicheng.org/structure/binary_indexed_tree/

#include<stdio.h>
#include<string.h>
int num[1004],n;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x)
//更新含有x的数组个数
{
    while(x<=n)
    {
        num[x]++;
        x+=lowbit(x);
    }
}
int sum(int x)
//向下统计小于x的个数
{
    int total=0;
    while(x>0)
    {
        total+=num[x];
        x-=lowbit(x);
    }
    return total;
}
int main()
{
    int x,cases;
    scanf("%d",&cases);
    while(cases--)
    {
        scanf( "%d",&n);
        memset( num,0,sizeof( num ));
        int ss = 0;
        for( int i = 0; i < n; ++i )
        {
            scanf( "%d",&x);
            add(x);
            ss += (i-sum( x - 1 ));
        }
        printf( "%d\n",ss );
    }
    return 0;
}

 

 
   

 

转载于:https://www.cnblogs.com/lovychen/p/3238090.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
孪生素数是指两个素数之间的差值为2的素数对。通过筛选法可以找出给定素数范围内的所有孪生素数的组数。 在引用的代码中,使用了递归筛选法来解决孪生素数问题。该程序首先使用循环将素数的倍数标记为非素数,然后再遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 具体实现过程如下: 1. 定义一个数组a[N,用来标记数字是否为素数,其中N为素数范围的上限。 2. 初始化数组a,将0和1标记为非素数。 3. 输入要查询的孪生素数的个数n。 4. 循环n次,每次读入一个要查询的素数范围num。 5. 使用两层循环,外层循环从2遍历到num/2,内层循环从i的平方开始,将素数的倍数标记为非素数。 6. 再次循环遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 7. 输出总数。 至此,我们可以使用这个筛选法的程序来解决孪生素数问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python用递归筛选法求N以内的孪生质数(孪生素数)](https://blog.csdn.net/weixin_39734646/article/details/110990629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [NYOJ-26 孪生素数问题](https://blog.csdn.net/memoryofyck/article/details/52059096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值