HDU-6227 Rabbits(规律题+贪心水题)

                                            Rabbits

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

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

Source

2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

http://acm.hdu.edu.cn/showproblem.php?pid=6227

题意:

一开始没读懂,看了博客才明白题意,英语太菜。。。。

给你n个位置,选择最外边的两个之一往其他中间插入,保证最后每两个之间没有其他的,问能最多插几次。

思路:

 贪心思想,为了保证插入的次数最多,应保证中间的空多,才能插入的多。其实就是找一开始最外层的那两个,谁的中间空隙少,然后就移动谁,把他移动到与它相邻结点挨着的地方。然后就一步一步的移动,每次填一个空隙。

比如:1 3 6  9  这个样例

先让1跳到4位置,现在为 3 4 6 9

再让3跳到5位置,现在为 4 5 6 9

再让4跳到7位置,现在为 5 6 7 9

再让5跳到8位置,现在为 6 7 8 9.

so,最多4次。

代码:

#include
using namespace std;
int a[501], sum[501];
int main()
{
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int len = 0; 
		memset(sum, 0, sizeof(sum));
		for(int i = 0; i < n; i++)
			cin >> a[i];
		for(int i = 0; i < n -1 ; i++) {
			sum[i] = a[i+1] - a[i] - 1;
			len += sum[i];
		}
		int Min = min(sum[0], sum[n-2]);
		cout << len - Min << endl;
	}

	return 0;
}

 

你可能感兴趣的:(水题)