UVALive 5983 MAGRID
题意:在一个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的更多相关文章
- UVALive 5983 MAGRID DP
题意:在一个n*m的网格上,从(0,0)走到(n-1,m-1),每次只能向右或者向下走一格.一个人最初有一个生命值x,走到每一个格生命值会 变为x + s[i][j],(s[i][j]可为负,0,正) ...
- UVALive 5983 二分答案+dp
想了很久都想不出怎么dp,然后发现有些例子,如果你开始不确定起始值的话,是不能dp的,每种状态都有可能,所以只能二分一个答案,确定开始的val值,来dp了. #include <cstdio&g ...
- 2015暑假训练(UVALive 5983 - 5992)线段树离线处理+dp
A: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83690#problem/A 题意:N*M的格子,从左上走到右下,要求在每个点的权值 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- xcode本地运行H5游戏可以吗?
答案是不可以!!! 不可以!!! 不可以!!! 有时候很郁闷的接受一个需求,然后以为自己能力不行,或者是代码写错,还是哪里了解不够,然而并不是啊!!! MD的我想说有些事是行不通的的!! 好了,平静下 ...
- Java之webService知识
Java之webService知识 1 webservice基础知识 1.1 webService请求的本质 一次webService本质请求,如下所示: 1.2 wsdl文档解析 wsdl文档元素结 ...
- SGU 231.Prime Sum
题意: 求有多少对质数(a,b)满足a<=b 且a+b也为质数.(a+b<=10^6) Solution: 除了2之外的质数都是奇数,两个奇数的和是偶数,不可能是质数.所以题目就是求差为2 ...
- 解决linux .so的链接时符号依赖问题
问题描述 target: a.out SO:libmyfile.so 依赖描述: a.out: libmyfile.so libmyfile.so: libssl.so.1.0.0 libssl.s ...
- .NET Framework(二)
在上一篇的随笔中,我们在理论层面上大致说明了.NET Framework的工作机制,内容的确比较晦涩难懂,但是还是希望大家有时候可以看看.我个人觉得,编程不是一味的敲代码,当自己遇到瓶颈的时候,可以多 ...
- C# winform 右下角弹窗
[DllImport("user32")] private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, in ...
- 技术大牛面试 http://www.itmian4.com/forum.php?mod=viewthread&tid=3824
不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic...在跪拜之余我们不禁要想,和这些牛人比,作为绝大部分技 ...
- PHP Cookie学习
<?php /* Cookie在计算机中保存的格式 用户名@网站地址[数字].txt Cookie在文件夹下,每个Cookie文件都是一个简单而又普通的文件件而不是程序,Cookie中的内容大多 ...
- 关于SQL server的一些知识点
关于怎么打开xp_cmdshell的方法: exec sp_configure 'show advanced option',1reconfiguregoexec sp_configure 'xp_c ...
- Asp.Net Mvc - 在OnResultExecut* 拦截Action返回的HTML
在Asp.Net MVC项目中通过重写ActionFilterAttribute中的方法,我们就可以在轻松的在Action方法执行前后做一些特殊的操作如:[身份认证.日志记录.内容截取等]. 但是我们 ...