题意:在一个n*m的网格上,从(0,0)走到(n-1,m-1),每次只能向右或者向下走一格。一个人最初有一个生命值x,走到每一个格生命值会变为x + s[i][j],(s[i][j]可为负,0,正),若生命值小于等于0,则人死亡。告诉网格上所有s[i][j],求x的最小值使得该人能够或者走到(n-1,m-1)。|s[i][j]| < 1000,n,m < 500。

解法:这道题不能直接dp,否则会错。必须要先二分x的值,然后再dp。dp[i][j]记录的是走到(i,j)格所能有的最大生命值,但是要注意,d[i][j]只能在d[i][j-1]或d[i-1][j]中有一个为正时才能转移过来。

   if (d[i-1][j] > 0) d[i][j] = max(d[i][j], d[i-1][j] + s[i][j]), if (d[i][j-1] > 0) d[i][j] = max(d[i][j], d[i][j-1] + s[i][j])。初始化时将所有d[i][j]赋值为-1。

tag:二分,dp

 /*
* Author: Plumrain
* Created Time: 2013-12-03 20:25
* File Name: DP-LA-5983.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x)) int n, m;
int d[][];
int s[][]; void init()
{
scanf ("%d%d", &n, &m);
for (int i = ; i < n; ++ i)
for (int j = ; j < m; ++ j)
scanf ("%d", &s[i][j]);
} bool dp(int x)
{
CLR1 (d);
d[][] = x + s[][];
for (int i = ; i < n; ++ i)
if (d[i-][] > )
d[i][] = d[i-][] + s[i][];
for (int i = ; i < m; ++ i)
if (d[][i-] > )
d[][i] = d[][i-] + s[][i]; for (int i = ; i < n; ++ i)
for (int j = ; j < m; ++ j){
if (d[i][j-] > )
d[i][j] = max(d[i][j], d[i][j-] + s[i][j]);
if (d[i-][j] > )
d[i][j] = max(d[i][j], d[i-][j] + s[i][j]);
}
if (d[n-][m-] <= ) return ;
return ;
} int bin_search()
{
int l = , r = 1e7;
while (l <= r){
int mid = (l + r) >> ;
if (!dp(mid)) l = mid + ;
else r = mid - ;
}
return l;
} int main()
{
int T;
scanf ("%d", &T);
while (T--){
init();
int ans = bin_search();
printf ("%d\n", ans);
}
return ;
}

UVALive 5983 MAGRID的更多相关文章

  1. UVALive 5983 MAGRID DP

    题意:在一个n*m的网格上,从(0,0)走到(n-1,m-1),每次只能向右或者向下走一格.一个人最初有一个生命值x,走到每一个格生命值会 变为x + s[i][j],(s[i][j]可为负,0,正) ...

  2. UVALive 5983 二分答案+dp

    想了很久都想不出怎么dp,然后发现有些例子,如果你开始不确定起始值的话,是不能dp的,每种状态都有可能,所以只能二分一个答案,确定开始的val值,来dp了. #include <cstdio&g ...

  3. 2015暑假训练(UVALive 5983 - 5992)线段树离线处理+dp

    A: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83690#problem/A 题意:N*M的格子,从左上走到右下,要求在每个点的权值 ...

  4. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  5. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  6. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  7. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  8. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  9. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. ORACLE添加作业

    --创建job declare job number; beginsys.dbms_job.submit(job,'prc_into_actiwager;',sysdate,'sysdate+30/( ...

  2. mac下开发环境常用操作与命令

    [1] 修改hosts文件 vim /private/etc/hosts

  3. java simple check whether a file or directory.

    Ref:  check whether a file or directory First, make sure the path exists by using: new File(path).ex ...

  4. 国际化 native2ascii用法

    cmd下输入: native2ascii -encoding GBK(需要编译成哪种语言) (中文文件路劲) (英文文件路劲) 其他固定 例如 native2ascii -encoding GBK C ...

  5. node 裁剪图片

    1.前端一般用Jcrop这个jq插件 要返回 x: 图片 x坐标 y: 图片 y坐标 w: 图片 宽度 h: 图片 高度 2.node 实现 var images = require("im ...

  6. WampServer修改端口及菜单Localhost

    一.修改Apache端口 1.在界面中选Apache,弹出隐藏菜单选项,打开配置文件httpd.conf: 2.找到 Listen 80: 3.将 80 改成 8080(当然自己也可以设定别的不使用的 ...

  7. 『重构--改善既有代码的设计』读书笔记----Introduce Local Extension

    同Introduce Foreign Method一样,很多时候你不能修改编辑原始类,你需要为这些服务类增加一些额外的函数,但你没有这个权限或者入口.如果你只需要一个或者两个外加函数那么你可以放心的使 ...

  8. C#快速导入海量XML数据至SQL Server数据库

    #region 将Xml中的数据读到Dataset中,然后用SqlBulkCopy类把数据copy到目的表中using (XmlTextReader xmlReader = new XmlTextRe ...

  9. html5本地数据库(一)

    本地数据库 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important ...

  10. javascript删除目标div tr 等

    var delTr = document.getElementById("要删除的位置"); // 获取要删除的位置”对象“ delTr.parentNode.removeChil ...