归并求逆序数(逆序对数) && 线段树求逆序数
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个元素,你的 ...
随机推荐
- 在Android开发中如何判读当前设备是否连接网络
1:前言: 我们在Android开发的过程中,很多实现是要向远程服务器拿数据的,但是未必当前设备一定连接了网络啊,那么此时我们就是要进行判断的了, 如果是有网络的话,那么此时就去向远程服务器去拿数据, ...
- KVC 与 KVO
一.Key-Value Coding (KVC)键值编码 KVC,即是指 NSKeyValueCoding,一个非正式的 Protocol,提供一种机制来间接访问对象的属性.KVO 就是基于 KVC ...
- workflow GetListIdByName 获取表名
1.Assign 获取表的地址 和表名 2.HttpsendWithSuspend==HttpSend 3.ParseDynamicValue 4.GetDynamicValuePropertie ...
- Linux网络栈下两层实现
http://www.cnblogs.com/zmkeil/archive/2013/04/18/3029339.html 1.1简介 VLAN是网络栈的一个附加功能,且位于下两层.首先来学习Linu ...
- C#方法中三个重要的参数:out、ref、params
备注:适用于初学者,自学于传智播客. 1.out参数. 概念:如果在一个方法中,返回多个相同类型值的时候,可以考虑返回一数组.但是返回多个不同类型值的时候,返回数组显然不能解决问题,这时就引入out参 ...
- win7 64的系统安装。net4.0总是提示安装未成功
主要原因是Windows update的临时文件损坏,建议重命名该文件夹. 1. 开始——运行——cmd——键入net stop WuAuServ回车(停止windows update服务): 2. ...
- DbProviderFactory .net数据库工厂模式
http://kb.cnblogs.com/page/72789/ 工厂模式 http://www.cnblogs.com/Ruiky/archive/2012/04/19/2456784.html ...
- Keep It Simple Stupid!
Kelly Johnson提出了KISS原则.他是一个飞机工程师以及航空发明家,同时也是一个管理天才,他一生中主要设计了40多架飞机,获得的荣誉相当之多,总之,很牛. 这个原则是对Johnson带领的 ...
- 【温故Delphi】双击工程文件打开软件
问题描述 大部分软件都有鼠标双击文件,就能打开所关联的软件并打开所选工程,这是如何做到的呢? 把文件关联到一个程序中,双击文件来启动程序,那么这个文件名称就是这个程序的命令行的一个参数. 所以要想实现 ...
- 用nexus搭建maven私服
首先介绍一下背景,公司访问外网有限制,项目组大部分人员不能访问maven的central repository,因此在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到这台私服上 环 ...