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. hadoop shell 详解

    概述  所有的hadoop命令均由bin/hadoop脚本引发.不指定参数运行hadoop脚本会打印所有命令的描述.  用法: hadoop [--config confdir] [COMMAND] ...

  2. python 的一些小技巧

    赋值: a, b, c = 'xixi', 'haha', 'hehe' 连接字典: >>> s = {1:'a', 2:'b', 3:'c'} >>> s.key ...

  3. MyEclipse+Struts+Hibernate+Mysql开发环境配置

    软件: jdk-6u22-windows-x64.exe apache-tomcat-6.0.29.exe mysql-5.1.51-winx64.exe myeclipse-8.6.0-win32. ...

  4. 在centos使用rpm包的方式安装mysql,以及更改root密码

    在centos使用rpm包的方式安装mysql,对于centos官方实际推荐使用yum进行安装,下载安装的方式主要用于内网服务器不能连接外网yum源的情况. 下载包 首先根据centos版本在mysq ...

  5. 70. Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  6. C#使用指针的2个主要原因

    一下内容来自于书籍:<C#高级编程(第六版)> C#使用指针的2个主要原因:

  7. hdu4642 Fliping game ——博弈

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4642 refer to: http://www.cnblogs.com/jackge/archive/ ...

  8. 【转】beancopy的替代方案

    链接:http://jingyan.baidu.com/article/215817f7d55b871edb14235b.html 最近在项目中接触到了BeanUtils.copyProperties ...

  9. spring源码学习【准备】之jdk动态代理和cglib动态代理的区别和性能

    一:区别:---->JDK的动态代理依靠接口实现,如果有些类并没有实现接口,则不能使用JDK代理,这就要使用cglib动态代理了.--->JDK的动态代理机制只能代理实现了接口的类,而不能 ...

  10. spring源码学习之【准备】jdk动态代理例子

    一:被代理的对象所要实现的接口 package com.yeepay.testpoxy; import java.util.Map; /** * 被动态代理的接口 * @author shangxia ...