Hdu 6227 Rabbits

本文解析了一道关于兔子游戏的算法题目,通过合理的数学模型帮助理解如何让兔子们尽可能长时间地进行跳跃游戏。输入包括多组测试案例,每组案例包含兔子的数量及初始位置,输出则是最大可能的游戏轮数。

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

Rabbits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 126    Accepted Submission(s): 75


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

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 500 + 10;
int s[N];
int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		int n;
		scanf("%d", &n);
		memset(s, 0, sizeof(s));
		for(int i = 1; i <= n; i++){
			scanf("%d", &s[i]);
		}
	//	printf("%d %d", maxn, minn);
		int ans1 = 0;
		for(int i = 2; i < n; i++){
			ans1 = ans1 + s[i + 1] - s[i] - 1;
		}
		int ans2 = 0;
		for(int i = n - 1; i > 1; i--){
			ans2 = ans2 + s[i] - s[i - 1] - 1;
		}
		int ans = max(ans1, ans2);
		printf("%d\n", ans);
	}
	return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值