K - Rabbits HDU-6222

版权声明:如需转载,请注明出处: https://blog.csdn.net/qq_38701476/article/details/83036137

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个从小到大的整数,边上的端点每次能够跳到任意两个点之间的,任意两个点不能够在同一位置,求出最多能够跳多少次.

  • 解题思路:对与一组数,如果有另一个数往里面跳,为使能够跳的最多次,那么应该尽可能的紧挨着这组数的两端的空处,而最多能够跳的次数就是里面的空格数(既这组数最大值ma与最小值mi之差减去这组数的个数(n-1) 再+1) .

例如数组a[n];
如果第一个选a[0]第一个跳的话,那最多能跳的个数a[n-1]-a[1]+1-(n-1).
a[n-1]-a[1]+1,为一共有多少个位置,(n-1)为已经占据了的位置

同理如果第一个选a[n-1]跳的话,最多能跳a[n-1]-a[0]-(n-1)+1;

#include<bits/stdc++.h>
#define mk make_pair
#define endl '\n'
#define pb pusk_back
#define _ ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const double EPS = 1e-10;
const double PI = acos(-1);
bool SUBMIT = 1;
int n,sn[510];
int main()
{
	if(!SUBMIT)freopen("i.txt","r",stdin);else _;	
	int t;cin>>t;
	while(t--){
		cin>>n;
		for(int i=0;i<n;i++)cin>>sn[i];
		int ans1=0,ans2=0;
		ans1=sn[n-1]-sn[1]+1-(n-1);
		ans2=sn[n-2]-sn[0]+1-(n-1);
		cout<<max(ans1,ans2)<<endl;

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38701476/article/details/83036137