HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)

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):

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 const int MAXN = 110;
 8 const int MAXM = 5010;
 9 const int INF = 0x3f3f3f3f;
10 
11 int high[MAXN][MAXM], siz[MAXN][MAXM];
12 int n, m;
13 
14 int tree[MAXM * 4], mint[MAXM * 4];
15 
16 inline void update_min(int &a, const int &b) {
17     if(a > b) a = b;
18 }
19 
20 inline void pushdown(int x) {
21     int ll = x << 1, rr = ll ^ 1;
22     update_min(tree[ll], tree[x]);
23     update_min(tree[rr], tree[x]);
24     update_min(mint[ll], tree[ll]);
25     update_min(mint[rr], tree[rr]);
26 }
27 
28 inline void update(int x) {
29     int ll = x << 1, rr = ll ^ 1;
30     mint[x] = min(mint[ll], mint[rr]);
31 }
32 
33 void update(int x, int left, int right, int L, int R, int val) {
34     if(L <= left && right <= R) {
35         update_min(tree[x], val);
36         update_min(mint[x], val);
37     }
38     else {
39         pushdown(x);
40         int ll = x << 1, rr = ll ^ 1;
41         int mid = (left + right) >> 1;
42         if(L <= mid) update(ll, left, mid, L, R, val);
43         if(mid < R) update(rr, mid + 1, right, L, R, val);
44         update(x);
45     }
46 }
47 
48 int query(int x, int left, int right, int L, int R) {
49     if(L <= left && right <= R) return mint[x];
50     else {
51         pushdown(x);
52         int ll = x << 1, rr = ll ^ 1;
53         int mid = (left + right) >> 1, ret = INF;
54         if(L <= mid) update_min(ret, query(ll, left, mid, L, R));
55         if(mid < R) update_min(ret, query(rr, mid + 1, right, L, R));
56         return ret;
57     }
58 }
59 
60 int solve() {
61     for(int i = 2; i <= n; ++i) {
62         memset(tree, 0x3f, sizeof(tree));
63         memset(mint, 0x3f, sizeof(mint));
64         for(int j = 1; j <= m; ++j)
65             update(1, 1, m, max(1, j - siz[i - 1][j]), min(m, j + siz[i - 1][j]), high[i - 1][j]);
66         for(int j = 1; j <= m; ++j)
67             high[i][j] += query(1, 1, m, max(1, j - siz[i][j]), min(m, j + siz[i][j]));
68     }
69     int ret = INF;
70     for(int i = 1; i <= m; ++i) update_min(ret, high[n][i]);
71     return ret;
72 }
73 
74 int main () {
75     while(scanf("%d%d", &n, &m) != EOF) {
76         if(n == 0 && m == 0) break;
77         for(int i = 1; i <= n; ++i)
78             for(int j = 1; j <= m; ++j) scanf("%d", &high[i][j]);
79         for(int i = 1; i <= n; ++i)
80             for(int j = 1; j <= m; ++j) scanf("%d", &siz[i][j]);
81         printf("%d
", solve());
82     }
83 }
View Code
原文地址:https://www.cnblogs.com/oyking/p/3307484.html