题目:uva757 - Gone Fishing(贪心)

题目大意:有N个湖泊仅仅有一条通路将这些湖泊相连。

每一个湖泊都会给最開始5分钟间隔内能够调到的鱼(f)。然后给每过5分钟降低的鱼的数量(d),假设当前的鱼少于等于降低的数量,说明在下个5分钟没有鱼。还有过每条道路的所要耗费的时间(N-1),时间都是以5分钟为单位的。渔者能够在随意一个湖泊钓鱼,可是起始位置是在湖泊1。问H小时后,渔者如何合理的在每一个湖泊分配时间,能够得到的鱼最多。

假设得到最多的鱼的方式有多种,则取在前面的湖泊耗时最久的那一种。

解题思路:由于这些湖泊是由一条通路串起来的。这样就意味着经过湖泊3,那么的先经过湖泊2。

所以这里先枚举钓鱼的终点i。这样在路上耗费的时间就能够确定了。然后在0 - i这些湖泊中以f最大的排序。每次取都是f取最多的,直到和这个湖泊的鱼降低到和第二个大的f相等或者更小。这里有个注意点。两个湖泊假设f同样的话。应该先选比較前面的湖泊。

然后再进行排序。反复这些操作直到时间耗尽,或是没有湖泊有鱼。

这样就可能存在时间没实用完的情况。这个时候随意的湖泊都是没有鱼的,在哪个湖泊呆着都是一样的,可是由于要求的前面的湖泊耗时多,所以剩下的时间就都给湖泊1。

最后统计鱼的时候,大于当前的最大值自然要保存这样的分配方式,可是等于的话,就须要选择前面湖泊耗时较大的那种。

