poj 1804 (nyoj 117)Brainman : 归并排序求逆序数
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 7810 | Accepted: 4261 |
Description
Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He
wants to beat his brother in a similar task.
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 8
So 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 8
The 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
For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.
Output
Terminate the output for the scenario with a blank line.
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
归并排序求出逆序数,拿的以前写好的模板,速度还不错,不过发现之前模板有一个错误,就是合并完以后并没有释放new的空间,在POJ上运行没问题,就是内存大一点,但是在NYOJ上,如果不delete就会MLE,两题的格式不一样,以下是POJ的AC代码,NYOJ需要修改输出格式才能AC
#include<stdio.h>
#include<iostream>
using namespace std;
int array[1000001];
long long flag = 0;
void merg(int head, int tail)
{
int mid = (tail + head) / 2 + 1;
int * new_array = new int[(tail - head) + 1];
int top1 = head;
int top2 = mid;
int i;
for(i = 0; top1 < mid && top2 <= tail ; i++)
{
if(array[top1] > array[top2])
{
new_array[i] = array[top2];
top2 ++;
}
else
{
new_array[i] = array[top1];
flag += top2 - (mid);
top1 ++;
}
}
if(top1 == mid && top2 <= tail)
{
while(top2 <= tail)
new_array[i++] = array[top2++];
}
else if(top1 != mid && top2 > tail)
{
while(top1 < mid)
{
new_array[i++] = array[top1++];
flag += tail - (mid) + 1;
}
}
memcpy(&array[head], new_array, sizeof(int) * (tail - head + 1) );
delete new_array;
}
void mergsort(int head, int tail)
{
if(head >= tail)
return ;
mergsort(head, (head + tail) / 2);
mergsort((head + tail) / 2 + 1, tail);
merg(head, tail);
}
int main()
{
int n;
int m;
// freopen("test.txt", "r", stdin);
scanf("%d", &m);
int j;
for(j = 1; j <= m; j++)
{
printf("Scenario #%d:\n", j);
scanf("%d", &n);
int i;
flag = 0;
for(i = 0; i < n; i++)
scanf("%d", &array[i]);
mergsort(0, n - 1);
printf("%lld\n\n", flag);
}
return 0;
}
poj 1804 (nyoj 117)Brainman : 归并排序求逆序数的更多相关文章
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- HDU 3743 Frosh Week(归并排序求逆序数)
归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
- POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 39279 Accepted: 14163 ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- poj2299解题报告(归并排序求逆序数)
POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...
随机推荐
- 【转】DNS劫持和DNS污染的区别
什么是DNS服务器? 简单来说,DNS服务器就是域名管理系统. DNS(Domain Name System)是域名解析服务器的意思. DNS服务器是干什么的? DNS服务器在互联网的作用是:把域名转 ...
- 淘宝天猫关键词SEO优化
淘宝天猫的网站完全像是一个成熟的搜索引擎,只是从google.bing.baidu改成了淘宝天猫而已,普通搜索引擎有品专,有皇冠,有PC,有无线:淘宝天猫里面有钻展,有直通车,也有PC,无线.搜索引擎 ...
- Apache Kafka 分布式消息队列中间件安装与配置 转载
bin/zkServer.sh start /home/guym/down/kafka_2.8.0-0.8.0/config/zookeeper.properties& bin/kafka-s ...
- 如何破解海蜘蛛ISP6.1.5 极其isp运营商 v6.1.5
海蜘蛛ISPV6.1.5,目前破解版本中最稳定的!破解步骤如下:一.安装完毕进控制台二.使用muddyboot登陆 密码(123456)三.输入root回车四.输入regtools回车五.在web控制 ...
- Python基础教程【读书笔记】 - 2016/7/4
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第二波:第7章 更加抽象 [总览] 创建自己的对象,是Python的核心概念!Python被称为面向对象的语言.介绍如 ...
- 通信原理读书笔记:常规AM调制的功率
Proakis,通信系统原理,p101: 两个不同频率正弦和的功率为其功率的和. 计算功率时,和的平方展开后会出现两个正弦乘积项,按积化和差展开后在公共周期内积分为零.
- NOR Flash擦写和原理分析 (二)
Nor Flash上电后处于数据读取状态(Reading Array Data).此状态可以进行正常的读.这和读取SDRAM/SRAM/ROM一样.(要是不一样的话,芯片上电后如何从NorFlash中 ...
- title与h1标签的区别和联系
很多新站长在网站SEO过程中,会认为把H1等同于Title. 其实两是有区别和联系的,两者不能划等号.下面主要从文章和页面角度分析title和H1. H1等同于title吗? H1不等于title.H ...
- 黄聪:WordPress固定链接设置的几种方法
wordpress固定链接设置的一些参数: %year%:基于文章发布的年份,比如2010: %monthnum%:基于文章发布的月份,比如01: %day%:基于文章发布当日,比如06: %hour ...
- System.InvalidOperationException: Sequence contains no elements
foreach (var keyCode in unexpectedKeyCodesDetected) { string unexpected = expectedCapturedKeyCodes.W ...