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. Java-IDEA环境搭建swagger

    1.项目POM导入包(使用Maven管理的代码) 2.POM文件导入包 <dependencyManagement> <dependencies> <dependency ...

  2. inputStream 与 String 的互相转换

    一. String 转换为InputStream String str = "String 与 inputStream转换"; InputStream ins1 = new Byt ...

  3. HDU 4003 Find Metal Mineral (树形DP,经典)

    题意:给定一棵树图,n个节点,有边权,要派k<11个机器人从节点s出发,遍历所有的点,每当1只机器人经过1条边时就会花费该边的边权,边是可重复走的.问遍历完所有点的最小花费? 思路: 非常经典, ...

  4. windows中安装模拟器后修改模拟器中的hosts方法

    1.背景 有的时候我们测试安卓的app需要绑定hosts,这个时候我们如果只是修改PC机器上的hosts,然而在模拟器中并不生效.这个时候我们就需要修改模拟器中的hosts. 模拟器中的hosts为只 ...

  5. ucosii(2.89) 在Lpc1765移植中定时器的使用。

    1,lpc1765的systicker register是24bit, cpu 频率64Mhz时候,注意不要设置systicker 的值超过24bit. 2, 使用timer 的callback函数, ...

  6. ArcGis server连接oracle

    ArcGIG server连接Oracle 目录--gis服务器--添加arcgis server 下一步: 身份验证为在arcgis server manager 中的管理员登录密码和账户 对于服务 ...

  7. 数据库-SQL语法:LEFT JOIN

    LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行.(补充:left join是一对多的关系,表里所有符合条件的记 ...

  8. vsftp配置日志及其启用本地时间

    vsftp配置日志及其启用本地时间 1. 启用vsftp日志 xferlog_enable=YES xferlog_std_format=YES xferlog_file=/var/log/xferl ...

  9. touch-action css属性 滚动和缩放手势

    CSS 属性 touch-action 用于指定某个给定的区域是否允许用户操作,以及如何响应用户操作(比如浏览器自带的划动,缩放等) 默认情况下,平移(滚动) 和 缩放手势由浏览器专门处理.该属性用于 ...

  10. 长链剖分优化dp三例题

    首先,重链剖分我们有所认识,在dsu on tree和数据结构维护链时我们都用过他的性质. 在这里,我们要介绍一种新的剖分方式,我们求出这个点到子树中的最长链长,这个链长最终从哪个儿子更新而来,那个儿 ...