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. FastDFS java 辅助类

    package cn.saiz.drkms.task.crack.utils; import java.io.File; import java.io.FileInputStream; import ...

  2. webpack(1)

    在网页中会引用哪些常见的静态资源? JS .js .jsx .coffee .ts(TypeScript 类 C# 语言) CSS .css .less .sass .scss Images .jpg ...

  3. java 设计模式 之 桥梁模式

    桥梁模式:将抽象和实现解耦,使两者可以独立的变化.解释:将两个有组合关系,强耦合的对象,各自抽象然后解耦.(类关系图看https://www.cnblogs.com/blogxiao/p/951388 ...

  4. 双飞翼布局介绍-始于淘宝UED-2011年淘宝玉伯写的

    仔细分析各种布局的技术实现,可以发现下面三种技术被经常使用: 浮动 float 负边距 negative margin 相对定位 relative position 这是实现布局的三个最基本的原子技术 ...

  5. 使用JavaScript调用手机平台上的原生API

    我之前曾经写过一篇文章使用Cordova将您的前端JavaScript应用打包成手机原生应用,介绍了如何使用Cordova框架将您的用JavaScript和HTML开发的前端应用打包成某个手机平台(比 ...

  6. DRM 简介

    首先,我们对和DRM 相关的一些概念进行介绍. Buffer: 对于RAC 数据库,当一个数据块被读入到buffer cache后,我们就称其为buffer , cache fusion 会将这个bu ...

  7. UVA 753 A Plug for UNIX (最大流)

    关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...

  8. 数学题 HDOJ——2086 简单归纳

    哎 真的是懒得动脑子还是怎么滴... 题目如下 Problem Description 有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n).若给 ...

  9. 闭包和OC的block的本质

    “闭包” 一词来源于以下两者的结合:要执行的代码块(由于自由变量被包含在代码块中,这些自由变量以及它们引用的对象没有被释放)和为自由变量提供绑定的计算环境(作用域). http://blog.csdn ...

  10. iPhone Tutorials

    http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...