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 ...
随机推荐
- 深入理解c++构造函数, 复制构造函数和赋值函数重载(operator=)
注 以下代码编译及运行环境均为 Xcode 6.4, LLVM 6.1 with GNU++11 support, Mac OS X 10.10.2 调用时机 看例子 // // main.cpp / ...
- 错误The request sent by the client was syntactically incorrect ()的解决
http://www.cnblogs.com/xiandedanteng/p/4168609.html 这个错误是SpringMVC报出来的,见到它意味着html/jsp页面的控件名称 和 contr ...
- Linux kernel develop -- Hello World
hello.c: #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h&g ...
- iOS YSDropdownMagnify 下拉放大,向上导航显示
要实现的效果如上.在实际开发中,我们会使用到三种方式来实现. 通过隐藏导航栏,自定义导航View 改变原生导航栏背景透明 原生导航栏通过添加背景图片改变 个人是比较喜欢第二种. github下载地址: ...
- js函数的调用问题
1.js函数的调用方式有三种.请问以下“二”处的几行代码有什么猫腻? //一 事件调用 btn.onclick=fn; //二 直接调用(window调用) fn(); //自上而下解析到这一行的时候 ...
- 共享jQuery/Eclipse/SVN/PS/DW/的API文档
1:jQuery的API和jquery.js和jquery.min.js 链接:http://pan.baidu.com/s/1gf7GD83 密码:tbt1 2:虚拟机软件和frdora和ubunt ...
- C#:Oracle数据库带参PLSQL语句的正确性验证
在有Oracle数据库C#项目中,有一个这样的需求:在界面上配置了带参数的PLSQL语句,但是要通过程序验证其正确性,那么该如何实现?这就是本文要探讨的内容. 一:通过OracleCommand对象的 ...
- python类的特性
#encoding=utf-8 class Province: #静态字段 memo = '这里是静态变量' def __init__(self,name,capital,leader,flag): ...
- ORA-01033:ORACLE initialization or shutdown in progress
借用他人的经验 客户Oracle服务器进入PL/SQL Developer时报ora-01033:oracle initializationg or shutdown in progress 错误提示 ...
- 奇怪吸引子---ShimizuMorioka
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...