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. android(eclipse)编程中常见的java问题总结(四)

    0:java流:   流是具有方向的   在文件操作中java流分为字节流:Filereader和Filewriter字符流:FileOutputStream,FileInputSream   例如在 ...

  2. o'Reill的SVG精髓(第二版)学习笔记——第一章

    1.1图形系统 计算机中描述图形信息的两大系统是栅格系统(raster graphics)和矢量图形(vector graphics) 1.1.4矢量图形的用途 ①计算机辅助绘图(CAD)程序. ②设 ...

  3. TCP套接字

    端口的概念 每个电脑一根网线,但是你挂着QQ的同时还可以浏览网页.两个不同应用的数据在同一根网线里是如何传输的呢?根据七层互联网模型,这个功能由运输层(TCP是运输层主要协议)实现.怎么实现呢,在网络 ...

  4. Oracle 用户、授权、角色管理

    Oracle 用户管理 一.创建用户的Profile文件SQL> create profile student limit // student为资源文件名FAILED_LOGIN_ATTEMP ...

  5. python之selectors

    selectors是select模块的包装器,ptython文档建议大部分情况使用selectors而不是直接使用selectors 样例代码如下 # -*- coding: utf-8 -*- __ ...

  6. 使用poi读取excel文件 Cannot get a text value from a numeric cell

    我这样转换得到一个excel文本域的值 Cell cell = row.getCell(c); cell.setCellType(Cell.CELL_TYPE_STRING); String park ...

  7. B. Train Seats Reservation 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

    You are given a list of train stations, say from the station 1 to the station 100. The passengers ca ...

  8. Laravel 集合的处理

    其中的方法有: $arrs = collect($arr)->collapse()->collapse() //去除最外一层数组,不论最外层数组时否有值,都会去除掉 collect($ar ...

  9. Python 2.6.6升级到Python2.7.15

    最近在使用Python处理MySQL数据库相关问题时,需要用到Python2.7.5及以上版本,而centos6.5等版本操作系统默认自带的版本为2.6.6,因此需要对python进行升级. Pyth ...

  10. 理解HBase

    1.HBase HBase: Hadoop Database,根据Google的Big Table设计 HBase是一个分布式.面向列族的开源数据库.HDFS为Hbase提供了底层的数据存储服务,Ma ...