计算鱼的数量时候要细心。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 30;
int h, n;
int t[N];
typedef long long ll;
ll fish; struct Lake { int i;
ll f;
int d;
int hour;
}l[N], l1[N]; int cmp (const Lake &a, const Lake &b) {
if (a.f == b.f)
return a.i < b.i;
return a.f > b.f;
} int cmp1 (const Lake &a, const Lake &b) { return a.i < b.i; } void init () { h *= 12;
fish = -1;
for (int i = 0; i < n; i++)
l[i].hour = 0;
} bool judge (int num, int newh) { //同样鱼的数目的时候比較选择哪种方案 sort (l1, l1 + num + 1, cmp1);
l1[0].hour += newh;
for (int i = 0; i <= num; i++)
if (l1[i].hour != l[i].hour)
return l1[i].hour > l[i].hour;
return false;
} void solve () { init ();
int newh;
int k, max_day;
ll sum; for (int i = 0; i < n; i++) { //枚举终点 newh = h;
for (int j = 0; j <= i; j++) { l1[j].i = l[j].i;
l1[j].f = l[j].f;
l1[j].d = l[j].d;
l1[j].hour = 0;
} for (int j = 0; j <= i - 1; j++) //路上要耗费的时间
newh -= t[j];
if (newh <= 0)
continue; //到达不了直接下一种方案 if (i == 0) { l1[i].hour += newh;
newh = 0;
} while (newh > 0 && i) { //找f最大的湖泊 sort (l1, l1 + i + 1 , cmp);
if (l1[0].f == 0)
break;
if (l1[0].d != 0) { if (l1[0].f == l1[0].f)
k = 1;
else { k = (l1[0].f - l1[1].f) / l1[0].d;
if ((l1[0].f - l1[1].f) % l1[0].d)
k++;
}
}
else
k = newh; //假设d等于0说明这里的鱼不会降低。自然剩下的时间都耗费在这里最合理。 if (newh - k < 0)
k = newh;
newh -= k;
l1[0].f -= k * l1[0].d; //更新f和这个湖泊的耗时
if (l1[0].f < 0)
l1[0].f = 0;
l1[0].hour += k;
} sum = 0;
for (int j = 0; j <= i; j++) { //计算鱼的数目 k = l1[j].i;
if (!l1[j].hour)
continue;
if (l[k].d == 0) { sum += l[k].f * l1[j].hour;
continue;
}
max_day = l[k].f/ l[k].d;
if (l[k].f % l[k].d != 0)
max_day++;
if (l1[j].hour <= max_day)
max_day = l1[j].hour;
l1[j].f = l[k].f - (max_day - 1) * l[k].d;
sum += (l[k].f + l1[j].f) * max_day / 2; } if (sum > fish || (sum == fish && judge(i, newh))) { //维护最大值 fish = sum;
for (int j = 0; j <= i; j++) { if (l1[j].i == 0)
l[0].hour = l1[j].hour + newh;
else
l[l1[j].i].hour = l1[j].hour;
}
}
}
} int main () { bool flag = 0;
while (scanf ("%d", &n) && n) { if (flag)
printf ("\n");
flag = 1;
scanf ("%d", &h);
for (int i = 0; i < n; i++)
scanf ("%lld", &l[i].f);
for (int i = 0; i < n; i++)
scanf ("%d", &l[i].d);
for (int i = 0; i < n - 1; i++)
scanf ("%d", &t[i]);
for (int i = 0; i < n; i++)
l[i].i = i; solve();
printf ("%d", l[0].hour * 5);
for (int i = 1; i < n; i++)
printf (", %d",l[i].hour * 5);
printf ("\n");
printf ("Number of fish expected: %lld\n", fish);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

uva757 - Gone Fishing(馋)的更多相关文章

  1. ZOJ 1015 Fishing Net(弦图判定)

    In a highly modernized fishing village, inhabitants there make a living on fishery. Their major tool ...

  2. bzoj 1242: Zju1015 Fishing Net 弦图判定

    1242: Zju1015 Fishing Net弦图判定 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 214  Solved: 81[Submit ...

  3. Poj/OpenJudge 1042 Gone Fishing

    1.链接地址: http://bailian.openjudge.cn/practice/1042/ http://poj.org/problem?id=1042 2.题目: Gone Fishing ...

  4. POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)

    Gone Fishing Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 30281   Accepted: 9124 Des ...

  5. ●BZOJ 1006 [HNOI2008]神奇的国度(弦图最小染色数)○ZOJ 1015 Fishing Net

    ●赘述题目 给出一张弦图,求其最小染色数. ●题解 网上的唯一“文献”:<弦图与区间图>(cdq),可以学习学习.(有的看不懂) 摘录几个解决改题所需的知识点: ●子图和诱导子图(一定要弄 ...

  6. Cocos2d-X开发教程-捕鱼达人 Cocos2-x development tutorial - fishing talent

    Cocos2d-X开发教程-捕鱼达人 Cocos2-x development tutorial - fishing talent 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱 ...

  7. CSU 1859 Gone Fishing(贪心)

    Gone Fishing [题目链接]Gone Fishing [题目类型]贪心 &题解: 这题要先想到枚举走过的湖,之后才可以贪心,我就没想到这,就不知道怎么贪心 = = 之后在枚举每个湖的 ...

  8. Gone Fishing(贪心)

    Gone Fishing John is going on a fising trip. He has h hours available (1 ≤ h ≤ 16), and there are n ...

  9. LightOJ1106 Gone Fishing

    Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...

随机推荐

  1. Could Not Connect

    今天在写Quartz定时任务web应用时,访问项目页面出现Could Not Connect,后台也没报错. 然后我访问http://127.0.0.1:8080(Apache主页)的时候是正常的. ...

  2. 乐在其中设计模式(C#) - 单例模式(Singleton Pattern)

    原文:乐在其中设计模式(C#) - 单例模式(Singleton Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 单例模式(Singleton Pattern) 作者:weba ...

  3. (2) 用DPM(Deformable Part Model,voc-release4.01)算法在INRIA数据集上训练自己的人体检測模型

    步骤一,首先要使voc-release4.01目标检測部分的代码在windows系统下跑起来: 參考在window下执行DPM(deformable part models) -(检測demo部分) ...

  4. [TroubleShooting] The server network address can not be reached or does not exist

    Backtround: I'm trying to set up mirroring between two sql 2008 R2 databases on different servers in ...

  5. tcpdump参数及使用介绍(转)

    原文地址:http://dogdogcom.blog.51cto.com/2402458/490398 tcpdump -a 将网络地址和广播地址转变成名字: -d 将匹配信息包的代码以人们可以理解的 ...

  6. linux 下修改 apache 启动的所属用户和组

    apache默认启动的用户和组是www-data,所以有些时候,就会涉及到权限问题,没有权限在执行目录下创建或者读写文件.改变用户和组的方法其实很简单: 1.进入到apache默认安装路径/etc/a ...

  7. openstack临时存储后端

    声明: 本博客欢迎转发.但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 眼下openstack提供了 ...

  8. HDU 4946 Area of Mushroom 凸包

    链接:pid=4946">http://acm.hdu.edu.cn/showproblem.php?pid=4946 题意:有n个人.在位置(xi,yi),速度是vi,假设对于某个点 ...

  9. android 更新实现自己主动

    其主要原理是: 在应用程序启动.取server在版本 , 以下这个是获取当前应用的版本号信息 private void getCurVersion() { try { PackageInfo pInf ...

  10. 【翻译】Ext JS最新技巧——2014-10-30

    原文:Top Support Tips Greg Barry:Ext JS 5的ExtraParams Ext JS 4同意用户直接将extraParams加入到一个链接,相似例如以下代码: Ext. ...