Description
Recently an accident destroyed the eternal tranquility. Some greedy fools tried using powerful bombs to find the hidden treasure. Of course they failed and such behavior enraged those spirits--the consequence is that all the human villages nearby are haunted by ghosts.
In order to stop those ghosts as soon as possible, Panda the Archmage and Facer the great architect figure out a nice plan. Since the plain can be represented as grids of N rows and M columns, the plan is that we choose ONLY ONE cell in EACH ROW to build a magic tower so that each tower can use holy light to protect the entire ROW, and finally the whole plain can be covered and all spirits can rest in peace again. It will cost different time to build up a magic tower in different cells. The target is to minimize the total time of building all N towers, one in each row.
“Ah, we might have some difficulties.” said Panda, “In order to control the towers correctly, we must guarantee that every two towers in two consecutive rows share a common magic area.”
“What?”
“Specifically, if we build a tower in cell (i,j) and another tower in cell (i+1,k), then we shall have |j-k|≤f(i,j)+f(i+1,k). Here, f(i,j) means the scale of magic flow in cell (i,j).”
“How?”
“Ur, I forgot that you cannot sense the magic power. Here is a map which shows the scale of magic flows in each cell. And remember that the constraint holds for every two consecutive rows.”
“Understood.”
“Excellent! Let’s get started!”
Would you mind helping them?
Input
Each test case starts with a line containing 2 integers N and M (2<=N<=100,1<=M<=5000), representing that the plain consists N rows and M columns.
The following N lines contain M integers each, forming a matrix T of N×M. The j-th element in row i (Tij) represents the time cost of building a magic tower in cell (i, j). (0<=Tij<=100000)
The following N lines contain M integers each, forming a matrix F of N×M. The j-th element in row i (Fij) represents the scale of magic flows in cell (i, j). (0<=Fij<=100000)
For each test case, there is always a solution satisfying the constraints.
The input ends with a test case of N=0 and M=0.
Output
题目大意:一个n*m的矩阵,每个点有两个属性T和F,然后每行选出一个点,要求相邻两行选的点x、y满足abs(x - y) ≤ F(x) + F(y),求min(sum(T))。
思路:dp[i][j]代表选第 i 行第 j 列能得到的最小值,朴素的DP复杂度为O(nm²),数据范围无法承受。观察发现对于每一行,它上一行的每一个点只能影响一个区间(可以假设下一行的都是0),而当前行也只能取一个区间的最小值(假设上一行全部是0)。或者说,我们思考的时候可以考虑在每一行之间插入一行0(两个参数都是0),这样做并不影响结果。这种区间赋值取区间最小值的东东,线段树正合适。复杂度降为O(nmlog(m))。
PS:这题的n有可能等于1,题目坑爹,为了这个我居然调了好久……
代码(1562MS):
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; const int MAXN = ;
const int MAXM = ;
const int INF = 0x3f3f3f3f; int high[MAXN][MAXM], siz[MAXN][MAXM];
int n, m; int tree[MAXM * ], mint[MAXM * ]; inline void update_min(int &a, const int &b) {
if(a > b) a = b;
} inline void pushdown(int x) {
int ll = x << , rr = ll ^ ;
update_min(tree[ll], tree[x]);
update_min(tree[rr], tree[x]);
update_min(mint[ll], tree[ll]);
update_min(mint[rr], tree[rr]);
} inline void update(int x) {
int ll = x << , rr = ll ^ ;
mint[x] = min(mint[ll], mint[rr]);
} void update(int x, int left, int right, int L, int R, int val) {
if(L <= left && right <= R) {
update_min(tree[x], val);
update_min(mint[x], val);
}
else {
pushdown(x);
int ll = x << , rr = ll ^ ;
int mid = (left + right) >> ;
if(L <= mid) update(ll, left, mid, L, R, val);
if(mid < R) update(rr, mid + , right, L, R, val);
update(x);
}
} int query(int x, int left, int right, int L, int R) {
if(L <= left && right <= R) return mint[x];
else {
pushdown(x);
int ll = x << , rr = ll ^ ;
int mid = (left + right) >> , ret = INF;
if(L <= mid) update_min(ret, query(ll, left, mid, L, R));
if(mid < R) update_min(ret, query(rr, mid + , right, L, R));
return ret;
}
} int solve() {
for(int i = ; i <= n; ++i) {
memset(tree, 0x3f, sizeof(tree));
memset(mint, 0x3f, sizeof(mint));
for(int j = ; j <= m; ++j)
update(, , m, max(, j - siz[i - ][j]), min(m, j + siz[i - ][j]), high[i - ][j]);
for(int j = ; j <= m; ++j)
high[i][j] += query(, , m, max(, j - siz[i][j]), min(m, j + siz[i][j]));
}
int ret = INF;
for(int i = ; i <= m; ++i) update_min(ret, high[n][i]);
return ret;
} int main () {
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) break;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) scanf("%d", &high[i][j]);
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) scanf("%d", &siz[i][j]);
printf("%d\n", solve());
}
}
HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)的更多相关文章
-
题解 HDU 3698 Let the light guide us Dp + 线段树优化
http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...
-
HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)
Description “Farm Game” is one of the most popular games in online community. In the community each ...
-
hdu3698 Let the light guide us dp+线段树优化
http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...
-
HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...
-
HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)
Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...
-
HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)
Description A new Semester is coming and students are troubling for selecting courses. Students ...
-
HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)
Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...
-
hdu 3698 Let the light guide us(线段树优化&;简单DP)
Let the light guide us Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/O ...
-
HDU 3698 Let the light guide us
Let the light guide us Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. ...
随机推荐
-
linux 文件与进程
1. linux查看一个文件正被那些程序使用 fuser -hUsage: fuser [-fMuvw] [-a|-s] [-4|-6] [-c|-m|-n SPACE] [-k [-i] [-SIG ...
-
weblogic .NoClassDefFoundError: Could not initialize class sun.awt.X11Graphi
这个是常见问题,可以通过增加Weblogic的启动参数来解决: -Djava.awt.headless=true 你可以修改 startWebLogic.sh 文件. export JAVA_OPTI ...
-
ubuntu 设置vpn
百度了资料 http://jingyan.baidu.com/article/fa4125aca7f1b628ad709271.html 1. 设置 VPN CONNECTION 2.configur ...
-
redux入门指南
前言:大概一个月没有写博客了,这两天正好是周末,就写点东西来梳理下之前几个月的所写与所得; 大概两个月前,学习了一下 redux ,还是一点难度的,花了我一天的时间来搞明白他, 但是都没怎么记录,今天 ...
-
GET与POST请求的区别
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...
-
maven项目的配置
软件151 王帅 1.增加web.xml Maven项目最重要的配置文件是pom.xml,pom是“项目对象模型”的意思.现在pom.xml中有一个错误,提示缺少web.xml: 展开目录src—m ...
-
货币转换 I
描述 人民币和美元是世界上通用的两种货币之一,写一个程序进行货币间币值转换,其中: 人民币和美元间汇率固定为:1美元 = 6.78人民币. 程序可以接受人民币或美元输入,转换为美元或人民币输出.人民币 ...
-
The application was unable to start correctly (0xc000007b)
用VS2013建立了一个c++ console application,然后引用了一个DLL, 启动的时候报错: The application was unable to start correct ...
-
SpringMVC学习笔记四:数据绑定
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831344.html 参考:http://www.cnblogs.com/HD/p/4107674.html ...
-
扫盲贴,802.11AD
早在去年,大家已经开始关注新的802.11ad规范,其高频高带宽低延迟的特性也让大家对将来的无线网络应用形态充满了期待,而今年的CES2016展会上,已经有不少的802.11ad产品出现,看来2016 ...