C. Ultra-QuickSort

Time Limit: 7000ms
Memory Limit: 65536KB

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 ;
}

xtu数据结构 C. Ultra-QuickSort的更多相关文章

  1. xtu数据结构 H. City Horizon

    H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cl ...

  2. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  3. xtu数据结构 D. Necklace

    D. Necklace Time Limit: 5000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class ...

  4. xtu数据结构 G. Count the Colors

    G. Count the Colors Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Jav ...

  5. xtu数据结构 B. Get Many Persimmon Trees

    B. Get Many Persimmon Trees Time Limit: 1000ms Memory Limit: 30000KB 64-bit integer IO format: %lld  ...

  6. Big O Complexity Graph

    Big O Complexity Graph Big O === O() 算法复杂度速查表 数据结构 数组排序算法 Quicksort O(n log(n)) O(n log(n)) O(n^2) O ...

  7. C#数据结构与算法系列(二十二):快速排序算法(QuickSort)

    1.介绍 快速排序(QuickSort)是对冒泡排序的一种改进,基本思想是:通过一趟排序将要排序的数据分割成独立的两部分, 其中一部分的所有数据都比另一部分的所有数据都要小,然后再按此方法对这两部分数 ...

  8. 数据结构(主席树,Bit):XTU 1247/COGS 2344. pair-pair

    pair-pair 输入文件:pair-pair.in   输出文件:pair-pair.out   简单对比 时间限制:7 s   内存限制:64 MB Time Limit : 7000 MS M ...

  9. 数据结构与算法 Big O 备忘录与现实

    不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新.        算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...

随机推荐

  1. 删除所有约束、表、视图等SQL脚本

    --删除所有约束.表.视图等SQL脚本 --############################################### --删除所有外键约束 --################# ...

  2. CSS子元素设置margin-top作用于父容器?

    CSS子元素设置margin-top作用于父容器? 原因: In this specification, the expression collapsing margins means that ad ...

  3. 详解HTML中的表格标签

    详细代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  4. arcgis jsapi接口入门系列(2):图层基础操作

    //图层相关demo layerFun: function () { //获取地图的所有图层(不包括的图层类型:底图图层(basemaps)) let layers = this.map.layers ...

  5. linux 删除文件后空间没有释放的解决办法

    清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [root@ticketb ~]# df -hFilesystem          ...

  6. shell框架

    #!/bin/bash#注释#注释#环境变量相关,如下PATH=/sbin:/bin:/usr/bin:/usr/sbin #引入库函数,如下,类似于c语言的#include "*.h&qu ...

  7. Linux中grep、sed、awk使用介绍

    linux文件操作命令介绍1)grepgrep 用于在文件中查找符合条件的记录grep 参数 过滤条件 文件过滤的条件中可使用正则表达式-c 显示符合的行数-i 忽略大小写-n 显示符合要求的记录,包 ...

  8. 洛谷 P1311 选择客栈

    题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...

  9. OpenStack安装keyston 错误BError: (pymysql.err.InternalError) (1071, u‘Specified key was too long; max key length is 767 bytes‘) [SQL: u‘\nCREATE TABLE migrate_ver

    折腾了两天的错误,BError: (pymysql.err.InternalError) (1071, u‘Specified key was too long; max key length is ...

  10. halcon相机标定及图像矫正

    https://blog.csdn.net/humanking7/article/details/44756073 相机标定内容详解:转载自 祥的博客 预备知识 标定中的四个坐标系 1.1.平面旋转 ...