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. STL使用迭代器逆向删除

    网上有很多这种例子: void erase(vector<int> &v) { for(vector<int>::reverse_iterator ri=v.rbegi ...

  2. DVWA之Brute Force教程

    ---恢复内容开始--- Brute Force暴力破解模块,是指黑客密码字典,使用穷举的方法猜出用户的口令,是一种广泛的攻击手法. LOW low级别的漏洞利用过程 1.使用burp suite工具 ...

  3. HTTP 三次握手  建立连接 和  四次握手断开连接

    三次握手建立连接    第一次握手:主机A发送位码为syn=1,随机产生seq number=1234567的数据包到服务器,主机B由SYN=1知道,A要求建立联机: 第二次握手:主机B收到请求后要确 ...

  4. 查询 request 对象的数据

    在 EmpController 中调用 RequestInfoService ris = new RequestInfoService(); ris.saveRequestInfo(request); ...

  5. 覆盖alert对话框-自制Jquery.alert插件

    Javascript 代码: (function ($) { 'use strict'; window.alert = $.alert = function (msg) { var defaultOp ...

  6. UVA 1625 Color Length 颜色的长度 (预处理+dp)

    dp[i][j]表示前一个序列拿了i个颜色,后一个序列拿了j个颜色的最小花费. 转移的时候显然只能向dp[i+1][j],或dp[i][j+1]转移,每增加拿走一个颜色,之前已经出现但没结束的颜色个数 ...

  7. chrom浏览器-F12使用方法二

    文摘摘自:https://blog.csdn.net/run65536/article/details/80568543 提示:右键点击图片选择在新窗口或新标签页中打开可查看大图. 一.Element ...

  8. PJSIP-iOS源码编译

    官方文档https://trac.pjsip.org/repos/wiki/Getting-Started/iPhone 功能 在iPhone上可以实现的功能: 包含基于CoreAudio的音频设备, ...

  9. WINDOWS-API:取得系统语言种类-GetOEMCP

    GetOEMCP VB声明 Declare Function GetOEMCP Lib "kernel32" Alias "GetOEMCP" () As Lo ...

  10. Codeforces Round #273 (Div. 2)-C. Table Decorations

    http://codeforces.com/contest/478/problem/C C. Table Decorations time limit per test 1 second memory ...