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 ...
随机推荐
- 学习java随笔第一篇:搭建java平台(java se)
电脑系统:windows8 在这里介绍一下java平台的3个版本: Java SE--Java Standard Edition,Java的标准版,主要用于桌面级的应用和数据库开发. Java EE- ...
- Linux 自动更新时间
1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...
- 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)
转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...
- this point
// this.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespa ...
- elastic search 学习笔记
Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...
- POJ 2942.Knights of the Round Table (双连通)
简要题解: 意在判断哪些点在一个图的 奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...
- Jquery异步请求数据实例
一.Jquery向aspx页面请求数据 前台页面JS代码: $("#Button1").bind("click", function () { $.ajax({ ...
- java中如何操作数据库(增删改查)
EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满足条件的实体Bean.实体B ...
- yii2源码学习笔记(四)
继续了解组件Component.php /** * Returns a value indicating whether a property is defined for this componen ...
- dedecms由子目录访问修改为根目录访问
现在我需要将原来位于xampp/htdocs/zm下的网站修改到D:/lyh/webhome目录下访问,原来的访问路径为http://localhost/zm,现在为http://www.yihui. ...