1020(树状数组,逆序对+离散化)
来源:互联网 发布:tcpip网络层安全协议 编辑:程序博客网 时间:2024/06/06 02:34
Problem 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.
Ultra-QuickSort produces the output
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
59105431230
Sample Output
60
题目大概+思路:
逆序对数量的模板题。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int c[500001];int b[500002];int lowbit(int x){ return x&(-x);}void add(int x,int v){ while(x<=500001) { c[x]+=v; x+=lowbit(x); }}long long sum(int x){ long long sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum;}struct poin{ int v,id;}a[500005];int cmp(const poin a,const poin b){ if(a.v<b.v)return 1; else return 0;}int main(){ int n; while(scanf("%d",&n)&&n) { memset(c,0,sizeof(c)); memset(b,0,sizeof(b)); for(int i=1;i<=n;i++) { scanf("%d",&a[i].v); a[i].id=i; } sort(a+1,a+n+1,cmp); b[a[1].id]=1; int o=2; for(int i=2;i<=n;i++) { if(a[i].v==a[i-1].v)b[a[i].id]=b[a[i-1].id]; else b[a[i].id]=o++; } long long su=0; for(int j=1;j<=n;j++) { add(b[j],1); su+=j-sum(b[j]); } printf("%I64d\n",su); } return 0;}
阅读全文
0 0
- 1020(树状数组,逆序对+离散化)
- 树状数组+离散化求逆序对
- [离散化+树状数组]逆序对
- 洛谷P1908逆序对(离散化+树状数组)
- 树状数组求逆序对(要离散化)
- 【求逆序对】CODE[VS] 3286 火柴排队 (树状数组离散化求逆序对)
- 树状数组+离散化 《求逆序数对》
- POJ 2299 <离散化+树状数组求逆序数对>
- [NOIP2013] 火柴排队 离散化 树状数组 逆序对
- 【codevs4163】 hzwer与逆序对(树状数组+离散化)
- 树状数组求逆序对及离散化
- poj 2299 Ultra-QuickSort (离散化,树状数组,逆序对)
- hdu 3743 Frosh Week (离散化+树状数组,求逆序对)
- POJ 2299 Ultra-QuickSort(离散化+树状数组求逆序对)
- hdu 5792 逆序对的个数(离散化&&树状数组&&容斥)
- POJ:2299 Ultra-QuickSort(树状数组+离散化+技巧+求逆序对)
- bzoj3289 Mato的文件管理(树状数组求逆序对+莫队+离散化)
- hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化
- 好未来:n个数里出现次数大于等于n/2的数
- 【codevs1159】最大全0子矩阵(悬线法)
- laravel5.4+vue+element+vux环境搭建
- N-Queens leetcode java
- spring整合mongodb时出现的错误
- 1020(树状数组,逆序对+离散化)
- Android中图片占用内存的计算
- 目标检测的标注工具--集成环境版本labelImg
- SpringMVC的配置文件
- <Linux>详解Makefile变量
- [前缀和 乱搞]BZOJ4972 .小Q的方格纸
- xcode忽略警告
- Java反射
- 【juincen】mac上搭建php环境