Description

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

【分析】

开始离散化用MAP T了半天,不活了..

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <map> const int MAXN = + ;
const int MAXM = + ;
//const int MAXM = 2000 + 10;
const int MAXL = ;
using namespace std;
struct DATA{
int val;
int order;
bool operator < (DATA b)const{
return val < b.val;
}
}rem[MAXN];
typedef long long ll;
int n;
int data[MAXN];
int C[MAXN]; void init(){
for (int i = ; i <= n; i++) {
scanf("%d", &data[i]);
C[i] = ;
rem[i].val = data[i];
rem[i].order = i;
}
sort(rem + , rem + + n);
for (int i = ; i <= n; i++) data[rem[i].order] = i;
//for (int i = 1; i <= n; i++) printf("%d\n", data[i]);printf("\n");
}
int lowbit(int x){return x&-x;}
int sum(int x){
int cnt = ;
while (x > ){
cnt += C[x];
x -= lowbit(x);
}
return cnt;
}
void add(int x){
while (x <= n){
C[x]++;
x += lowbit(x);
}
return;
} void work(){
ll Ans = ;
//前面共 i - 1个数字
for (int i = ; i <= n; i++){
Ans += (i - - sum(data[i]));//严格大于
add(data[i]);
}
printf("%lld\n", Ans);
} int main(){
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
while (scanf("%d", &n) && n){
init();
work();
}
return ;
}

【POJ2266】【树状数组+离散化】Ultra-QuickSort的更多相关文章

  1. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  3. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  4. BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学

    1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...

  5. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  6. [HDOJ4325]Flowers(树状数组 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...

  7. Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6321  Solved: 2628[Su ...

  8. CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序

    http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...

  9. Ultra-QuickSort(树状数组+离散化)

    Ultra-QuickSort  POJ 2299 Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50495   Accep ...

  10. HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数.请输出最少需要修改多少 ...

随机推荐

  1. java 学习连接

    @Repository.@Service.@Controller 和 @Component   注解:http://blog.csdn.net/ye1992/article/details/19971 ...

  2. 笔记-人老了-github

    其实GITHUB是很不错的,虽然之前的JD泄露那件事情,后果很严重. 但是作为个人使用很不错的. github使用入门: 1:申请一个帐号 2:github使用ssh推送的.(ssh走的是加密) 所以 ...

  3. yarn的初步理解

    查考site: http://hadoop.apache.org/docs/r2.6.0/hadoop-yarn/hadoop-yarn-site/YARN.html yarn结构图如下: 1.yar ...

  4. iOS屏蔽高频点击技巧

    例如高频率点击一个按钮或者TableViewCell,会造成功能多次重复执行,在异步网络请求时候或者多线程时候,造成的问题尤其明显. 解决方法: 声明一个属性self.actionWorking ,标 ...

  5. LaTeX中参考文献管理以及注意事项

    注意:版权所有,转载请注明出处 使用LaTeX写作,很重要的一个部分就是参考文献的制作,LaTeX使用bibtex管理文章的参考文献,bibtex即是一种工具,又是一种格式的代表,它要求使用*.bib ...

  6. java 23 种设计模式

    一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接 ...

  7. God of War - HDU 2809(状态压缩+模拟)

    题目大意:貌似是一个游戏,首先给出卢布的攻击,防御,还有血量,再给出每升一级增加的攻击防御还有血量,然后又N个敌人,杀死每个敌人都会得到一些经验,求杀死完所有敌人时剩余的最大血量. 分析:因为敌人比较 ...

  8. python_list和tuple互转

    Python中,tuple和list均为内置类型, 以list作为参数将tuple类初始化,将返回tuple类型 tuple([1,2,3,4]) list->tuple 以tuple做为参数, ...

  9. C#中的线程(中)-线程同步

    1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具:                       简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...

  10. 【Android - MD】之NavigationView的使用

    NavigationView是Android 5.0新特性--Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局). Nav ...