Brainman

题目链接(点击)

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12942   Accepted: 6504

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

思路:

目的:给出序列让计算出  通过 相邻数字的移位 实现序列  从小到大排列  所需要的总移动次数

在给出的序列中 没有可以更多可以利用的条件 既然是要排序 就试着找排序后位次和现在的位次的关系:

例:          2 8 0 3   是已知的序列

排序后: 0 2 3 8

其中 2的位次变化:   1→2  差值: 1

8:    2→4              2

0:    3→1             -2

3:    4→3             -1

差值相加为零可以理解,但同号之间的关系是不是直接相加就可以?

用题目示例3:可以证明不对

仔细想了想是因为:(下面是模拟的过程)

如果用开始的方法直接相加结果是0+4

但其实是0+1+4:

因为-100 最终在6 的前面 所以6 的位置就会由3 变成4 然后4再变成3 这样是1 而不是0(看不懂直接看规律)

所以规律是:从前往后移动计算前后序号差之和再加上在该位置后面比它小或相等值的个数

例如: -42 23 6 28 -100 65537

4+0+1(在6后面有1个-100比它小)

AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAX=1e3;
struct node{
LL count;
LL num1;
LL num2;
}edge[MAX+5];
int main()
{
LL b[MAX+5],T;
scanf("%lld",&T);
for(LL k=1;k<=T;k++){
LL n;
scanf("%lld",&n);
for(LL i=1;i<=n;i++){
scanf("%lld",&edge[i].count);
edge[i].num1=i;
b[i]=edge[i].count;
}
sort(b+1,b+n+1);
for(LL i=1;i<=n;i++){
for(LL j=1;j<=n;j++){
if(b[i]==edge[j].count){
edge[j].num2=i;
}
}
}
LL sum=0;
printf("Scenario #%lld:\n",k);
for(LL i=2;i<=n;i++){
if(edge[i].count<edge[i-1].count){
LL count1=0;
for(LL j=i+1;j<=n;j++){
if(edge[i].count>=edge[j].count){
count1++;
}
}
sum+=(edge[i].num1+count1-edge[i].num2);
edge[i].count=edge[i-1].count;
}
}
printf("%lld\n\n",sum);
}
return 0;
}

最新文章

  1. 构建自己的PHP框架--实现Model类(3)
  2. (七)WebGIS中栅格、矢量图层设计之栅格、矢量图层的本质
  3. string转byte[]
  4. PHP开发环境配置~Windows 7 IIS
  5. rabbitMQ学习(三)
  6. STL之lower_bound和upper_bound
  7. atitit.提升2--3倍开发效率--cbb体系的建设..
  8. 2015安徽省赛 A.First Blood
  9. Common Configration实验
  10. 详解CSS设置默认字体样式
  11. 下 面 这 条 语 句 一 共 创 建 了 多 少 个 对 象 : String s=&quot;a&quot;+&quot;b&quot;+&quot;c&quot;+&quot;d&quot;;
  12. Libevent API
  13. 几种在shell命令行中过滤adb logcat输出的方法
  14. linux中的颜色控制
  15. 学JAVA第十七天,接口与抽象方法
  16. 题解-SDOI2015 约数个数和
  17. JS引用类型之Array
  18. Gradle学习笔记(1)创建简单的Java项目
  19. native.js 判断是否安装某app
  20. SpringMVC关于ajax提交400错误(后台获取为null)

热门文章

  1. python脚本实现接口自动化轻松搞定上千条接口用例
  2. Pyqt5_QlineEdit
  3. 织梦系统dedecms实现列表页双样式,列表样式循环交替变化
  4. h5 js判断是IOS系统还是android系统
  5. [前端开发]Vue mixin
  6. 蒲公英 &#183; JELLY技术周刊 Vol.08 -- 技术周刊 &#183; npm install -g typescript@3.9.3
  7. JavaScript计时
  8. indetityserver4-implicit-grant-types-请求流程叙述-下篇
  9. 一个神秘URL酿大祸,差点让我背锅!
  10. Java实现洛谷 P1007独木桥