1042:Gone Fishing,考点:贪心策略
原题:http://bailian.openjudge.cn/practice/1042/
描述
John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,...,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=192). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >= 0 ), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di (di >= 0). If the number of fish expected to be caught in an interval is less than or equal to di , there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch.
Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.
中文简洁易懂型描述:
输入
You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.
输出
For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected.
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. Insert a blank line between cases.
样例输入
2 1 10 1 2 5 2 4 4 10 15 20 17 0 3 4 3 1 2 3 4 4 10 15 50 30 0 3 4 3 1 2 3 0
样例输出
45, 5 Number of fish expected: 31 240, 0, 0, 0 Number of fish expected: 480 115, 10, 50, 35 Number of fish expected: 724
解法
思路:贪心+枚举。枚举最终停下来的湖,将方案分为n类,每类方案的走路时间就是确定的,在每类方案中找最优解,保留所有方案中最优的结果。
坑:一开始的鱼初值设为-1,所以在纯钓鱼时间小于等于0(也就是没有钓鱼)的时候也要进行处理,不能直接break;
搜到的一些很有用的测试用例:http://poj.org/showmessage?message_id=162286
AC代码如下:
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #include <memory> 6 using namespace std; 7 const int MAXN = 26; 8 int F[MAXN][200]; 9 struct stay { 10 int lake; 11 int time; 12 int fish; 13 stay(int l, int t, int f) :lake(l), time(t), fish(f) {} 14 bool operator <(const stay A)const { 15 if (fish == A.fish) 16 return lake < A.lake; 17 return fish > A.fish; 18 } 19 }; 20 int main() { 21 int n;//n个湖 22 int numcase = 0; 23 while (cin >> n) { 24 if (n == 0)break; 25 int h;//总时间 26 cin >> h; 27 h = h * 12;//以5分钟为一个单位 28 int f[MAXN], d[MAXN], t[MAXN]; 29 for (int i = 0; i < n; i++) 30 cin >> f[i]; 31 for (int i = 0; i < n; i++) 32 cin >> d[i]; 33 for (int i = 0; i < n - 1; i++) 34 cin >> t[i]; 35 memset(F, 0, sizeof(F)); 36 for (int i = 0; i < n; i++) { 37 F[i][0] = f[i]; 38 for (int j = 1; j < h; j++) { 39 if (F[i][j - 1] - d[i] < 0) 40 break; 41 F[i][j] = F[i][j - 1] - d[i]; 42 } 43 } 44 int fish = -1; 45 int result[MAXN]; 46 memset(result, 0, sizeof(result)); 47 for (int lake = 0; lake < n; lake++) {//枚举停下来的湖为i 48 int walktime = 0; 49 for (int j = 0; j < lake; j++) 50 walktime += t[j]; 51 int k = h - walktime;//纯钓鱼时间 52 if (k <= 0) { 53 fish = max(fish, 0); 54 break; 55 } 56 vector<stay>find_best; 57 for (int i = 0; i <= lake; i++) 58 for (int j = 0; j < k; j++) 59 find_best.push_back(stay(i, j, F[i][j])); 60 sort(find_best.begin(), find_best.end()); 61 int sumfish = 0; 62 for (int i = 0; i < k; i++) 63 sumfish += find_best[i].fish; 64 if (sumfish > fish) { 65 fish = sumfish; 66 memset(result, 0, sizeof(result)); 67 for (int i = 0; i < k; i++) 68 result[find_best[i].lake] ++; 69 } 70 else if (sumfish == fish) { 71 int temp[MAXN]; 72 memset(temp, 0, sizeof(temp)); 73 for (int i = 0; i < k; i++) 74 temp[find_best[i].lake]++; 75 bool needChange = false; 76 for (int i = 0; i < n; i++) { 77 if (temp[i] > result[i]) { 78 needChange = true; 79 break; 80 } 81 else if (temp[i] < result[i]) 82 break; 83 } 84 if (needChange) 85 memcpy(result, temp, sizeof(temp)); 86 } 87 } 88 if (numcase != 0)cout << endl; 89 for (int i = 0; i < n; i++) 90 { 91 if (i == 0)cout << result[i] * 5; 92 else cout << ", " << result[i] * 5; 93 } 94 cout << endl; 95 cout << "Number of fish expected: " << fish << endl; 96 numcase++; 97 } 98 return 0; 99 }
原文地址:https://www.cnblogs.com/erable/p/15013494.html
- 基于流量的OpenSSL漏洞利用检测方法
- [原创]Fluent NHibernate之旅(四)-- 关系(中)
- 华为专家 | 轻量化微服务测试实践
- Android Material Design系列之Navigation Drawer
- [原创]Fluent NHibernate之旅(四)-- 关系(下)
- 一次通过漏洞挖掘成功渗透某网站的过程
- 使用fuzzDB进行web安全测试
- Android Material Design系列之FloatingActionButton和Snackbar
- Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发
- Android Material Design系列之Toolbar
- Struts2 S2-020在Tomcat 8下的命令执行分析
- Struts2再曝S2-020补丁绕过漏洞 – 万恶的正则表达式
- 学习BlogEngine.Net解读笔记系列(一)
- Android面试系列之应用内多语言切换
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Linux alias命令编写
- 详解linux系统调用原理
- Linux下安装pyenv的方法
- 详解Linux如何生成随机数字和字符串
- linux(ubuntu)用户连续N次输入错误密码进行登陆时自动锁定X分钟
- linux下安装golang的方法
- Linux系统的文件传输方法
- CentOS 6.8 NFS 文件共享设置的方法
- linux如何mount挂载磁盘并设置开机自动mount的实现
- 浅谈Linux的编码及编码转换方法
- 在 Linux 上用 DNS 实现简单的负载均衡的方法
- centos7.2.1511安装jdk1.8.0_151及mysql5.6.38的方法
- Linux CentOS使用crontab设置定时重启的方法
- centos安装php5、卸载php、安装php7的教程
- centos7中crontab定时计划任务5分钟一次命令写法