Luo Gu P1220 off street interval [dp]

Topic : https://www.luogu.org/problemnew/show/P1220

The meaning of problems : Given n and position lamp power, lamp c at the first stand initially.

Turn off the lights no time, walking speed is 1 unit / sec. Ask all the lights turned off, the minimum power is.

Idea: It looks interval dp is quite clear. Because time does not need to turn off the lights, since the passing on the way to shut it. So it must be a certain period of intermediate lights are off, each end section lit.

So we can use $ dp [i] [j] $ express i ~ j lights are off. However, the final closing of a $ i $ or $ j $ are still differences, so it needs to mark the one-dimensional.

Range and because of the need, so then $ sum $ prefix and maintenance intervals

Finally get the state transition equation

$dp[i][j][0] = min(dp[i+1][j][0] + (pos[i+1] - pos[i]) * (sum[i] + sum[n] - sum[j]), dp[i+1][j][1] + (pos[j] - pos[i]) * (sum[i] + sum[n] - sum[j]))$

$dp[i][j][1] = min(dp[i][j-1][1] + (pos[j] - pos[j - 1]) * (sum[n] - sum[j - 1] + sum[i - 1]), dp[i][j-1][0] + (pos[j] - pos[i]) * (sum[n] - sum[j - 1] + sum[i - 1]))$

To be noted that the lamp power of the two are different.

The initial situation and the end of the case are better judgment.

The number of lights in the middle, then update is actually enumerate off, and then enumerate the starting point on it.

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<map>
 4 #include<set>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<vector>
 8 #include<cmath> 
 9 #include<stack>
10 #include<queue>
11 #include<iostream>
12 
13 #define inf 0x7fffffff
14 using namespace std;
15 typedef long long LL;
16 typedef pair<int, int> pr;
17 
18 int n;
19 const int maxn = 55;
20 int c;
21 int light[maxn], sum[maxn], pos[maxn];
22 int dp[maxn][maxn][2];
23 
24 int main()
25 {
26     memset(dp, 0x3f, sizeof(dp));
27     scanf("%d%d", &n, &c);
28     for(int i = 1; i <= n; i++){
29         scanf("%d%d", &pos[i], &light[i]);
30         sum[i] = sum[i - 1] + light[i];
31     }
32     dp[c][c][0] = dp[c][c][1] = 0;
33     for(int num = 2; num <= n; num++){
34         for(int l = 1; l + num - 1 <= n; l++){
35             int r = l + num - 1;
36             int x = sum[l] + sum[n] - sum[r], y = sum[l - 1] + sum[n] - sum[r - 1];
37             dp[l][r][0] = min(dp[l][r][0], dp[l + 1][r][0] + (pos[l + 1] - pos[l]) * x);
38             dp[l][r][0] = min(dp[l][r][0], dp[l + 1][r][1] + (pos[r] - pos[l]) * x);
39             dp[l][r][1] = min(dp[l][r][1], dp[l][r - 1][1] + (pos[r] - pos[r - 1]) * y);
40             dp[l][r][1] = min(dp[l][r][1], dp[l][r - 1][0] + (pos[r] - pos[l]) * y);
41         }
42     }
43     
44     printf("%d\n", min(dp[1][n][0], dp[1][n][1]));
45     
46 }

 

Guess you like

Origin www.cnblogs.com/wyboooo/p/11079806.html