归并求逆序数(逆序对数) && 线段树求逆序数
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个元素,你的 ...
随机推荐
- Python之Scrapy爬虫框架安装及简单使用
题记:早已听闻python爬虫框架的大名.近些天学习了下其中的Scrapy爬虫框架,将自己理解的跟大家分享.有表述不当之处,望大神们斧正. 一.初窥Scrapy Scrapy是一个为了爬取网站数据,提 ...
- ROS实时采集Android的图像和IMU数据
前言 临近毕业,整理一下之前做的东西.这篇博客来自于博主在2016年3月份投的一篇会议论文(论文主要介绍了一个基于手机摄像头和IMU的简单VIO系统,用于AR的Tracking部分,本博文 ...
- css3新特性@rgba
1.rgba也经常在实际应用中使用,它主要是在原来rgb的基础上添加了一透明度.但是他又和opacity又有一些差别,主要体现在对子元素的透明度的影响上. 例如:使用opacity和backgroun ...
- myql数据库在cmd下,中文乱码的问题原因
使用navicat把数据导入数据库,这些数据都是中文,导入成功,显式也正常,但是在mysql cmd下都是乱码.检查了我的mysql配置,字符编码都是utf8,包括navicat连接时候也设置过是ut ...
- Linux系统编程重要细节记录(持续更新中)
1.在打印rlim_t值时,需要将其转换为long long并使用%lld printf()修饰符.
- Javascript 处理时间大全
1. 获取从今天算起,几天后的日期 function GetDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate() + ...
- IE6的连接数限制问题
今天解决了一个bug.看似是UI的bug,最后发现IE的设置问题(严格来说,IE6这么做没有问题,因为HTTP协议的规范如此). 先描述一下问题: 有一个页面管理Job,选中一些Job可以Run,每次 ...
- maven 三个基本插件 clean dependency compiler
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 用python画xy散点图
import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,6],'ro') plt.show()#这个智障的编辑器 这样的话,就可以画一个散点图,图中 ...
- android杂记
1.ArrayAdapter requires the resource ID to be a TextView问题 listView.setAdapter(new ArrayAdapter<S ...