poj -- 1042 Gone Fishing(枚举+贪心)
题意:
John现有h个小时的空闲时间,他打算去钓鱼。钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(John每在一个湖钓完鱼后,他只能走到下一个湖继续钓),John必须从1号湖开始钓起,但是他可以在任何一个湖结束他此次钓鱼的行程。此题以5分钟作为单位时间,John在每个湖中每5分钟钓的鱼数随时间的增长而线性递减。每个湖中头5分钟可以钓到的鱼数用fi表示,每个湖中相邻5分钟钓鱼数的减少量用di表示,John从任意一个湖走到它下一个湖的时间用ti表示。求一种方案,使得John在有限的h小时中可以钓到尽可能多的鱼。
注意:
1)每在一个湖钓完鱼后,他只能走到下一个湖,不能回头。
2)所求的方案可能有多种,但是题目中要求的方案需要满足下面条件:如果存在多种方案,那么选取在第一个湖(钓鱼)停留时间最长的一种,或者在第二个湖·····第N个湖停留时间最长的一种方案。(If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. )
分析:
1)可以枚举john在哪些湖钓鱼的所有情况,然后,总时间去掉路上花费的时间。剩余的时间,每次选举一个能钓到最多的鱼的湖钓鱼。
2)剩余时间大于0,但是,所有湖能钓到的鱼的个数都为0,此时需要把剩余时间全部加在第一个湖的停留时间中。
3)当钓到的鱼的数量更新时,停留时间列表必须更新。
4)当钓到的鱼的数量与当前所掉的鱼的数量相等时,停留时间列表可能需要更新。
代码如下:
#include <iostream>
#define maxn 30 using namespace std; int num, hour,ans,anst;
int f[maxn],d[maxn],t[maxn],ft[maxn];
int res[maxn] , temp[maxn];
void solve()
{
t[0] = 0; //统计不同的停留方案所需要的停留时间
for(int i = 1 ; i < num; i ++) t[i] += t[i-1]; //以5分钟为计量单位的总时间
int h = hour*12;
int pos;
ans = -1 ; for(int k = 0 ; k < num ; k ++)
{ anst = 0; //ft数组存的是f数组的副本,因为在计算过程中,需要改变ft数组的值;
//temp数组存储的是当前情况下,在每个湖停留的时间的列表
for(int i = 0 ; i < num ; i ++) temp[i] = 0 ,ft[i] = f[i]; //去掉路上的时间,之后钓鱼的总时间
int th = h - t[k]; while(th > 0)
{
pos = 0;
for(int j = 0 ; j <= k ; j ++)
{//找到当前时间,能钓到最大量的鱼,所在的湖
if(ft[j] > ft[pos])
pos = j;
}
//等于号不能去掉。如果在pos位置所能钓到的鱼的数量等于0,
//那么这一个时间单位将没有必要放在pos所在的湖(可能更新到第一个湖上)
if(ft[pos] <= 0) break;
anst += ft[pos];//更新鱼数量
ft[pos] -= d[pos];//更新所能钓到的鱼的数量
temp[pos] ++;//更新pos位置停留时间信息 th --;
}
//剩余时间全部加在第一个湖上
if(th > 0) temp[0] += th; if(anst > ans)
{//更新结果值和停留时间列表
ans = anst;
for(int i = 0 ; i < num ; i ++) res[i] = temp[i];
}
else if(anst == ans)
{//判断并更新停留时间列表
bool flag = true; for(int i = 0 ; i < num ; i ++)
if(temp[i] > res[i])
{
flag = false;
break;
}else if(temp[i] < res[i]) break;
if(!flag)
for(int i = 0 ; i < num ; i ++) res[i] = temp[i];
}
}
}
void print()
{//输出结果
for(int i = 0 ; i < num - 1 ; i ++) cout<<res[i]*5<<", ";
cout<<res[num-1]*5<<endl; cout<<"Number of fish expected: "<<ans<<endl;
cout<<endl;
}
int main()
{
while(cin>>num,num)
{
cin>>hour;
for(int i = 0 ; i < num ; i ++) cin>>f[i] ;
for(int i = 0 ; i < num ; i ++) cin>>d[i] ;
for(int i = 1 ; i < num ; i ++) cin>>t[i] ; solve();
print();
}
return 0;
}
poj -- 1042 Gone Fishing(枚举+贪心)的更多相关文章
- POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)
Gone Fishing Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 30281 Accepted: 9124 Des ...
- POJ 1042 Gone Fishing#贪心
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...
- POJ 1042 Gone Fishing
题意:一个人要在n个湖中钓鱼,湖之间的路径是单向的,只能走1->2->3->...->n这一条线路,告诉你每个湖中一开始能钓到鱼的初始值,和每钓5分钟就减少的数量,以及湖之间的 ...
- POJ #1042 Gone Fishing - WA by a DP solution. TODO
I used DP instead of Greedy. But got WA on PoJ, though it passed all web-searched cases. Maybe I hav ...
- POJ 1042 Gone Fishing( DP )
题意:小明打算做一个h((1 <= h <= 16))个小时钓鱼旅行.发现这里有n(2 <= n <= 25)个湖,而且所有的湖都在一条路的旁边.小明打算从第1个湖开始钓起,每 ...
- D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 51nod1625(枚举&贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...
- 枚举+贪心 HDOJ 4932 Miaomiao's Geometry
题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...
- Poj/OpenJudge 1042 Gone Fishing
1.链接地址: http://bailian.openjudge.cn/practice/1042/ http://poj.org/problem?id=1042 2.题目: Gone Fishing ...
随机推荐
- 使用dom4j解析XML
jar包:dom4j //使用dom4j解析返回的xml SAXReader reader = new SAXReader(); Document doc = reader.read(new Byte ...
- tableView中cell的重用机制
如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象,然而OS设备的内存是有限的,这样就将耗尽iOS设备的内存.要解决这个问题,需要提到重用UITable ...
- java 工具类
首先把构造函数设置为私有.然后方法都改为静态.
- redis常用总结
1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...
- 萌萌的websocket原理解析
转载自:http://www.zhihu.com/question/20215561 一.WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持 ...
- [转载]jquery中attr和prop的区别
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...
- How-to: Enable User Authentication and Authorization in Apache HBase
With the default Apache HBase configuration, everyone is allowed to read from and write to all table ...
- sql搜索like通配符的用法详解
http://www.lmwlove.com/ac/ID878 有很多朋友写了几年的like搜索,可能对like后面通配符的用法都不了解,甚至于%的作用是什么都不清楚.在这篇文章中,我们就一起来学习一 ...
- 使用Axis2实现WebService的发布和调用
一.Axis2的下载和安装 1.可从http://ws.apache.org/axis2/ 下载Axis2的最新版本: 可以下载如下三个zip包: axis2-1.7.3-bin.zip(用 ...
- JAVA类图
类与类图 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 在系统中,每个类具有一定的职责,职责指的是类所担任的任务,即类要完成什么样的功能, ...