POJ 2299 逆序对

Crossings

Time Limit: 2 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100463

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

HINT

题意

逆序对求解

题解:

树状数组水

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <queue>
10 #include <map>
11 #include <stack>
12 #define MOD 1000000007
13 #define maxn 32001
14 using namespace std;
15 typedef __int64 ll;
16 inline ll read()
17 {
18     ll x=0,f=1;
19     char ch=getchar();
20     while(ch<‘0‘||ch>‘9‘)
21     {
22         if(ch==‘-‘)f=-1;
23         ch=getchar();
24     }
25     while(ch>=‘0‘&&ch<=‘9‘)
26     {
27         x=x*10+ch-‘0‘;
28         ch=getchar();
29     }
30     return x*f;
31 }
32 //*******************************************************************
33
34 struct ss
35 {
36     int v,index;
37 } in[500005];
38 int c[500005];
39 int a[500005];
40 int n;
41 bool cmp(ss s1,ss s2)
42 {
43     return s1.v<s2.v;
44 }
45 int lowbit(int x)
46 {
47     return x&(-x);
48 }
49 int getsum(int x)
50 {
51     int sum=0;
52     while(x>0)
53     {
54         sum+=c[x];
55         x-=lowbit(x);
56     }
57     return sum;
58 }
59 void update(int x,int value)
60 {
61     while(x<=n)
62     {
63         c[x]+=value;
64         x+=lowbit(x);
65     }
66 }
67 int main()
68 {
69
70     while(scanf("%d",&n)!=EOF)
71     {
72         if(n==0) break;
73         memset(c,0,sizeof(c));
74         for(int i=1; i<=n; i++)
75         {
76             in[i].v=read();
77             in[i].index=i;
78         }
79         sort(in+1,in+n+1,cmp);
80         for(int i=1; i<=n; i++)a[in[i].index]=i; //离散化处理
81         ll ans=0;
82         for(int i=1; i<=n; i++)
83         {
84             update(a[i],1);
85             ans+=i-getsum(a[i]);
86         }
87         cout<<ans<<endl;
88     }
89
90     return 0;
91 }
时间: 2024-09-30 20:55:32

POJ 2299 逆序对的相关文章

POJ 2299 逆序对(归并排序)

终于解决了一个忧伤好久的问题,严重拖了项目进度,深感惭愧!一直被一系列的问题所困扰,然后又只能自己一个人摸索,也是一段辛酸忧伤史,现在小结一下上个月在做二维码的过程中所碰到的问题以及解决办法,现在庆幸终于解决好了,终于能将这个功能告一段落,一下小结也是分享一下Unity的某些"坑",让同行少走弯路,起码在二维码这方面应该会有所启迪,欣慰的是接下来几天终于可以做自己应该做的事情了! 效果图: 先小结一下碰到的问题: 1.Unity工程屏幕方向与Android工程屏幕方向要一致的问题 本来

POJ 1804 逆序对数量 / 归并排序

Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12175   Accepted: 6147 Description Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just

poj 2299 逆序数

http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归并排序求逆序数之前写过 1.归并排序是把两个有序的数组合并成为一个有序的数组,利用分治的思想就可以进行排序 逆序数可以利用这个思想求 求出第一个数组的逆序数,和第二个数组的逆序数,再将两个数组整体的逆序数求出来 f(x,y) = f(x,mid) + f(mid,y) + 之后数组的逆序数 #inc

树状数组求逆序对:POJ 2299、3067

前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换相邻的两数使它有序,要你求出交换的次数.实质上就是求逆序对,网上有很多人说它的原理是冒泡排序,可以用归并排序来求出,但我一时间想不出它是如何和归并排序搭上边的(当初排序没学好啊~),只好用刚学过的树状数组来解决了.在POJ 1990中学到了如何在实际中应用上树状数组,没错,就是用个特殊的数组来记录即

POJ - 2299 - Ultra-QuickSort = 归并排序 + 逆序对 / 树状数组

http://poj.org/problem?id=2299 求逆序对最简单的绝对不会是树状数组,一定是归并排序(认真),不过树状数组会不会快一点呢?理论上应该是树状数组快一点(假如不进行离散化). #include<algorithm> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<map> #include<set

Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

POJ 2299 Ultra-QuickSort(归并排序&#183;逆序对)

题意  给你一个数组求其中逆序对(i<j&&a[i]>a[j])的个数 我们来看一个归并排序的过程: 给定的数组为[2, 4, 5, 3, 1],二分后的数组分别为[2, 4, 5], [1, 3],假设我们已经完成了子过程,现在进行到该数组的"并"操作: a: [2, 4, 5] b: [1, 3] result:[1] 选取b数组的1 a: [2, 4, 5] b: [3] result:[1, 2] 选取a数组的2 a: [4, 5] b: [3] r

poj 2299 Ultra-QuickSort 逆序对模版题

用树状数组求逆序对 唯一的坑点就是sum要用long long存 直接贴代码了 以后忘了还能直接看 2333…… PS:和hdu3743代码是一样的,因为两个都是逆序对模版题…… 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int s[500005]; 6 int N; 7 struct num{ 8 int xuhao,num; 9 }n

Ultra-QuickSort (poj 2299 归并排序 || 树状数组 求逆序对)

Language: Default Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 45751   Accepted: 16615 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct i