POJ 1804 Brainman
来源:互联网 发布:linux samba进程 编辑:程序博客网 时间:2024/05/16 18:25
Brainman
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8954 Accepted: 4804
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.
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:
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:
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.
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
44 2 8 0 310 0 1 2 3 4 5 6 7 8 96 -42 23 6 28 -100 655375 0 0 0 0 0
Sample Output
Scenario #1:3Scenario #2:0Scenario #3:5Scenario #4:0
这题和上一题简直一样,也是要求逆序对数,方法还是一样:用归并排序求逆序对数,在归并操作之中增加一条语句即可解决,不废话。
import java.util.Scanner;/** * Created by 小粤 on 2015/8/4. */public class Main{ private static int[] aux = new int[1000]; private static int[] numbersOfSequence = new int[1000]; private static int counter = 0; private static void sort(int lo, int hi) { if (lo >= hi) { return; } int mid = (lo + hi) / 2; sort(lo, mid); sort(mid + 1, hi); merge(lo, mid, hi); } private static void merge(int lo, int mid, int hi) { int i = lo, j = mid + 1; for (int k = lo; k <= hi; k++) { aux[k] = numbersOfSequence[k]; } for (int k = lo; k <= hi; k++) { if (i > mid) { numbersOfSequence[k] = aux[j++]; } else if (j > hi) { numbersOfSequence[k] = aux[i++]; } else if (aux[j] < aux[i]) { numbersOfSequence[k] = aux[j++]; counter += mid - i + 1; } else { numbersOfSequence[k] = aux[i++]; } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numberOfScenarios = scanner.nextInt(); for (int i = 0; i < numberOfScenarios; i++) { int lengthOfSequence = scanner.nextInt(); for (int j = 0; j < lengthOfSequence; j++) { numbersOfSequence[j] = scanner.nextInt(); } counter = 0; sort(0, lengthOfSequence - 1); System.out.println("Scenario #" + (i+1) + ":"); System.out.println(counter); System.out.println(); } }}
0 0
- POJ 1804 Brainman
- poj 1804 Brainman
- POJ 1804 Brainman
- POJ 1804 Brainman
- poj 1804 Brainman
- poj 1804 Brainman
- poj 1804:Brainman
- POJ 1804 Brainman
- POJ 1804 Brainman
- POJ 1804 Brainman 笔记
- poj 1804 Brainman(归并)
- poj 1804Brainman(树状数组)
- [POJ 1804] Brainman · 逆序对
- POJ 1804 Brainman(归并排序)
- poj 1804 Brainman(求逆序对)
- poj 1804 Brainman(归并排序求逆序对)
- POJ 1804 Brainman (归并排序 -- 求逆序对数)
- poj 1804 (归并排序求逆序数)Brainman
- 点的变换(矩阵转化)
- 最大二分匹配
- JavaScript基础----12Javascript函数-定义函数
- hdu1757(矩阵加速)
- 网络爬虫URL消重
- POJ 1804 Brainman
- 创新工场老板李开复讲创业中需要用到的冥定律!
- HDU OJ Max sum 题目1003
- JavaScript基础----13Javascript函数-调用函数
- C#通过PHP向MySQL插入数据的编码问题
- 八月六日 Linux操作系统学习笔记
- JavaScript基础----14Javascript函数-带参数的函数
- 编写主引导扇区代码
- 第十五天 简单聊天