原题poj 2299:http://poj.org/problem?id=2299

题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归并排序~~~

这里列出归并的解法:

#include"iostream"
using namespace std; const int maxn=500000+10; int T[maxn];
int a[maxn]; long long sum; void merge_sort(int *a,int x,int y,int *T)
{
if(y-x>1)
{
int m=x+(y-x)/2;
int p=x,q=m,i=x;
merge_sort(a,x,m,T);
merge_sort(a,m,y,T);
while(p<m||q<y)
{
if(q>=y||(p<m&&a[p]<a[q])) T[i++]=a[p++];
else {sum+=m-p;T[i++]=a[q++];}
}
for(int i=x;i<y;i++) a[i]=T[i];
}
}
int main()
{
int n;
while(cin>>n&&n)
{
sum=0;
for(int i=0;i<n;i++) cin>>a[i];
merge_sort(a,0,n,T);
/* for(int i=0;i<n;i++) cout<<a[i]<<' ';
cout<<endl;*/
cout<<sum<<endl;
}
return 0;
} 贴出一段代码,请找出其中的错误并改正:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=5e5+;
int a[maxn],n;
int t[maxn];
long long ans;
void mergesort(int c[],int b,int e)
{
if(e-b>)
{
int p=(b+e)>>;
int mid=p,begi=b;
mergesort(c,b,p);
mergesort(c,p,e); int i=; while(mid<e||begi<p)
{
if(mid>=e||(begi<p&&(c[begi]<=c[mid]))) t[i++]=c[begi++];
else {ans+=p-begi;t[i++]=c[mid++];}
} i=;
for(int j=b;j<=e;j++) c[j]=t[i++];
}
} int main()
{
while(scanf("%d",&n),n!=)
{
for(int i=;i<=n;i++) scanf("%d",&a[i]); ans=; mergesort(a,,n+); printf("%d\n",ans); }
return ;
}

改正后:

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=5e5+;
int a[maxn],n;
int t[maxn];
long long ans;
void mergesort(int c[],int b,int e)
{
if(e-b>)
{
int p=(b+e)>>;
int mid=p,begi=b;
mergesort(c,b,p);
mergesort(c,p,e); int i=; while(mid<e||begi<p)
{
if(mid>=e||(begi<p&&(c[begi]<c[mid]))) t[i++]=c[begi++]; /*必须严格递增,等于也算是逆序数,因为a[begi]后面的数肯定都会大于a[mid]*/
else {ans+=p-begi;t[i++]=c[mid++];}
} i=;
for(int j=b;j<e;j++) c[j]=t[i++]; //j不会到e正如begi不会到mid
}
} int main()
{
while(scanf("%d",&n),n!=)
{
for(int i=;i<=n;i++) scanf("%d",&a[i]); ans=; mergesort(a,,n+); printf("%lld\n",ans); //输出长整型必须使用%lld或者%I64d }
return ;
}
 

集训第四周(高效算法设计)A题 Ultra-QuickSort的更多相关文章

  1. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  2. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  3. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  4. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  5. 集训第四周(高效算法设计)I题 (贪心)

    Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...

  6. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

  7. 集训第四周(高效算法设计)D题 (区间覆盖问题)

    原题 UVA10020  :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...

  8. 集训第四周(高效算法设计)L题 (背包贪心)

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

  9. 集训第四周(高效算法设计)K题 (滑窗问题)

    UVA 11572 唯一的雪花 题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新 方法一: #include& ...

随机推荐

  1. 关于数学函数中的abs——————————————杭电2057——————————————————————————

    数学函数中的abs当你用abs之后括号之中的数字就转换成了int格式.可能会丢失一些数据造成误差而且还会有,    警告: #include<stdio.h> #include<ma ...

  2. Logstash读取文本信息并写入到ES

    Logstash读取文本信息并写入到ES 前提是ELK安装没问题 进入到logstash安装目录下的bin目录(我的logstash安装目录:/usr/local/) [root@es1 bin]# ...

  3. 软件常用版本英文snapshot和ga

    版本号,顾名思义,系统.架包.软件的标识号.版本号的数字信息通俗易懂, 格式:主版本号+次版本+(修正版本号build-可选)+(编译版本号-可选)+英文常见号(重点). 常见号:英文各种架包名,Ma ...

  4. [SDOI2013]spring

    Description Input Output Sample Input 3 3 1 2 3 4 5 6 1 2 3 0 0 0 0 0 0 4 5 6 Sample Output 2 HINT 容 ...

  5. [Usaco2005 Jan]Sumsets 求和

    Description Farmer John commanded his cows to search for different sets of numbers that sum to a giv ...

  6. A - Add More Zero

    Bryce1010模板 #include <bits/stdc++.h> using namespace std; #define LL long long int main() { in ...

  7. WIN7中Beyond Compare报错误“应用程序发生错误” 无法启动

    BCompare在WIN7中打开提示"应用程序发生错误"的解决办法: WIN7下寻找:把C:\用户\[用户名]\AppData\Roaming\Scooter Software\B ...

  8. Lena Sort 构造题,很多细节的模拟

    https://www.hackerrank.com/contests/101hack46/challenges/lena-sort 把题目转换成一颗二叉树,也就是当前的节点是cur,然后大的,放右边 ...

  9. 构建微服务开发环境5————安装Node.js

    [内容指引] 下载Node.js: Mac下安装Node.js: Windows下安装Node.js; 查看node和npm的版本. 一.下载Node.js 访问Node.js官网:https://n ...

  10. SpringBoot 2.x (2):请求和传参

    其实请求和传参这些知识属于SpringMVC 不过这也属于必须掌握的知识,巩固基础吧 GET请求: 以第一篇文章自动的方式创建SpringBoot项目: 然后新建Controller: package ...