归并求逆序数(逆序对数) && 线段树求逆序数
Brainman
Time Limit: 1000 MS Memory Limit: 30000 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
Problem Here's what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:Start with: 2 8 0 3 swap (2 8) 8 2 0 3 swap (2 0) 8 0 2 3 swap (2 3) 8 0 3 2 swap (8 0) 0 8 3 2 swap (8 3) 0 3 8 2 swap (8 2) 0 3 2 8 swap (3 2) 0 2 3 8 swap (3 8) 0 2 8 3 swap (8 3) 0 2 3 8So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:Start with: 2 8 0 3 swap (8 0) 2 0 8 3 swap (2 0) 0 2 8 3 swap (8 3) 0 2 3 8The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond's mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.
Input
Output
Sample Input
4
4 2 8 0 3
10 0 1 2 3 4 5 6 7 8 9
6 -42 23 6 28 -100 65537
5 0 0 0 0 0
Sample Output
Scenario #1:
3 Scenario #2:
0 Scenario #3:
5 Scenario #4:
0
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#define N 1000010
using namespace std;
long long ans;
int a[N]; void merge(int s1,int e1,int s2,int e2)
{
int p1,p2,p;
int* temp = new int[e2-s1+];
p=;p1=s1;p2=s2;
while(p1<=e1&&p2<=e2)
{
if(a[p1]<=a[p2])
{
temp[p++]=a[p1++];
continue;
}
else
{
temp[p++]=a[p2++];
ans+=e1-p1+; //关键所在
continue;
}
}
while(p1<=e1) temp[p++]=a[p1++];
while(p2<=e2) temp[p++]=a[p2++];
int i;
for(i=s1;i<=e2;i++) a[i]=temp[i-s1];
delete temp;
} void merge_sort(int s,int e)
{
int m;
if(s<e)
{
m=(s+e)/;
merge_sort(s,m);
merge_sort(m+,e);
merge(s,m,m+,e);
}
} int main()
{
int test;
int num;
scanf("%d",&test);
for(num=;num<=test;num++)
{
int n,i;
ans=;
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%d",&a[i]);
merge_sort(,n-);
printf("Scenario #%d:\n",num);
printf("%lld\n",ans);
if(num!=test)
printf("\n");
}
}
#include<iostream>
#include<cstring>
#include <stdio.h>
#include<algorithm>
using namespace std;
int n;
int static c[];
int a[];
const int maxn = ;
struct Node
{
int value,pos;
bool operator<(const Node a)const
{
return value<a.value;
}
} s[];
int lowbit(int x)
{
return x&(-x);
}
void insert(int pos,int x)
{
while(pos<=maxn)
{
c[pos]+=x;
pos+=lowbit(pos);
}
}
int sum(int x)
{
int s=;
while(x>)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int t,num;
cin>>t;
for(num=; num<=t; num++)
{
scanf("%d",&n);
memset(c,,sizeof(c));
memset(a,,sizeof(a));
for(int i=; i<n; i++)
{
scanf("%d",&s[i].value);
s[i].pos=i+;
}
sort(s,s+n);
int id=;
a[s[].pos]=;
for(int i=; i<n; i++) //离散化
{
if(s[i].value!=s[i-].value)
{
a[s[i].pos]=++id;
}
else
{
a[s[i].pos]=id;
}
}
long long int ans=;
for(int i=; i<=n; i++) //一个个插入求逆序对
{
insert(a[i],);
ans+=(i-sum(a[i]));
}
printf("Scenario #%d:\n",num);
printf("%lld\n",ans);
if(num!=t)
printf("\n");
}
return ;
}
树状数组时间空间上都没有归并好~~~~~~~
归并求逆序数(逆序对数) && 线段树求逆序数的更多相关文章
- BNU 2418 Ultra-QuickSort (线段树求逆序对)
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...
- 线段树求逆序数方法 HDU1394&&POJ2299
为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...
- hdu 1394 (线段树求逆序数)
<题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...
- poj2299 Ultra-QuickSort(线段树求逆序对)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- <Sicily>Inversion Number(线段树求逆序数)
一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 线段树-最小逆序数hdu1394
title: 线段树-最小逆序数 date: 2018-10-12 17:19:16 tags: acm 算法 刷题 categories: ACM-线段树 概述 这是一道简单的线段树的题,,,当然还 ...
- 【BZOJ3295】动态逆序对(线段树,树状数组)
[BZOJ3295]动态逆序对(线段树,树状数组) 题面 Description 对于序列A,它的逆序对数定义为满足iAj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的 ...
随机推荐
- 如何理解反向传播 Backpropagation 梯度下降算法要点
http://colah.github.io/posts/2015-08-Backprop/ http://www.zhihu.com/question/27239198 待翻译 http://blo ...
- html5 录制mp3音频,支持采样率和比特率设置
13年的时候做过html5录音,一个问题是保存的wav格式文件很大,当初用了一个迂回的方式,上传到服务器后调用 lame 编码器转换,但由于文件大,上传较慢.不得不说,前端技术发展真是日新月异,有人实 ...
- 开源安卓Http文件下载框架file-downloader的使用
file-downloader FileDownloader(https://github.com/wlfcolin/file-downloader)是本人开源的一个安卓Http文件下载框架,是根据自 ...
- python自我输出源程序
1.参考相应c程序 s='s=%s%s%s;print s%s(chr(39),s,chr(39),chr(37))';print s%(chr(39),s,chr(39),chr(37)) http ...
- mysql 锁优化
一.myisam存储引擎锁优化 1.合理理由读写优先级MyISAM 的表锁,写互相阻塞的表锁,默认系统是写优先,可改为读有先:low_priority_updates=1如果我们的系统是一个以读为主, ...
- 浅析MVC模式与三层架构的区别01
三层架构和MVC是有明显区别的,MVC应该是展现模式(三个加起来以后才是三层架构中的UI层)三层架构(3-tier application) 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI ...
- php框架制做笔记
在学习完基础之后,最好的提高方式是做一个自己的框架,因为框架会用到各个知识点,在制做过程中,复习,巩固,提高. 在框架中,因为是单入口,整个脚本运行时都存在的变量我们应该设为静态变量,这样它在每个地方 ...
- javascript eval和JSON之间的联系
原出处:http://www.jb51.net/article/21688.htm eval函数的工作原理 eval函数会评估一个给定的含有JavaScript代码的字符串,并且试图去执行包含在字符串 ...
- nginx编译模块详解
–prefix= 指向安装目录–sbin-path 指向(执行)程序文件(nginx)–conf-path= 指向配置文件(nginx.conf)–error-log-path= 指向错误日志目录–p ...
- RTMP协议中文翻译(首发)(转)
Adobe公司的实时消息传输协议 摘要 此备忘录描述了 Adobe公司的实时消息传输协议(RTMP),此协议从属于应用层,被设计用来在适合的传输协议(如TCP)上复用和打包多媒体传输流(如音频.视频和 ...