2017年ICPC中国大陆区域赛真题(下)F题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43735840/article/details/99737695

题目网址点击进入

problem
Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number line, each occupying a different integer. In a single move, one of the outer rabbits jumps into a space between any other two. At no point may two rabbits occupy the same position.
Help them play as long as possible
Input
The input has several test cases. The first line of input contains an integer t (1 ≤ t ≤ 500) indicating the number of test cases.
For each case the first line contains the integer N (3 ≤ N ≤ 500) described as above. The second line contains n integers a1 < a2 < a3 < … < aN which are the initial positions of the rabbits. For each rabbit, its initial position
ai satisfies 1 ≤ ai ≤ 10000.

Output
For each case, output the largest number of moves the rabbits can make.

Sample Input
5
3
3 4 6
3
2 3 5
3
3 5 9
4
1 2 3 4
4
1 2 4 5

Sample Output
1
1
3
0
1

大致题意
输入t组数据,有n只兔子,当兔子位于最两端的时候可以往中间两只兔子之间的数字空隙跳跃,问直到兔子都没位置跳的时候最多所有的兔子可以跳几步?

思路
这题有点贪心的感觉,经常玩跳棋的也许会比较快想到一种办法,一开始两端的兔子都可以跳,那么哪一只跳取决于这只兔子与邻近兔子间隔大小谁比较小,只有比较小的兔子跳了,才可以为之后的跳跃腾出更大的空间,当这只兔子跳跃的时候,只要往邻近兔子的下一个位置跳跃,如145,5跳到3去变成134,只要跳过去一直贴脸,就可以保证下一次最外面的兔子跳的时候距离为0,优先跳跃,134变成123,4跳过去继续贴脸,就可以一直把两数之间的空格填满,所以最大就是所有数字空格总和减去一开始两端兔子相邻数字间隔较小的那个

代码

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
typedef long long int LLD;
LLD rabbit[505];
int main()
{
    LLD t,n;
    scanf("%lld",&t);
    while (t--)
    {
        LLD n,ans=0;
        memset(rabbit,0,sizeof(rabbit));///这个好像没什么用
        scanf("%lld",&n);
        for (LLD i=1;i<=n;i++)
        {
            scanf("%lld",&rabbit[i]);
        }
        for (LLD i=2;i<=n;i++)
        {
            ans+=rabbit[i]-rabbit[i-1]-1;///把所有间隔数字差加起来,记得减去1
        }
        ans=ans-min(rabbit[2]-rabbit[1]-1,rabbit[n]-rabbit[n-1]-1);///减去两头小的那个
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43735840/article/details/99737695