poj2299--B - Ultra-QuickSort(线段树,离散化)
| Time Limit: 7000MS | Memory Limit: 65536K | |
| Total Submissions: 41215 | Accepted: 14915 |
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 elementsuntil 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 i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
题意:求出最小交换次数。使得数组变得有序(由小到大)。 也就是求出逆序数。这个题归并排序,树状数组。线段树又能够解,能够当做练手用
使用线段树解的话,由于给出的数值非常大。所以须要离散化。一開始直接使用map果断的超时了,仅仅能想一个更简便的离散的方法,使用结构体存下一開始的值k和它初始的序号(id1),sort对k进行排序。得到新的序号(id2),通过id1直接改变给出的数组。变为id2。这样仅仅用n + logn的时间就能够离散完毕,(注意要推断反复的值。反复的值共享一个id2)。至于线段树部分就是一个模板
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL __int64
#define maxn 600000
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now LL l,LL r,LL rt
struct node{
LL id1 , id2 ;
LL k ;
}p[maxn] ;
LL cl[maxn<<2] , a[maxn] ;
bool cmp(node a,node b)
{
return a.k < b.k ;
}
void push_up(int_now)
{
cl[rt] = cl[rt<<1] + cl[rt<<1|1] ;
}
void update(LL i,int_now)
{
if( i < l || i > r )
return ;
if( i == l && i==r )
{
cl[rt]++ ;
return ;
}
update(i,lson);
update(i,rson);
push_up(now);
return ;
}
LL query(int ll,int rr,int_now)
{
if( ll > r || rr < l )
return 0;
if( ll <= l && r <= rr )
return cl[rt] ;
return query(ll,rr,lson) + query(ll,rr,rson);
}
int main()
{
LL i , n , m , l , r , x , num ;
while(scanf("%I64d", &m) && m)
{
for(i = 0 ; i < m ; i++)
{
scanf("%I64d", &a[i]);
p[i].k = a[i] ;
p[i].id1 = i ;
}
sort(p,p+m,cmp);
int temp = -1 ;
n = 0 ;
for(i = 0 ; i < m ; i++)
{
if( p[i].k == temp )
p[i].id2 = n ;
else
{
p[i].id2 = ++n ;
temp = p[i].k ;
}
}
for(i = 0 ; i < m ; i++)
a[ p[i].id1 ] = p[i].id2 ;
memset(cl,0,sizeof(cl));
num = 0 ;
for(i = 0 ; i < m ; i++)
{
num += (i - query(1,a[i],root));
update(a[i],root);
}
printf("%I64d\n", num);
}
return 0;
}
poj2299--B - Ultra-QuickSort(线段树,离散化)的更多相关文章
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
随机推荐
- android switch语句case expressions must be constant expressions
在项目中遇到这样的Exception:case expressions must be constant expressions public class StandingCityActivity e ...
- [ASP.NET]利用itextsharp将GridView汇出PDF档
原文 [ASP.NET]利用itextsharp将GridView汇出PDF档 最近在讨论区看到有人说itextsharp可以把网页变成PDF 小弟就去抓一下itextsharp来玩玩,先教大家最实用 ...
- Mysql事务,并发问题,锁机制-- 幻读、不可重复读(转)
1.什么是事务 事务是一条或多条数据库操作语句的组合,具备ACID,4个特点. 原子性:要不全部成功,要不全部撤销 隔离性:事务之间相互独立,互不干扰 一致性:数据库正确地改变状态后,数据库的一致性约 ...
- MSSQL - 创建新用户
1.首先使用Windows身份验证登陆. 2.然后一次打开:安全性--->登录名.右键登录名,点击新建登录名. 3.常规选项卡下:填写登录名.选择SQL Server身份验证,填写登录名密码.取 ...
- NVelocity 实例
using System; using System.IO; using System.Collections; using System.Collections.Generic; using Sys ...
- ubuntu 安装Opencv2.4.7
1.安装Cmake: sudo apt-get install cmake 2.到Opencv目录下创建文件夹release $ cmake -D CMAKE_BUILD_TYPE=RELEASE - ...
- cocos2d-x游戏开发系列教程-坦克大战游戏之虚拟手柄的显示
上篇文章我们有了坦克,但是没有手柄,无法控制坦克. 1.这篇我们编写虚拟手柄来控制坦克.头文件大致内容如下: #define RES_PADDLE_LEFT "paddle/left.png ...
- Study notes for Latent Dirichlet Allocation
1. Topic Models Topic models are based upon the idea that documents are mixtures of topics, where a ...
- 再淡spring jdbc 连接池断开重连设置
先看一段错误日志: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConne ...
- 学习算法-基数排序(radix sort)卡片分类(card sort) C++数组实现
基数排序称为卡片分类,这是一个比较早的时间越多,排名方法. 现代计算机出现之前,它已被用于排序老式打孔卡. 说下基数排序的思想.前面我有写一个桶式排序,基数排序的思想是桶式排序的推广. 桶式排序:ht ...