hdoj 5037 Frog(贪心)

Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3641    Accepted Submission(s): 908


Problem Description
Once upon a time, there is a little frog called Matt. One day, he came to a river.

The river could be considered as an axis.Matt is standing on the left bank now (at position 0). He wants to cross the river, reach the right bank (at position M). But Matt could only jump for at most L units, for example from 0 to L.

As the God of Nature, you must save this poor frog.There are N rocks lying in the river initially. The size of the rock is negligible. So it can be indicated by a point in the axis. Matt can jump to or from a rock as well as the bank.

You don't want to make the things that easy. So you will put some new rocks into the river such that Matt could jump over the river in maximal steps.And you don't care the number of rocks you add since you are the God.

Note that Matt is so clever that he always choose the optimal way after you put down all the rocks.
 

Input
The first line contains only one integer T, which indicates the number of test cases.

For each test case, the first line contains N, M, L (0<=N<=2*10^5,1<=M<=10^9, 1<=L<=10^9).

And in the following N lines, each line contains one integer within (0, M) indicating the position of rock.
 

Output
For each test case, just output one line “Case #x: y", where x is the case number (starting from 1) and y is the maximal number of steps Matt should jump.
 

Sample Input
 
  
2 1 10 5 5 2 10 3 3 6
 

Sample Output
 
  
Case #1: 2 Case #2: 4
 

题意:

有一只青蛙,在一条河(数轴)上跳,他要从0的位置跳到m;

现在这条河上已经有n个石头了,青蛙每次最多可以跳l;

现在你可以往河里任意放石头,使青蛙跳的次数最多;

每一组样例给出n,m,l.然后接下去给出已有n个石头的位置;

问最多跳几次:


思路:

贪心,dis表示前一跳的距离,我们要算接下去那一跳,和之前那一跳的和,如果小于等于l,则说明这两跳可以合并成一跳;

否则的话跳数加1;

如果距离>l+1;则我们可以放石头,让它必须跳两次;

如现在位置0,下一个石头要跳到15,青蛙一次跳10,那么我们在1放一个,他就必须两次才能到11,然后再跳4到15.这样上一跳的距离就成了余数,余下来的4;



#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
const int NI = 200005;
int T, n, m, l;
int a[NI];
int main() {
	scanf("%d", &T);
	for(int cas = 1; cas <= T; cas++) {
		scanf("%d%d%d", &n, &m, &l);
		for(int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
		}
		a[0] = 0; a[n+1] = m;
		sort(a, a+n+2); 	//给定的石子不一定有序 
		int step = l, res = 0;
		for(int i = 1; i < n+2; i++) {
			int x = (a[i]-a[i-1]) % (l+1);
			int y = (a[i]-a[i-1]) / (l+1);
			if(step + x <= l) {
				res += 2 * y;
				step += x;
			} 
			else {
				res += 2 * y + 1;
				step = x;
			}
		}
		printf("Case #%d: %d\n", cas, res);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/zyf_2014/article/details/52810574