博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu5037 Frog (贪心)
阅读量:7064 次
发布时间:2019-06-28

本文共 3750 字,大约阅读时间需要 12 分钟。

网络赛 北京 比较难的题

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:          

题意:

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

题解:

贪心狂跳。

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

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

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

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

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

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

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

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

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

 

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

代码:

1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 #define ll long long14 #define usll unsigned ll15 #define mz(array) memset(array, 0, sizeof(array))16 #define mf1(array) memset(array, -1, sizeof(array))17 #define minf(array) memset(array, 0x3f, sizeof(array))18 #define REP(i,n) for(i=0;i<(n);i++)19 #define FOR(i,x,n) for(i=(x);i<=(n);i++)20 #define RD(x) scanf("%d",&x)21 #define RD2(x,y) scanf("%d%d",&x,&y)22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)23 #define WN(x) printf("%d\n",x);24 #define RE freopen("D.in","r",stdin)25 #define WE freopen("huzhi.txt","w",stdout)26 #define mp make_pair27 #define pb push_back28 const double pi=acos(-1.0);29 const double eps=1e-10;30 31 const int maxn=222222;32 33 int n,m,l;34 int a[maxn];35 36 int farm() {37 int now=0,pre=-maxn;38 int i=1;39 int ans=0;40 a[n+1]=m;41 sort(a+1,a+n+2);42 a[n+2]=m+l+1;43 while(now
now) {47 pre=now;48 now=a[i-1];49 ans++;50 } else {51 int w=(a[i]-now)/(l+1)-1;52 if(w>0) {53 int t=max(now+1,pre+l+1);54 pre=t+(w-1)*(l+1);55 now+=w*(l+1);56 ans+=w*2;57 } else {58 int t=now;59 now=max(now+1,pre+l+1);60 pre=t;61 ans++;62 }63 }64 //printf("%d %d %d\n",now,pre,ans);65 }66 return ans;67 }68 69 int main() {70 int T,cas=1;71 int i;72 RD(T);73 while(T--) {74 RD3(n,m,l);75 FOR(i,1,n)scanf("%d",&a[i]);76 printf("Case #%d: %d\n",cas++,farm());77 }78 return 0;79 }
View Code

 

转载于:https://www.cnblogs.com/yuiffy/p/3984846.html

你可能感兴趣的文章
微软私有云分享(R2)27维护窗口的使用
查看>>
Mac 平台下功能强大的Shimo软件使用指南
查看>>
永远不要对一个外行聊你的专业
查看>>
MySQL学习四部曲
查看>>
SCCM 2012 R2实战系列之一:SQL安装
查看>>
windows下安装memcached
查看>>
08R2-12R2基于访问权限的文件枚举
查看>>
Gartner:网络信息安全投入依然不在中国政企客户优先投入之列
查看>>
恢复误删除的ESXi服务器存储VMFS卷
查看>>
SFB 项目经验-22-如何查看存储的管理IP地址
查看>>
libevent入门教程:Echo Server based on libevent
查看>>
.NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)...
查看>>
一次服务器CPU占用率高的定位分析
查看>>
安装office2007 1706错误
查看>>
crontab中执行多条命令
查看>>
25 JavaScript的幻灯片用于在Web布局的精彩案例
查看>>
用C语言写的迅雷看看XV文件提取器及C语言源代码
查看>>
ccpuid:CPUID信息模块 V1.01版,支持GCC(兼容32位或64位的Windows/Linux)
查看>>
用dom4j操作XML文档(收集)
查看>>
WinForm实例源代码下载
查看>>