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

题目网址点击进入

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值