K - Rabbits HDU-6222

本文解析了一种关于兔子在数轴上跳跃的游戏算法。通过分析输入的整数序列,算法计算了兔子能在不占据同一位置的情况下,从两边向内跳跃的最大次数。关键在于计算序列中空隙的数量,即最大值与最小值之差减去序列长度加一。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值