poj 2299(离散化+树状数组)
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 53777 | Accepted: 19766 |
Description

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
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
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
Source
题意:求解一个串的逆序数的个数是多少??
题解:离散化数组变成下标,然后每次将离散化的下标放进树状数组,放进去之后统计小于他的数的个数是多少。用 i - getsum(a[i])即为大于它的数的个数,其中 i 为当前已经插入的数的个数。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
const int N = ; struct Node{
int v,id;
}node[N];
int a[N],c[N],n;
int lowbit(int x){
return x&(-x);
}
void update(int idx,int v){
for(int i=idx;i<=N;i+=lowbit(i)){
c[i]+=v;
}
}
int getsum(int idx){
int sum = ;
for(int i=idx;i>=;i-=lowbit(i)){
sum+=c[i];
}
return sum;
}
int cmp(Node a,Node b){
return a.v<b.v;
}
int main()
{
while(scanf("%d",&n)!=EOF,n){
memset(c,,sizeof(c));
for(int i=;i<=n;i++){
scanf("%d",&node[i].v);
node[i].id = i;
}
sort(node+,node+n+,cmp);
for(int i=;i<=n;i++){
a[node[i].id] = i;
}
long long cnt = ;
for(int i=;i<=n;i++){
update(a[i],);
cnt=cnt+ i - getsum(a[i]);
}
printf("%lld\n",cnt);
}
return ;
}
poj 2299(离散化+树状数组)的更多相关文章
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- POJ 2299 Ultra-QuickSort (树状数组 && 离散化&&逆序)
题意 : 给出一个数n(n<500,000), 再给出n个数的序列 a1.a2.....an每一个ai的范围是 0~999,999,999 要求出当通过相邻两项交换的方法进行升序排序时需要交换 ...
- POJ 2299 Ultra-QuickSort (树状数组 && 离散化)
题意 : 给出一个数n(n<500,000), 再给出n个数的序列 a1.a2.....an每一个ai的范围是 0~999,999,999 要求出当通过相邻两项交换的方法进行升序排序时需要交换 ...
- POJ 2299 Ultra-QuickSort (树状数组+离散化 求逆序数)
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组
BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...
- poj-----Ultra-QuickSort(离散化+树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 38258 Accepted: 13784 ...
随机推荐
- linux Ubuntu18.04 安装配置MySQL
1.安装 ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get install mysql ...
- Gym - 101908G Gasoline 二分+最大流
G - Gasoline Gym - 101908G 题意:给出R个提供点,P个接收点,每个接收点都要接收满,还有一个运输的时间,问最小时间能够完成所有的运输 题解:首先每次都必须要满流,所以我们只要 ...
- 51NOD 1292 1277(KMP算法,字符串中的有限状态自动机)
在前两天的CCPC网络赛中...被一发KMP题卡了住了...遂决定,哪里跌倒就在哪里爬起来...把个KMP恶补一发,连带着把AC自动机什么的也整上. 首先,介绍设定:KMP算法计划解决的基本问题是,两 ...
- [项目2] 10mins
1.准备工作 M层:生成虚假数据 from django.db import models from faker import Factory # Create your models here. c ...
- Retrofit 入门和提高
首先感谢这个哥们,把我从helloworld教会了. http://blog.csdn.net/angcyo/article/details/50351247 retrofit 我花了两天的时间才学会 ...
- Python接口测试之封装requests
首先安装requests库: pip install requests test_requests.py 首先在TestRequest类中封装get与post方法, import requests i ...
- Lucene.Net 精品教程
http://www.cnblogs.com/piziyimao/archive/2013/01/31/2887072.html
- C#入门篇6-1:字符串操作 char常用的函数
//char 字符的常见操作 public static void FChar() { //判定字符的类别 char ch1 = 'a';//使用小引号 bool bl = true; bl = ch ...
- STL学习笔记5--map and multimap
Maps是一种关联式容器,包含“关键字/值”对. Multimaps和maps很相似,但是MultiMaps允许重复的元素. 简单介绍: 1.声明,首先包含头文件 “map” map <int, ...
- Selenium中自动输入10位随机数字的方法
有时候项目中需要输入快递号,对于已输入过的快递单号则不能再次输入,这种问题怎么解决呢,可以看下这个方法 public static final String ALLCHAR = "01234 ...