Brainman
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7810   Accepted: 4261

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

归并排序求出逆序数,拿的以前写好的模板,速度还不错,不过发现之前模板有一个错误,就是合并完以后并没有释放new的空间,在POJ上运行没问题,就是内存大一点,但是在NYOJ上,如果不delete就会MLE,两题的格式不一样,以下是POJ的AC代码,NYOJ需要修改输出格式才能AC

#include<stdio.h>
#include<iostream>
using namespace std;
int array[1000001];
long long flag = 0;
void merg(int head, int tail)
{
int mid = (tail + head) / 2 + 1;
int * new_array = new int[(tail - head) + 1];
int top1 = head;
int top2 = mid;
int i;
for(i = 0; top1 < mid && top2 <= tail ; i++)
{
if(array[top1] > array[top2])
{
new_array[i] = array[top2];
top2 ++;
}
else
{
new_array[i] = array[top1];
flag += top2 - (mid);
top1 ++;
}
}
if(top1 == mid && top2 <= tail)
{
while(top2 <= tail)
new_array[i++] = array[top2++];
}
else if(top1 != mid && top2 > tail)
{
while(top1 < mid)
{
new_array[i++] = array[top1++];
flag += tail - (mid) + 1;
}
}
memcpy(&array[head], new_array, sizeof(int) * (tail - head + 1) );
delete new_array;
}
void mergsort(int head, int tail)
{
if(head >= tail)
return ;
mergsort(head, (head + tail) / 2);
mergsort((head + tail) / 2 + 1, tail);
merg(head, tail);
}
int main()
{
int n;
int m;
// freopen("test.txt", "r", stdin);
scanf("%d", &m);
int j;
for(j = 1; j <= m; j++)
{
printf("Scenario #%d:\n", j);
scanf("%d", &n);
int i;
flag = 0;
for(i = 0; i < n; i++)
scanf("%d", &array[i]);
mergsort(0, n - 1);
printf("%lld\n\n", flag);
}
return 0;
}

最新文章

  1. no suitable HttpMessageConverter found for request type [java.lang.Integer]
  2. VS2008上借助VA来提示QT API
  3. ubuntu之使用sublime text3搭建Python IDE
  4. i.BIO方式的SSL通道流程
  5. IOS开发之网络编程--文件压缩和解压缩
  6. 360手机助手内部资料曝光,63张PPT纯干货
  7. [HTTP] Origins, CROS, Preflight
  8. 关于var(string)++的类型自动转换
  9. Tamper Data 安装与使用
  10. ansible 检查大量客户端的文件与配置情况
  11. python基础——面向过程的编程思想及举例
  12. Android Studio 2.2 新功能详解
  13. Python科学计算基础包-Numpy
  14. bash 字符串处理
  15. Jenkins和pipeline
  16. .net core vs2015 vs2017打开后errpr
  17. bzoj 2936 [Poi 1999] 降水 - 并查集
  18. GoLang学习控制语句之switch
  19. BERT总结:最先进的NLP预训练技术
  20. 接口自动化测试框架-AIM

热门文章

  1. 【转】深入PHP FTP类的详解
  2. AMD规范:define和require的区别
  3. 更简单的跨域解决方案 - CORS
  4. azure 云上MySQL最新版本 MySQL5.7.11 批量自动化一键式安装 (转)
  5. C#限制程序只能运行一個实例 (防多开)
  6. linux挂载文件
  7. android学习笔记48——SQLite
  8. ARM各种版本号知识以及型号的发展(三星为例)
  9. string和stringBuilder区别
  10. eclipse调试总结(转)