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;
}

Brainman(规律题)【数学思想】的更多相关文章

  1. Codeforces - 规律题 [占坑]

    发现自己容易被卡水题,需要强行苟一下规律题 CF上并没有对应的tag,所以本题集大部分对应百毒搜索按顺序刷 本题集侧重于找规律的过程(不然做这些垃圾题有什么用) Codeforces - 1008C ...

  2. 数学思想:为何我们把 x²读作x平方

    要弄清楚这个问题,我们得先认识一个人.古希腊大数学家 欧多克索斯,其在整个古代仅次于阿基米德,是一位天文学家.医生.几何学家.立法家和地理学家. 为何我们把 x²读作x平方呢? 古希腊时代,越来越多的 ...

  3. LightOJ1010---Knights in Chessboard (规律题)

    Given an m x n chessboard where you want to place chess knights. You have to find the number of maxi ...

  4. ACM_送气球(规律题)

    送气球 Time Limit: 2000/1000ms (Java/Others) Problem Description: 为了奖励近段时间辛苦刷题的ACMer,会长决定给正在机房刷题的他们送气球. ...

  5. hdoj--1005--Number Sequence(规律题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. Visible Lattice Points(规律题)【数学规律】

    Visible Lattice Points 题目链接(点击) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9031   ...

  7. 1005:Number Sequence(hdu,数学规律题)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  8. 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】

    KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. codeforces round#524 D - Olya and magical square /// 大概算是数学规律题?

    题目大意: t 个测试用例  (1≤t≤103) 给定n k  (1≤n≤10^9,1≤k≤10^18) 表示有一个边长为2^n的正方形格子 每次操作只能将一个格子切割为左上左下右上右下的四等分格子 ...

随机推荐

  1. [ES6系列-04]再也不乱“哇”了:用 let 与 const 替代 var

    [原创]码路工人 Coder-Power 大家好,这里是码路工人有力量,我是码路工人,你们是力量. github-pages 博客园cnblogs 今天的内容是,关于 JavaScript 中定义变量 ...

  2. 12.Java连接Redis_Jedis_常用API

    上一篇总结我们使用我们本地的Eclipse中创建的jedis工程,链接到了我们处于VMware虚拟机上的Linux系统上的Redis服务,我们接下来讲一下jedis的一些常用的API. (1)jedi ...

  3. [安卓基础] 001.学习Android开发的好教程

    如果想自学android,有许多不错的android网站.这里收集了一些,列举如下: 国内 极客学院,这里有非常丰富的视频教程. http://www.jikexueyuan.com/course/a ...

  4. angularjs 路由切换回到顶部

    angularjs路由切换  页面不会回到顶部 问题: 在angularjs中  ui-sref或者$state.go(),通过路由切换页面,发现新打开的路由页面仍然停留在上一次的路由页面访问的位置. ...

  5. PHP获取临时文件的目录路径

    PHP获得临时文件的文件目录相对路径,能够 根据tempnam()和sys_get_temp_dir()函数来完成. 下边我们运用简单的代码实例,给大伙儿介绍PHP获得临时文件的文件目录相对路径的方式 ...

  6. Netty源码学习系列之1-NioEventLoopGroup的初始化

    前言 NioEventLoopGroup是netty对Reactor线程组这个抽象概念的具体实现,其内部维护了一个EventExecutor数组,而NioEventLoop就是EventExecuto ...

  7. 网络编程TCP/IP详解

    网络编程TCP/IP详解 1. 网络通信 中继器:信号放大器 集线器(hub):是中继器的一种形式,区别在于集线器能够提供多端口服务,多口中继器,每个数据包的发送都是以广播的形式进行的,容易阻塞网络. ...

  8. Codeforces Round #635 (Div. 2)部分(A~E)题解

    虽然打的是div1,但最后半小时完全处于挂机状态,不会做1C,只有个 \(O(n^3)\) 的想法,水了水论坛,甚至看了一下div2的AB,所以干脆顺便写个div2的题解吧,内容看上去还丰富一些(X) ...

  9. 【项目】关于TeenCode第二代评测机的技术分析

    晚上睡不着觉,仔细研读了洛谷的第四代评测机技术分析后,突然发现自己写的TeenCode评测机竟然有这么多地方可以改进,这不得不让我诞生了实现第二代TeenCode评测机的想法.[第一代评测机挺可怜的, ...

  10. Rocket - tilelink - Parameters

    https://mp.weixin.qq.com/s/1I6DcONr0Mg7xiX8F1C7SQ   简单介绍TileLink相关的参数实现(具体问题暂时不展开,后续用到时再做分析).   ​​   ...