码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5037 FROG 贪心 2014北京网络赛F

时间:2014-09-22 00:13:34      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   使用   ar   for   2014   sp   on   

题目链接;点击打开链接

题意:有一条小河长为M的小河,可以看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙,随意添加石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多需要多少次。

思路:不妨假设青蛙每个石头都要经过一次,用step表示青蛙上一次跳的步长,每跳一次对目前点到下一点的距离和step的和与L做比较,如果小与,证明青蛙可以一次跳到这,更新step和青蛙位置,cnt保持不变,若大于,证明青蛙至少需要再跳一次,若lenth<=l,则直接跳,更新step=lenth,cnt++;若lenth>l,则让青蛙以l+1-step,step,的周期跳,易证可以保证解最优,然后相当于把石头向后平移x*(l+1)个单位。and so on。详情请看代码,非常明了~比赛的时候手残wa到哭!!!

cpp:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int data[200010],T,n,m,l;
void slove()
{
	sort(data,data+n+1);
	int step=l,now=0,cnt=0;
	for(int i=0;i<=n;i++){
		int lenth=data[i]-now;
		if(lenth==0) continue;
		if(lenth+step<=l){
			now=data[i];
			step=lenth+step;
		}
		else if(lenth<=l){
			now=data[i];
			step=lenth;
			cnt++;
		}
		else if(lenth>l){
			int tp=lenth%(l+1);
			cnt+=(lenth/(l+1))*2;
			if(tp+step<=l){
				step=tp+step;
				now=data[i];
			}
			else {
				step=tp;
				now=data[i];
				cnt++;
			}
		}
	}
	printf("%d\n",cnt);
}
int main()
{
	//freopen("data.in","r",stdin);
	int cas=0;
	scanf("%d",&T);
	while (T--){
		printf("Case #%d: ",++cas);
		scanf("%d%d%d",&n,&m,&l);
		for(int i=0;i<n;i++){
			scanf("%d",data+i);
		}
		data[n]=m;
		slove();
	}
	return 0;
}


HDU 5037 FROG 贪心 2014北京网络赛F

标签:http   io   os   使用   ar   for   2014   sp   on   

原文地址:http://blog.csdn.net/alpc_paul/article/details/39457193

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!