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. 进程创建函数fork()、vfork() ,以及excel()函数

    一.进程的创建步骤以及创建函数的介绍 1.使用fork()或者vfork()函数创建新的进程 2.条用exec函数族修改创建的进程.使用fork()创建出来的进程是当前进程的完全复制,然而我们创建进程 ...

  2. HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. 更改mysql数据库latin1_swedish_ci为utf8

    原文在http://bingu.net/472/latin1_swedish_ci-to-utf8_general_ci/把下列文件保存为一个.php文件,然后运行 <?phpdefine('D ...

  4. DEDECMS5.7 首页和栏目页调用文章按权重排序

    dedecms 5.7版本已增加按权重排序功能: [arclist]标签增加按权重排序,在后台管理DEDE里找到以下目录\include\taglib中的arclist.lib.php文件并打开 大约 ...

  5. 华为EC169在MAC 10.9.6下的安装方法

    [问题描述] 华为EC169 3G上网卡需要在mbp中安装驱动. 华为官网(http://consumer.huawei.com/cn/)直接搜索EC169,会发现最新的驱动也是2009年发布. 下载 ...

  6. HDU 4901(杭电多校训练#3 1005题)The Romantic Hero(DP)

    题目地址:HDU 4901 这题没想到最后竟然可以做出来.. .. 这题用了两次DP,先从前往后求一次异或的.再从后往前求一次与运算的. 各自是 1:求异或的时候,定义二维数组huo[1000][10 ...

  7. Raspberry Pi + 3个USB摄像头 + Motion(简易监控设备配置记录1——介绍以及安装) 分类: Raspberry Pi 服务器搭建 2015-04-12 19:21 226人阅读 评论(0) 收藏

    参考: Debian官网链接 Motion官网链接 首先,参见Debian官网链接对Motion的介绍,网页中包含了所有相关依赖包,请首先确保这些依赖包的安装. Motion介绍 摘出对Motion的 ...

  8. 关于verilog中if与case语句不完整产生锁存器的问题 分类: FPGA 2014-11-08 17:39 260人阅读 评论(0) 收藏

    在很多地方都能看到,verilog中if与case语句必须完整,即if要加上else,case后要加上default语句,以防止锁存器的发生,接下来就来说说其中原因. 一,什么是锁存器?锁存器与触发器 ...

  9. IO之流程与buffer 图

    http://blog.chinaunix.net/uid-29075379-id-3944364.html

  10. iscsi介绍及iscsi target配置

    iSCSI 主要是透过 TCP/IP 的技术,将储存设备端透过 iSCSI target (iSCSI 目标) 功能,做成可以提供磁盘的服务器端,再透过 iSCSI initiator (iSCSI ...