POJ 2299 离散化+树状数组
来源:互联网 发布:kad网络连接不上 编辑:程序博客网 时间:2024/06/18 01:38
这段时间没怎么发博客 被动态规划折磨的头疼...
今天就把dp放下了 开始上手了几道树状数组的题
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 60829 Accepted: 22563
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
Source
Waterloo local 2005.02.05
#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<string>#include<stack>#include<queue>#include<cmath>#include<stack>#include<list>#include<map>#include<set>typedef long long ll;using namespace std;int a[500005];int bit[500005];int n;struct node{int i1;int x;}nd[500005];bool cmp1(node a,node b){ return a.x<b.x; //根据值的大小排序}bool cmp2(node a,node b){ return a.i1<b.i1; //之前根据值的大小排序 所以下标被大打乱了 离散化后根据下标重新排好}int sum(int i){ int s=0; while(i>0) { s+=bit[i]; i-=i&-i; } return s;}void add(int i,int x){ while(i<=500003) { bit[i]+=x; i+=i&-i; }}int main(){ int i,j,k; while(scanf("%d",&n)==1&&n) { for(i=0;i<n;i++) { nd[i].i1=i; scanf("%d",&k); nd[i].x=k; } memset(bit,0,sizeof bit); //不要忘记初始化!! sort(nd,nd+n,cmp1);//根据值的大小排序 for(i=0;i<n;i++) { nd[i].x=i+1; //进行离散化 因为sum函数里条件为i>0 所以最好还是不要出现0值 所以+1就好了 } sort(nd,nd+n,cmp2); //根据下标重新排好 ll ans=0; for(i=0;i<n;i++) { ans+=i-sum(nd[i].x); //之后这就是白书上的模板了 冒泡模板 add(nd[i].x,1); } printf("%lld\n",ans); // ans一定要开long long型的!int型过不了! } return 0;}
阅读全文
1 0
- POJ 2299 树状数组+离散化
- poj 2299 树状数组+离散化
- POJ 2299(树状数组,离散化)
- poj 2299 树状数组 离散化
- POJ-2299-树状数组,离散化
- POJ 2299 树状数组+离散化
- POJ 2299 离散化+树状数组
- POJ 2299 Ultra-QuickSort【树状数组+离散化】
- POJ 2299 离散化+树状数组求逆序数
- poj 2299 Ultra-QuickSort(离散化+树状数组)
- poj 2299 树状数组离散化逆序数 水
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
- POJ 2299 树状数组求逆序数+离散化
- poj 2299 离散化+树状数组求逆序数
- poj 2299 Ultra-QuickSort 离散化+树状数组
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
- poj 2299(离散化+树状数组求逆序数)
- POJ 2299 Ultra-QuickSort(树状数组+离散化处理)
- LeetCode (Gray Code)
- wget命令
- 多线程之旅(1)—
- PHP变量在内存中的存储方式
- opencv(27)---分水岭算法及图像修补
- POJ 2299 离散化+树状数组
- 华为网课云计算笔记
- Linux内核之高端内存
- C#综合揭秘——细说多线程(上)
- MySQL中的外键是什么、有什么作用
- 第1章 Android应用和开发环境
- Spark写入Hbase表预分区插入测试
- 在Ubuntu和Windows下用命令行升级NodeJS和npm的版本
- idea安装配置tomcat