HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)
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. ...
随机推荐
- Python 学习笔记(八)Python列表(一)
列表基本操作 列表(list)定义 列表是Python中的一种对象类型,也是一种序列 对象类型:list 表示方法:[ ] python 列表中的元素可以是任何类型的对象 >>> ...
- rest_framework -- mixins&generics
上面的mixins.generics都是rest_framework里的模块,我们可以继承其中的某些类,达到代码量减少的效果,这里充分体现出了面向对象的继承 一.mixins模块 mixins : f ...
- 微信小程序学习笔记(一)
1.目录及文件构成 1.1 根目录下 ** app.js 是小程序的脚本代码,用来监听并处理小程序的生命周期函数.声明全局变量. ** app.json 是对整个小程序的全局配置,配置小程序是由哪些页 ...
- NEC 框架规范 css reset
/* reset */html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquote,pre,hr,figure,table,captio ...
- [USACO5.2]蜗牛的旅行Snail Trails(有条件的dfs)
题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总是从棋盘的左上角出发.棋盘上有空的格子(用“.”来表示)和B 个路障 ...
- Java集合类——Set、List、Map、Queue接口
目录 Java 集合类的基本概念 Java 集合类的层次关系 Java 集合类的应用场景 一. Java集合类的基本概念 在编程中,常需要集中存放多个数据,数组是一个很好的选择,但数组的长度需提前指定 ...
- VULTR的VPS在centos的操作系统中出现网站无法访问 80端口被firewall禁止
导语:叶子在为一位客户配置web服务器环境的时候,出现网站不能访问的情况,但ping正常.客户的服务器是在VULTR上购买的VPS,安装的操作系统为centos 7.3.经过叶子的分析,认为是防火墙阻 ...
- Infinite Maze CodeForces - 196B
We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A ...
- Linux C语言结构体-学习笔记
Linux C语言结构体简介 前面学习了c语言的基本语法特性,本节进行更深入的学习. 预处理程序. 编译指令: 预处理, 宏定义, 建立自己的数据类型:结构体,联合体,动态数据结构 c语言表达式工具 ...
- TCD产品技术参考资料
1.Willis环 https://en.wikipedia.org/wiki/Circle_of_Willis 2.TCD仿真软件 http://www.transcranial.com/index ...