xtu数据结构 C. Ultra-QuickSort
C. Ultra-QuickSort
64-bit integer IO format: %lld Java class name: Main
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0 解题:求逆序数,归并排序或者快排+树状数组都可以。坑爹的地方在于要使用long long ,害我WA了几次。逗比。。。。。。 树状数组+快速排序
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
struct node {
int val,index;
} p[maxn];
LL tree[maxn];
bool cmp(const node &x,const node &y) {
return x.val > y.val;
}
int lowbit(int x) {
return x&(-x);
}
void update(int x,int val) {
for(int i = x; i < maxn; i += lowbit(i)) {
tree[i] += val;
}
}
LL sum(int x) {
LL ans = ;
for(int i = x; i; i -= lowbit(i)) {
ans += tree[i];
}
return ans;
}
int main() {
int n,i;
LL ans;
while(scanf("%d",&n),n) {
for(i = ; i < n; i++) {
scanf("%d",&p[i].val);
p[i].index = i+;
}
sort(p,p+n,cmp);
memset(tree,,sizeof(tree));
int pre = INT_MIN;
for(ans = i = ; i < n; i++) {
update(p[i].index,);
ans += sum(p[i].index-); }
printf("%lld\n",ans);
}
return ;
}
归并排序
#include <cstdio>
#define LL long long
LL sum,dt[];
void mysort(int lft,int rht,int step){
static LL temp[];
int md = lft + (step>>),i = ,k = ,j = ;
while(lft + i < md && md + j < rht){
if(dt[lft+i] > dt[md+j]){temp[k++] = dt[md+j];j++;
}else{temp[k++] = dt[lft+i];i++;sum += j;}
}
while(lft+i < md){temp[k++] = dt[lft+i];i++;sum += j;}
while(md+j < rht){temp[k++] = dt[md+j];j++;}
for(i = ; i < k; i++) dt[lft+i] = temp[i];
}
void ms(int n){
int len = ,step = ,m,i,u,v;
sum = ;
while(len < n){len <<= ;}
m = len/step;
while(step <= len){
for(i = ; i < m; i++){
u = i*step;v = (i+)*step;
mysort(u,v>n?n:v,step);
}
step <<= ;m = len/step;
}
}
int main(){
int n,i;
while(scanf("%d",&n),n){
for(i = ; i < n; i++)
scanf("%d",dt+i);
ms(n);
printf("%lld\n",sum);
}
return ;
}
最新文章
- CRM(四川网脉系统)项目总结
- linux实践之程序破解
- activemq jmsTemplate 发送消息速度太慢
- 使用Memory Analyzer tool(MAT)分析内存泄漏(一)
- IIC,RS485,RS232各种协议手册更新中
- Openstack:ice-house安装过程
- iPhone 已停用
- QT: QByteArray储存二进制数据(包括结构体,自定义QT对象)
- UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】
- android-改进&;lt;&;lt;仿QQ&;gt;&;gt;框架源代码
- C++库研究笔记——函数名的宏定义
- 10- python 网络爬虫分析
- DSAPI 提取中间文本(字符串)
- create-react-app 修改项目端口号及ip,设置代理
- .Net Core部署IIS
- 软件工程个人作业四--alpha阶段个人总结
- vue层级关系的数据管理
- Java8 in action
- android通过 Intent 传递类对象
- Linux下gcc编译生成动态链接库*.so文件并调用它(注:执行Test程序后无需用export 命令指定.so库文件路径:方法在文中下方;)
热门文章
- 日常博客-png,jpeg,gif图片
- table表格字母无法换行
- SQLServer 错误: 15404,无法获取有关 Windows NT 组/ 用户 &#39;WIN-8IVSNAQS8T7\Administrator&#39; 的信息,错误代码 0x534。
- SQL SERVER 2008 系列问题:无法访问,混合模式
- vue+element ui项目总结点(六)table编辑当前行、删除当前行、新增、合计操作
- Android学习总结(九)———— 内容提供器(ContentProvider)
- 使用javap分析Java的字符串操作
- iOS上架问题解决
- codeforces Gym 100338H 	High Speed Trains (递推,高精度)
- iOS7.1企业应用";无法安装应用程序 因为证书无效";的解决方案