H - H
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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 $a_1$ < $a_2$ < $a_3$ < ... < $a_N$ which are the initial positions of the rabbits. For each rabbit, its initial position
$a_i$ satisfies 1 ≤ $a_i$ ≤ 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
我的思维真是有待强化呀!!这个题目真的读不懂,队友后来给我解释了我才明白过来
给出的这串数字就是代表给的兔子所在数轴上的位置(由小到大),然后注意是最前或者最末尾的兔子往中间跳(不是什么奇奇怪怪的另外给的兔子...我刚开始就是这样想的,完全想错)!
(1)1st sample
3 4 6
最前面一只兔子往4兔 和 6兔中间跳(因为4、6兔他们中间有个空位5),所以是跳一次
(2)2nd sample 同1st
(3)3rd sample
3 5 9
3兔跳到5、9兔之间的位置6,队列变成5 6(3兔现在的位置) 9,
然后5兔跳到6、9之间的位置7,队列变成6 7 9 ,
同理,然后7 8 9,所以可以跳3次~~
所以判断下从哪头开始跳,能跳最多次就好了
【通过代码】
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define Pi acos(-1)
#define eps 1e-8
using namespace std;
typedef long long int ll;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
int sum=0,a[505];
scanf("%d",&n);
go(i,0,n-1) scanf("%d",&a[i]);
if(a[1]-a[0]<a[n-1]-a[n-2])
go(i,2,n-1) sum+=a[i]-a[i-1]-1;
else
go(i,1,n-2) sum+=a[i]-a[i-1]-1;
printf("%d\n",sum);
}
}