Frog
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1376 Accepted Submission(s): 373
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.
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.
2 1 10 5 5 2 10 3 3 6
Case #1: 2 Case #2: 4
题目大意:
有一只青蛙在一条直线上跳,他要从0跳到m。而这条直线上有n块石头,他每一次最多可以跳l个单位长度。
青蛙在跳石子时执行最优策略。
你所需要做的是,给这条直线加石子,使得青蛙可以跳到m,且使得青蛙的最优策略最渣渣渣渣渣渣渣渣渣渣渣渣
反省&思路:
这道题的确是有点难,最重要的就是关于最优策略的贪心选择,需要考虑的情况比较多,而且很复杂比较难理清楚。
在打比赛的时候没有做出来,后来也是花了几天才做出来。
(1)首先,如果前面有当前石子可以跳到的石子,应该尽可能选最远的可达石子跳,也就是在now+l的范围内的最远。这就是青蛙执行最优策略的最重要体现。
(这一点在比赛的时候还是不难想到的。
(2)其次,如果当前石子前方已经没有可达石子了,就要考虑放置石子。
关于这个问题最先要知道的是,我们一定不能在小于pre+l的范围内放,如果这样,
青蛙就会按照(1)的方案直接越过now这颗石子,本可以多拖延的一步就被剪掉了。
所以石子的放置一定是在[pre+l+1,now+l]这个闭区间范围内的。
(3)理论上按照上面两步贪心这道题已经可以做出来了,但是这道题恶心就恶心在还卡时间,不能仅仅是像上面这样一步一步跳
在2的情况中要对多步进行处理。后来就wa就主要wa在这个地方,思路没有理清楚,多步的时候直接跳到原本就有的可达的石子上了,
但由1的最优策略青蛙不一定会跳这个石子,而有可能是后面的石子。
另外,经过动笔算,我们可以发现,在处理前方无石子多步的时候,前进的距离一定是等于(l+1)*k的
综上两点,我们在处理前方无石子多步时,应该在不越过前方第一个原先就有的石子的情况下,使得k尽可能的大。(注意千万不能直接
跳上那颗石子,因为根据青蛙的最优策略,他会有可能选择跳过那颗石子而跳到其之后的石子上。
下面是ac代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 200008
using namespace std;
struct node{
int a,steps;
node(){
a=0,steps=0;
}
};
int a[maxn];
int tt;
int n,m,l;
int main()
{
int cas=1;
scanf("%d",&tt);
while(tt--){
memset(a,127,sizeof(a));
scanf("%d %d %d",&n,&m,&l);
if(!m){
printf("Case #%d: ",cas++);
printf("%d\n",0);
continue;
}
a[0]=0;
a[n+1]=m;
a[n+2]=m+l+1;
for(int i=1;i<=n;i+=1){
scanf("%d",&a[i]);
}
sort(a,a+n+2);
/*cout<<"__"<<endl;
for(int i=0;i<=n+1;i+=1){
cout<<a[i]<<" ";
}cout<<endl;*/
node now,next,pre;
int t=0;
now.a=a[t];
pre.a=-999999999;
pre.steps=0;
while(t<=n+1){
while(a[t]<=now.a+l&&t<=n+1){t++;}
//cout<<"==="<<a[t]<<" "<<t<<endl;
if(a[t-1]>now.a){
//cout<<"=="<<t<<endl;
next.a=a[t-1];
next.steps=now.steps+1;
pre=now;
now=next;
//cout<<"eatshit\n";
}
else if((a[t]-now.a)/(l+1)==1){
int tt=now.a;
int tmp=max(pre.a+l+1,now.a+1);
now.a=tmp;
now.steps+=1;
pre.a=tt;
pre.steps=now.steps-1;
//t-=1;
}
else{
int tt=now.a;
now.a+=(l+1)*((a[t]-tt)/(l+1)-1);
now.steps+=2*((a[t]-tt)/(l+1)-1);
pre.a+=(l+1)*((a[t]-tt)/(l+1)-1);
pre.steps+=2*((a[t]-tt)/(l+1)-1)-1;
}
//cout<<"___"<<pre.a<<" "<<now.a<<" "<<now.steps<</*<<a[t]<<" "<<*/t<<endl;
}
printf("Case #%d: ",cas++);
printf("%d\n",now.steps);
}
return 0;
}