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 ...
随机推荐
- ashx页面 “检测到有潜在危险的 Request.Form 值”的解决方法(控制单个处理程序不检测html标签)
如题: 使用web.config的configuration/location节点. 在configuration节点内新建一个location节点,注意这个节点和system.webserver那些 ...
- linux常用命令之tail
从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的 ...
- javascript基础学习(一)
javascript输出: javascript通过不同的方式来显示数据: (1)windows.alert()弹出警告框: (2)document.write()将方法写入HTML文档中: (3)i ...
- spring Mvc json返回json的日期格式问题
(一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...
- Java 8 与 .Net的平台发展
从早期版本中,我们已经可以领略到一些令人兴奋的特性.但是开发真Andrew C.Oliver表示,尽管如此,Java语言在某些特性上还是落后于.Net.比如,Java 8 中最令人期待的Lambda表 ...
- width:100%缩小窗口时背景图片出现空白bug
页面容器(#wrap)与页面头部(#header )为100%宽度.而内容的容器(#page)为固定宽度960px.浏览窗口缩小而小于内容层宽度时会产生宽度理解上的差异.如下图所示窗口宽度大于内容层宽 ...
- Android获取相册图片
1. AlertDialog的使用 2. 显示和隐式意图的区别 3. 相册页面的跳转 4. 选择完成后返回图片的获取 ----------------------------------------- ...
- C#【数据库】 Access类
using System; using System.Data; using System.Data.OleDb; namespace AccessDb { /**//// <summary&g ...
- php实现base64编码
工作需要,想弄一个加密的串,就想仿照base64的编码方式,写一个加密的方法,所以就有了下面这个用php实现的base64的代码 <?php /** * Base64 编码/解码 * @auth ...
- linux c数据库备份第一版
使用linuxC实现的mysql数据库备份目标:通过alarm信号定时备份数据库备注:目前是第一个版,本身不能定时备份可以结合linux自动化实现定时备份.运行平台:Linux或类unix测试平台:u ...