集训第四周(高效算法设计)A题 Ultra-QuickSort
原题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的更多相关文章
- 集训第四周(高效算法设计)P题 (构造题)
Description There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...
- 集训第四周(高效算法设计)O题 (构造题)
A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...
- 集训第四周(高效算法设计)N题 (二分查找优化题)
原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...
- 集训第四周(高效算法设计)M题 (扫描法)
原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...
- 集训第四周(高效算法设计)I题 (贪心)
Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...
- 集训第四周(高效算法设计)E题 (区间覆盖问题)
UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...
- 集训第四周(高效算法设计)D题 (区间覆盖问题)
原题 UVA10020 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
- 集训第四周(高效算法设计)K题 (滑窗问题)
UVA 11572 唯一的雪花 题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新 方法一: #include& ...
随机推荐
- win10系统下使用EDGE浏览器找不到Report Builder 启动图标
Win10系统下如果要使用Report Builder,可能存在EDGE浏览器或者Chrome找不到ReportBuilder的启动图标的情况,此时,应以管理员权限运行IE浏览器,即可看到图标.
- SolrCloud索引富文本数据
solrconfig配置文件: schema配置文件: 执行目录: /opt/solr-5.5.4/server/scripts/cloud-scripts -- 下载配置文件 ./zkcli.sh ...
- Android Dialogs(4)Dialog事件处理
Passing Events Back to the Dialog's Host When the user touches one of the dialog's action buttons or ...
- 【先定一个小目标】Asp.net Core 在IIS上的托管运行
1.安装 .NET Core Framework 下载.net core地址:官网地址 2.Install IIS 在控制面板->程序与功能->Internet Infomation Se ...
- C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响)
C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响),如以下代码将无法通过编译. foreach (int x in myArray) { x++; //错误代码,因为改变 ...
- input标签的hidden属性,四大常用JSTL标签库
input标签的hidden属性的应用及作用 定义:传输关于客户端/服务器交互的状态信息. Transmits state information about client/server intera ...
- 线程池机制使nginx性能提高9倍
原文标题:Thread Pools in NGINX Boost Performance 9x! 原文官方地址:https://www.nginx.com/blog/thread-pools-boos ...
- jquery基础知识点总结
Jquery是一个优秀的js库,它简化了js的复杂操作,不需要关心浏览器的兼容问题,提供了大量实用方法. Jquery的写法 方法函数化 链式操作 取值赋值合体] $(“p”).html(); 取 ...
- poj2718 Smallest Difference
思路: 暴力乱搞. 实现: #include <iostream> #include <cstdio> #include <sstream> #include &l ...
- swiper4实现的拥有header和footer的全屏滚动demo/swiper fullpage footer
用swiper4实现的拥有header和footer的全屏滚动demo, <!DOCTYPE html> <html lang="en"> <head ...