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. RESTful 杂文归集

    Json格式与对象转换 org.codehaus.jackson.map.ObjectMapper 工具转json格式,进行接口文档书写工具http://editor.swagger.io/#/ ja ...

  2. Eclipse中设置JDK内存方式

    (1) 打开Eclipse,双击Serveers进入到servers编辑画面 (2) 点击 Open launch configuration 选项 (3) 选项中找到Arguments 的选项卡(t ...

  3. 1024PHP数组

    <?php //定义数组//$attr = array();//$attr[0] = 1;//索引数组//$attr = array(1,2,3,4);//关联数组//$attr = array ...

  4. NetSetMan IP地址切换工具

    http://www.netsetman.com/en/freeware NetSetMan NetSetMan是一个网络设置管理器,它可以很容易在6种不同的,视觉结构化的配置之间切换,包括: IP地 ...

  5. OC小结

    #import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * a ...

  6. leetcode 109 Convert Sorted List to Binary Search Tree ----- java

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. 课堂所讲整理:HTML--6运算符、类型转换

    1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...

  8. HDU-5783 Divide the Sequence(贪心)

    题目大意:给一个整数序列,将其划分成若干个子连续序列,使其每个子序列的前缀和不为负.求最大的划分个数. 题目分析:从后往做累加计算,如果不为负,则计数加一,累加和清0.否则,一直往前扫描.如果最终的和 ...

  9. typedef 和 define的区别

    类型取别名,还可以定义常量.变量.编译开关 都知道两个在某些情况下是相同的 但是define是在预编译时就会处理掉,进行简单的宏替换,不管正不正确都替换掉,末尾没有分号,有分号连分号也一起替换了. 而 ...

  10. Python 基础语法(四)

    Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...