hdu5037 Frog (贪心)

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

网络赛 北京 比较难的题

Frog

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

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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5041 5040 5039 5038 5036

题意:

有个青蛙在数轴上跳,要从0跳到M,每次能跳0~L,数轴是个水面,只能跳到石头上。给出若干已有石头,可以在任意位置添加石头。要让青蛙能跳到终点,并且跳的步数最大。

题解:

贪心狂跳。

先给石子排个序,添加一个石子m代表终点。

设当前青蛙在now处,上一步在pre处(初始化负无穷)。

先在已有石子上狂跳,每次跳到能跳的最远的那个。

当不能继续跳的时候(下一个石子距离过远),就加石子。

加石子要加在max(now+1,pre+l+1)处,也就是pre刚好不能跳到的,now能跳到的最近的那一个。

然后重复,如果能跳到已有石头就跳,跳不到就加石头。

简直贪,这样就能得到正确答案!

不过这样有可能要跳太多次,会TLE,所以当两个石头距离过远的时候,可以特殊处理一下新建好多石头跳好多步。

可以发现连续新建两个石头跳两步的话,肯定是到now+l+1,于是可以直接跳若干个(l+1)步。

(简直比青蛙棋还难,我怕了)

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define mf1(array) memset(array, -1, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("huzhi.txt","w",stdout)
#define mp make_pair
#define pb push_back
const double pi=acos(-1.0);
const double eps=1e-; const int maxn=; int n,m,l;
int a[maxn]; int farm() {
int now=,pre=-maxn;
int i=;
int ans=;
a[n+]=m;
sort(a+,a+n+);
a[n+]=m+l+;
while(now<m) {
while(a[i]<=now+l)i++;
//printf("i=%d\n, a[i]=%d, a[i-1]=%d\n",i,a[i],a[i-1]);
if(a[i-]>now) {
pre=now;
now=a[i-];
ans++;
} else {
int w=(a[i]-now)/(l+)-;
if(w>) {
int t=max(now+,pre+l+);
pre=t+(w-)*(l+);
now+=w*(l+);
ans+=w*;
} else {
int t=now;
now=max(now+,pre+l+);
pre=t;
ans++;
}
}
//printf("%d %d %d\n",now,pre,ans);
}
return ans;
} int main() {
int T,cas=;
int i;
RD(T);
while(T--) {
RD3(n,m,l);
FOR(i,,n)scanf("%d",&a[i]);
printf("Case #%d: %d\n",cas++,farm());
}
return ;
}
上一篇:【Java】如何调用系统命令


下一篇:selenium+python定位元素方法