【2017 ICPC亚洲区域赛沈阳站 K】Rabbits(思维)

Problem Description

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

题意:

有n只兔子在不同的位置,任意一只最外面的兔子可以跳到其余任意两只兔子中间空位里,问所有兔子最多可移动的总次数。 

思路:

只要最外面的两只兔子中,有一只兔和其他兔相邻,即它们之间没有空格,那么最大可移动次数就是剩下的空格数之和。

那么问题就很简单了,就判断一下位于端点的两只兔子与它们最近兔子之间的距离,取相对距离小的一组,用总空格数-该组之间的空格数即可。

题目链接:HDU 6227

#include<iostream>
#include<algorithm>
#define MAX 505
using namespace std;
int main()
{
    int t,n,i,a[MAX];
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(i=1;i<=n;i++)
            cin>>a[i];
        int sum=0;
        for(i=2;i<=n;i++)
            sum=sum+a[i]-a[i-1]-1;
        int head=a[2]-a[1]-1;
        int tail=a[n]-a[n-1]-1;
        cout<<sum-min(head,tail)<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/kannyi/p/9694305.html