POJ 1804 Brainman

时间:2022-09-28 16:06:28
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&lpar;5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】&rpar;

    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&lpar;归并排序&rpar;

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

  7. POJ 1804 逆序对数量 &sol; 归并排序

    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. JAVAWEB监听器(二)

    监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事 ...

  2. 1065&colon; &lbrack;NOI2008&rsqb;奥运物流 - BZOJ

    Sample Input4 1 0.52 3 1 310.0 10.0 10.0 10.0Sample Output30.00 推荐题解:http://blog.csdn.net/whjpji/art ...

  3. Palindrome(poj3974)(manacher算法)

    http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...

  4. MYSQL学习笔记3--mysql 2PC二阶段协义 与 日志闪回

    mysql两份日志: binlog :server innodb redo log:engine 两份日志顺序一致性:否则主备不一致 两份日志:原子性,同时都有,同时都无 2PC二阶段协义: 第一阶段 ...

  5. flask-login使用笔记

    看外国文献的中文翻译  翻译的程度有的让人会疯,翻译最好的状态是异译 直译会显的很生硬 看起来确实难过:所以在看的时候,建议都看外国文献吧,或者自己用谷歌翻译,感觉比一些翻译的博客准多了: 在使用fl ...

  6. 网站发布出现&OpenCurlyDoubleQuote;未能找到路径&OpenCurlyDoubleQuote;path&bsol;bin&bsol;roslyn&bsol;csc&period;exe”&period;&period;&period;&period;&OpenCurlyDoubleQuote;和拒绝访问的解决办法

    最近在2017上新建了一个MVC项目,发布是出现了各种奇怪的问题,其中一个错误是: 未能找到路径“path\bin\roslyn\csc.exe”.... 经过网上搜寻资料发现罪魁祸首就是NUGET里 ...

  7. Python 的 setitem、getitem、delitem 特殊方法使用

    简介 setitem:当属性被以索引方式赋值的时候会调用该方法 getitem:一般如果想使用索引访问元素时,就可以在类中定义这个方法 delitem:当使用索引删除属性时调用该方法 实例 __Aut ...

  8. UVA 1592 DataBase

    思路: 知识补充: ①make_pair和pair: /*pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存.另一个应用 ...

  9. ABAP表抛FTP通用程序

    主要功能: 1.支持R3所有表(标准.自建)下传,下传方式为FTP 2.支持输出字段选择及顺序调整 3.支持动态条件,不同的表会有不同的选择条件,根据不同的条件选择需要下传的数据 4.支持单表.多表. ...

  10. 第七次Scrum冲刺

    第七次Scrum冲刺 1.今日完成的任务 队员 今日完成任务 刘佳 前端与后端对接 李佳 后端与数据库对接 周世元 数据库与后端对接 杨小妮 博客编写 许燕婷 管理团队当日及次日任务 陈水莲 综合测试 ...