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

被折叠的 条评论
为什么被折叠?



