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 studio gradle配置
什么是Gradle? Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. gr ...
- Git命令非主流札记
使用git做开发的版本管理也有一年半之多了,但是始终都是常用的branch commit status diff push等一些再常用不过的命令,最近闲下来,打算学习一下高端用法,所以就静下心来好好读 ...
- SQL之单行函数
单行函数语法: function name(column|expression,[arg1,arg2,...]) 参数说明: function name:函数名称 column:数据库列名 expre ...
- Swift - 使用UI Dynamics给UIKit组件添加重力和碰撞行为
UI Dynamics是UIKit的一个新组成部分,它向iOS中的视图提供了与物理学有关的功能和动画.可以让你向视图中引入力和物理属性,可以让你的视图弹跳,舞动,受重力影响等等. 下面通过样例,演示使 ...
- MFC 只启动一个程序实例
问题描述: 我们开发过程中可能会经常遇到,只启动一个程序实例.即一个程序启动之后,如果再次执行该程序,将会恢复之前打开的程序,而不是打开一个新的程序. 实现原理:利用FindWindow/FindWi ...
- CodeForces 462B Appleman and Card Game(贪心)
题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...
- 略懂 MySQL字符集
本文虽说旨在明白.但若略懂亦可.毕竟诸葛孔明如是 只有基于字符的值才有所谓字符集的概念 某些字符集可能需要更多CPU.消费更多的内存和磁盘空间.甚至影响索引使用 这还不包括令人 ...
- asp.net2.0安全性(1)--用户角色篇(代码实现1)--转载来自车老师
创建用户: MembershipCreateStatus mc; Membership.CreateUser(txtUid.Text, txtPwd.Text, txtEmail.Text, txtQ ...
- ASP.NET - 锚点跳转,用于回到顶部
<a name ="top"></a> <a href ="#top">回到顶部</a> 第一行代码写在顶部,第 ...
- PHP之验证码代码
<?php session_start(); $checkcode=""; /*for($i=0;$i<4;$i++) { $checkcode.=dechex(ran ...