Brainman
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7787   Accepted: 4247

Description

Background 
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

The first line contains the number of scenarios. 
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

Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. 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
题目大意:归并排序求逆序对。
#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的更多相关文章

  1. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  2. POJ 1804 Brainman(归并排序)

    传送门 Description Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted ...

  3. poj 1804 (nyoj 117)Brainman : 归并排序求逆序数

    点击打开链接 Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7810   Accepted: 4261 D ...

  4. 【POJ 1804】 Brainman

    [题目链接] 点击打开链接 [算法] 本题是一个很经典的问题 : 归并排序求逆序对数,可以用分治算法解决 分治,分而治之,分治算法的思想就是将一个问题转化为若干个子问题,对这些子问题分别求解,最后, ...

  5. POJ 1840 Brainman(逆序对数)

    题目链接:http://poj.org/problem?id=1804 题意:给定一个序列a[],每次只允许交换相邻两个数,最少要交换多少次才能把它变成非递降序列. 思路:题目就是要求逆序对数,我们知 ...

  6. poj 1084 Brainman(归并排序)

    题目链接:http://poj.org/problem?id=1804 思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可. 根据分治法思想,序列分为两个大小相等的两部分,分别求子序列的逆 ...

  7. POJ 1804 逆序对数量 / 归并排序

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12175   Accepted: 6147 Descrip ...

  8. POJ 1804

    /* *由此题发现规律 就是冒泡排序的交换次数等于这个数列的逆序对 的对数! */ #include<iostream> #include<stdio.h> #include& ...

  9. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

随机推荐

  1. ThreadLocal线程范围内的共享变量

    模拟ThreadLocal类实现:线程范围内的共享变量,每个线程只能访问他自己的,不能访问别的线程. package com.ljq.test.thread; import java.util.Has ...

  2. 文件系统:Ext3和Ext4

    一.ext3和ext4的区别: 1.与Ext3兼容:执行若干条命令,就能将Ext3在线迁移到Ext4,而无须重新格式化磁盘或者重新安装系统.原有Ext3数据结构照样保留, Ext4作用于新数据,当然, ...

  3. 聊聊 App Store 的产品和推广运营攻略

    在这个工匠的时代,越来越多开发者投入了手机应用的开发圈子.如何才能在激烈的竞争中脱颖而出,少走弯路呢?我们跟空中网负责iPhone游戏的运营和推广的洪亮进行了一次交流,得到了不少经验和诀窍,值得分享给 ...

  4. js系列(10)js的运用(二)

        本节继续介绍在html页面中js的运用.   (1)数码时钟:(http://files.cnblogs.com/files/MenAngel/text05.zip) <!DOCTYPE ...

  5. C#类、接口、虚方法和抽象方法0322

    虚拟方法和抽象方法有什么区别与联系: 1.抽象方法只有声明没有实现代码,需要在子类中实现:虚拟方法有声明和实现代码,并且可以在子类中重写,也可以不重写使用父类的默认实现. 2.抽象类不能被实例化(不可 ...

  6. Python:字符编码详解

    相关文章 Python中文编码问题:为何在控制台下输出中文会乱码及其原理 1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Informati ...

  7. 流媒体选择Nginx是福还是祸?

    CDN,视频云,已经“僧多粥少” 视频直播的持续升温,无意间也让带宽生意的争夺变得异常残酷.一时间,各种云计算.CDN.视频云提供商都在视频尤其是直播上投入重兵,揭竿而起的新生起义军们也正马不停蹄的赶 ...

  8. WPF与winform与silverlight的区别

    收到了一封学生的邮件: =========================== 金老师您好: 最近在学C#.NET,基本语法学习的差不多了,接下来准备学习图形界面设计部分.但是我目前对于.NET的Wi ...

  9. YAFFS2文件系统分析(转)

    http://blog.chinaunix.net/uid-25314474-id-343665.html 1.前言略. 2.yaffs 文件系统简介按理说这里应该出现一些诸如“yaffs 是一种适合 ...

  10. HashSet HashTable HashMap的区别 及其Java集合介绍

    (1)HashSet是set的一个实现类,hashMap是Map的一个实现类,同时hashMap是hashTable的替代品(为什么后面会讲到). (2)HashSet以对象作为元素,而HashMap ...