Description

Plain of despair was once an ancient battlefield where those brave spirits had rested in peace for thousands of years. Actually no one dare step into this sacred land until the rumor that “there is a huge gold mine underneath the plain” started to spread. 
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

There are multiple test cases. 
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

For each test case, output a line with a single integer, which is the minimum time cost to finish all magic towers.

题目大意:一个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)的更多相关文章

  1. 题解 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)

    Description     A new Semester is coming and students are troubling for selecting courses. Students ...

  7. 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 ...

  8. 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 ...

  9. 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. ...

随机推荐

  1. 代替eval执行字符串表达式

    function eval2(str) { var Fn = Function; return new Fn('return ' + str)(); }

  2. ATK-DataPortal 设计框架(二)

    在信息的交换过程中,总是有此相同相似的功能,由于业务的各自不同,由同一类型来处理诸如增删改查等常见的信息处理方式.从日常的对些类行为操作为生成的类分析,大量雷同的代码遍布整个项目.框架中xxxHand ...

  3. Python基础—10-常用模块:time,calendar,datetime

    #常用模块 time sleep:休眠指定的秒数(可以是小数) time:获取时间戳(从1970-01-01 00:00:00到此刻的秒数) localtime:将一个时间戳转换为一个对象,对象中包含 ...

  4. Java实现批量修改文件名,重命名

    平时下载的文件.视频很多都会有网址前缀,比如一些编程的教学视频,被人共享出来后,所有视频都加上一串长长的网址,看到就烦,所以一般会重命名后看,舒服很多,好了,不多说,直接上代码: 以下代码演示使用递归 ...

  5. MySQL提升课程 全面讲解MySQL架构设计-索引

    索引是什么? 索引是帮助MySQL高效获取数据的数据结构. 索引能干什么? 提高数据查询的效率. 索引:排好序的快速查找数据结构!索引会影响where后面的查找,和order by 后面的排序. 一. ...

  6. 【原创】os.chdir设置的工作路径和sys.path之间到底是个啥关系?

    转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9390455.html   直接放上测试后的结论(测试代码和截图过多,有兴趣的小伙伴可自己测试,未来看情况 ...

  7. date 参数(option)-d

    记录这篇博客的原因是:鸟哥的linux教程中,关于date命令的部分缺少-d这个参数的介绍,并且12章中的shell编写部分有用到-d参数 date 参数(option)-d与--date=" ...

  8. PHP计算翻页

    function fanye() { if ($total <= $num) { $list['curTotal'] = $total; } else { $offsetA = $start; ...

  9. 图解HTTP总结(8)——确认访问用户身份的认证

    Session 管理及 Cookie 应用 基于表单认证的标准规范尚未有定论,一般会使用Cookie来管理Session(会话). 基于表单认证本身是通过服务器端的Web应用,将客户端发送过来的用户I ...

  10. ISE中FPGA的实现流程

    一.ISE实现的步骤         在综合之后,我们开始启动FPGA在ISE中的实现过程,整个过程包括以下几个步骤:                 1.Translate              ...