POJ 1804 Brainman
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 7787 | Accepted: 4247 |
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
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 <stdio.h> int num[];
int temp[];
int ans = ; void Merge(int low, int mid, int high)
{
int i = low, j = mid + , k = ;
while(i <= mid && j <= high)
{
if (num[i] <= num[j])
{
temp[k] = num[i];
i++;
k++;
}
else
{
ans += mid - i + ;
temp[k] = num[j];
j++;
k++;
}
}
while(i <= mid)
{
temp[k] = num[i];
i++;
k++;
}
while(j <= high)
{
temp[k] = num[j];
j++;
k++;
}
for (k = , i = low; i <= high; k++, i++)
{
num[i] = temp[k];
}
} void MergeSort(int lwo, int high)
{
if (lwo < high)
{
int mid = (lwo + high) / ;
MergeSort(lwo, mid);
MergeSort(mid + , high);
Merge(lwo, mid, high);
}
} int main()
{
int nCase, n, nCount = ;
scanf("%d", &nCase);
while(nCase--)
{
scanf("%d", &n);
ans = ;
for (int i = ; i < n; i++)
{
scanf("%d", &num[i]);
}
MergeSort(, n - );
printf("Scenario #%d:\n%d\n\n", ++nCount, ans);
}
return ;
}
POJ 1804 Brainman的更多相关文章
- POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10575 Accepted: 5489 Descrip ...
- POJ 1804 Brainman(归并排序)
传送门 Description Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted ...
- poj 1804 (nyoj 117)Brainman : 归并排序求逆序数
点击打开链接 Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7810 Accepted: 4261 D ...
- 【POJ 1804】 Brainman
[题目链接] 点击打开链接 [算法] 本题是一个很经典的问题 : 归并排序求逆序对数,可以用分治算法解决 分治,分而治之,分治算法的思想就是将一个问题转化为若干个子问题,对这些子问题分别求解,最后, ...
- POJ 1840 Brainman(逆序对数)
题目链接:http://poj.org/problem?id=1804 题意:给定一个序列a[],每次只允许交换相邻两个数,最少要交换多少次才能把它变成非递降序列. 思路:题目就是要求逆序对数,我们知 ...
- poj 1084 Brainman(归并排序)
题目链接:http://poj.org/problem?id=1804 思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可. 根据分治法思想,序列分为两个大小相等的两部分,分别求子序列的逆 ...
- POJ 1804 逆序对数量 / 归并排序
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12175 Accepted: 6147 Descrip ...
- POJ 1804
/* *由此题发现规律 就是冒泡排序的交换次数等于这个数列的逆序对 的对数! */ #include<iostream> #include<stdio.h> #include& ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
随机推荐
- ubuntu14.04中文楷体变默认字体
使用ubuntu以来,最让人头疼的事情就是在英文系统里面使用中文,一般中文字体都很难看,要么有锯齿,要么就是楷体.经过网上搜索找到一堆方法.一个个尝试之后觉得以下方式是最简单有效的. 1.安装font ...
- JS 日期格式化和解析工具
本来想模仿Java里面的SimpleDateFormat()对象的,但是感觉这样用起来不方便,所以还是直接写成单独的方法算了. 原文链接 日期格式化 使用说明 formatDate(date, fmt ...
- LINQ-to-SQL那点事~LINQ-to-SQL中的数据缓存与应对
回到目录 这个文章写的有点滞后了,呵呵,因为总想把之前不确定的东西确定了之后,再写这篇,之前的LINQ-to-SQL那点事,请点这里. LINQ-to-SQL中的数据缓存与应对 Linq-to-SQL ...
- atitit.跨平台gui 概览
atitit.跨平台gui 概览 为什么需要跨平台gui 国际上那些跨平台的GUI程序,除了像Firefox之类的大型项目会重写界面外,中小型的项目基本上都是用GTK+或WxWidgets为多.毕竟要 ...
- 解决 "Windows 无法启动Mongo DB服务 错误:1067 进程意外终止"
在启动MongoDB服务时,有时会报上图所示的错误,解决方案为: 1. MongoDB安装目录\data\将此文件夹下的mongod.lock删除 2. 查看官方文档或按照上一篇安装文章检查是否设置d ...
- TF Boys (TensorFlow Boys ) 养成记(四)
前面基本上把 TensorFlow 的在图像处理上的基础知识介绍完了,下面我们就用 TensorFlow 来搭建一个分类 cifar10 的神经网络. 首先准备数据: cifar10 的数据集共有 6 ...
- new一个Object对象占用多少内存?
Java的自动内存管理机制省却了很多编码工作,大大地提高了Java的生产力,而且JVM的性能也越来越好,特别是G1的出现,改善了垃圾回收中stop the world的状况. 也许很多人都没有考虑过这 ...
- 整站HTTPS后的跨域请求 CORS是否还有效?
| 导语 手Q马上就要全量https了,很多业务都有跨域ajax请求的需求,原来使用的CORS头在HTTPS环境中还继续能用吗?我搜遍了谷歌.百度,都没看到有明确的答案,那么就自己来尝试一下吧. 关 ...
- Prototype in JavaScript
声明 本文旨在入门,简单了解下何为prototype & __proto__ 原型对象 我们创建每个函数都有个prototype(原型)属性,该属性是一个指针,指向一个对象,而这对象的用途是包 ...
- linux nc (NetCat) 命令详解
原文:http://www.huanxiangwu.com/477/linux-nc-netcat 一.版本通常的Linux发行版中都带有NetCat(简称nc),甚至在拯救模式光盘中也由busybo ...