I used DP instead of Greedy. But got WA on PoJ, though it passed all web-searched cases. Maybe I have to use Greedy.

BTW: a careless modification introduced an error, and it took me 1+ hours to debug. DETAILS

Sth. about this DP solution: dp[iLake][nCurrTotalTime + currentLakeTime] = dp[iLake - 1][nCurrTotalTime] + getFish(iLake, nCurrTotalTime, currentLakeTime);

Anyway, this is the first 2D DP problem I worked out :)

 #include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <vector>
using namespace std; #define Max(a, b) (a) > (b) ? (a) : (b)
#define MAX_LAKE 25
#define MAX_TIME (16*12) // We are at lake i now, with total time of tTtlLake spent on previous lakes,
// and we'd like to spend myt time on lake i -> how many fishes we can get from lake i?
int getFish(int i, int tTtlLake, int myt, int *accti, int *fi, int *di)
{
if (myt == ) return ; // If any left, get fishes by time myt
int ret = , left = fi[i];;
while (left > && myt > )
{
ret += left;
left -= di[i];
myt--;
}
return ret;
} void backtrack(int path[MAX_LAKE + ][MAX_TIME], int si, int sk, int n, int h)
{
bool bHasSolution = false;
int opath[MAX_LAKE + ] = { };
int actTtl = ;
while (si > && path[si][sk] != -)
{
opath[si] = path[si][sk];
actTtl += opath[si];
sk -= path[si][sk];
si--;
bHasSolution = true;
}
// Actual results less than h?
if (actTtl < h)
{
bool allAfterZero = true;
for (int i = ; i <= n; i++)
{
if (opath[i] != )
{
allAfterZero = false;
}
}
if (allAfterZero)
{
opath[] += h - actTtl;
}
}
if (!bHasSolution)
{
opath[] = h;
}
for (int i = ; i <= n; i++)
{
printf("%d", opath[i] * );
if (i < n) printf(", ");
}
printf("\n");
} void calc(int n, int h, int *fi, int *di, int *ti, int *accti)
{
// Reset
int dp[MAX_LAKE + ][MAX_TIME];
int path[MAX_LAKE + ][MAX_TIME];
memset( dp, , sizeof(int) * MAX_TIME * (MAX_LAKE + ));
memset(path, -, sizeof(int)* MAX_TIME * (MAX_LAKE + )); // dp[currentLakeIndex, currentTtlTimeOnLake] = currTtlFishCnt. Note: no time on the way included
// dp[i + 1, tTtl + t] = dp[i, tTtl] + f(fi(i + 1), t) int maxFish = , maxI, maxT;
for (int i = ; i <= n; i++)
{
// time on the way so far
int tWay = accti[i-]; for (int t = h; t >=; t--) // descending is necessary: it required more miniutes to spend as earlier as possible
{
if (i == && t > ) continue; int leftTime = h - t - tWay;
leftTime = leftTime < ? : leftTime; for (int k = ; k <= leftTime; k++) // k is how much time left in total
{
int newV = dp[i - ][t] + getFish(i, t, k, ti, fi, di);
if (newV > dp[i][t + k])
{
dp[i][t + k] = newV;
path[i][t + k] = k;
//printf("-putting %d to [%d][%d]\n", k, i, t + k); if (newV > maxFish)
{
maxFish = newV;
maxI = i; maxT = t + k;
}
}
}
}
} backtrack(path, maxI, maxT, n, h);
printf("Number of fish expected: %d\n\n", maxFish);
} int main()
{
int n, h;
while (scanf("%d", &n), (n != ))
{
scanf("%d", &h); // all in 5min-interval
h *= ; int fi[MAX_LAKE + ] = { }; // initial fish cnt
int di[MAX_LAKE + ] = { }; // decrease rate
int ti[MAX_LAKE] = { }; // travel time
int accti[MAX_LAKE] = { }; // travel time // Get Input
for (int i = ; i <= n; i ++) scanf("%d", fi + i);
for (int i = ; i <= n; i ++) scanf("%d", di + i);
for (int i = ; i <= n - ; i++)
{
scanf("%d", ti + i);
accti[i] = ti[i] + accti[i - ];
} calc(n, h, fi, di, ti, accti);
} return ;
}

POJ #1042 Gone Fishing - WA by a DP solution. TODO的更多相关文章

  1. POJ 1042 Gone Fishing( DP )

    题意:小明打算做一个h((1 <= h <= 16))个小时钓鱼旅行.发现这里有n(2 <= n <= 25)个湖,而且所有的湖都在一条路的旁边.小明打算从第1个湖开始钓起,每 ...

  2. POJ 1042 Gone Fishing

    题意:一个人要在n个湖中钓鱼,湖之间的路径是单向的,只能走1->2->3->...->n这一条线路,告诉你每个湖中一开始能钓到鱼的初始值,和每钓5分钟就减少的数量,以及湖之间的 ...

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

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

  4. poj -- 1042 Gone Fishing(枚举+贪心)

    题意: John现有h个小时的空闲时间,他打算去钓鱼.钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(John每在一个湖钓完鱼后,他只能走到下一个湖继续钓),John必须从1号湖开始钓起,但是他 ...

  5. POJ 1042 Gone Fishing#贪心

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...

  6. POJ 3249 Test for Job (拓扑排序+DP)

    POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...

  7. leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)

    leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...

  8. Poj/OpenJudge 1042 Gone Fishing

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

  9. Gone Fishing POJ 1042

    #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> us ...

随机推荐

  1. CoreOS Linux available in China

    CoreOS Linux 竭诚服务中国用户 今天,我们宣布一个令人振奋的消息 - CoreOS Linux 开源版本正式向中国地区提供服务!国内的用户们现在可以使用安全.自动的 CoreOS Linu ...

  2. vcffilter 工具bug以及解决办法

    1,使用说明: usage: vcffilter [options] <vcf file> options: -f, --info-filter     specifies a filte ...

  3. 安装GD库解决ThinkPHP 验证码Call to undefined function Think\imagecreate()出错

    在php中imagecreate函数是一个图形处理函数,主要用于新建一个基于调色板的图像了,然后在这个基础上我们可以创建一些图形数字字符之类的,但这个函数需要GD库支持,如果没有开启GD库使用时会提示 ...

  4. scrollView滚动(通过代码)

    平时的开发中可能会要求scrollview滚动,一般的方法时通过scrollview.scrollto(0,1000);来实现,但是注意这个方法是在scrollview停止动画之后才能执行的,因为如果 ...

  5. 153. Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  6. 基于Spring MVC的Web应用开发(三) - Resources

    基于Spring MVC的Web应用开发(3) - Resources 上一篇介绍了在基于Spring MVC的Web项目中加入日志,本文介绍Spring MVC如何处理资源文件. 注意到本项目的we ...

  7. Apache配置日志功能

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent ...

  8. 不要告诉我你懂margin

    分类: Html/CSS | 转载请注明: 出自 海玉的博客 本文地址: http://www.hicss.net/do-not-tell-me-you-understand-margin/ 你真的了 ...

  9. Comparison of B-Tree and Hash Indexes

    Understanding the B-tree and hash data structures can help predict how different queries perform on ...

  10. sed的实际用法举例

    sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器.能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上 ...