Hdu 6227 Rabbits

来源:互联网 发布:Python Max iteritem 编辑:程序博客网 时间:2024/06/07 03:21

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
533 4 632 3 533 5 941 2 3 441 2 4 5
 

Sample Output
11301

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;} 



原创粉丝点击