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 ...
随机推荐
- 关于C#本质论和CLR via C#中译本,不吐不快
C#本质论和CLR via C#两本好书,周老师可能是俗务缠身,太忙了吧,翻译得只能让人呵呵了. 你要是忙,别接那么多活好不啦. 现在都在说供给侧改革,都在大力提倡工匠精神,我们做技术的,还是踏实点好 ...
- python模块之configparser
configparser用于处理特定格式的文件,其本质上是利用open来操作文件. # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section ...
- Leetcode2:Add Two Numbers@Python
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- GitHub的.gitignore文件设置
用Eclipse连接GitHub 在本地仓库(最上层文件夹)建立.gitignore文件后,所有子文件夹下对应文件或者文件夹在submit的时候就会被忽略. 我将Eclipse的workspace当作 ...
- <读书笔记>软件调试之道 :从大局看调试-发现代码存在问题
声明:本文档的内容主要来源于书籍<软件调试修炼之道>作者Paul Butcher,属于读书笔记.欢迎转载! ---------------------------------------- ...
- tomcat manager配置
在tomcat-user.xml里面配置 <tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi=" ...
- shell 脚本杀死后台由php脚本控制运行的所有php脚本和java程序
效果: 运行命令: ./killallphpjavarm.sh java 源码: #!/bin/sh#根据进程名杀死进程#FileName: killjavaphprm.sh pgrep php ki ...
- Spring基本框架
1.Spring基本框架的概念 Spring 框架是一个分层架构,由 7 个定义良好的模块组成.Spring模块构建在核心容器之上,核心容器定义创建.配置和管理bean的方式.组成Spring框架的每 ...
- dock停靠管理器
DockManager停靠管理器可以对它所拥有的 停靠面板 的行为和外观设置进行集中控制.DockPanel停靠面板是停靠应用程序的主要构成部件. 常规面板 DockPanel.ParentPanel ...
- C# 自定义特性
http://www.cnblogs.com/tekkaman/p/3983360.html#undefined https://msdn.microsoft.com/zh-cn/library/sw ...