当前位置:网站首页>洛谷P1220 关路灯 题解 区间DP
洛谷P1220 关路灯 题解 区间DP
2022-06-10 00:34:00 【51CTO】
题目链接: https://www.luogu.com.cn/problem/P1220
本题涉及算法:区间DP。
我们一开始要做一些初始化操作,令:
- \(p[i]\) 表示第i个路灯的位置;
- \(w[i]\) 表示第i个路灯的功率;
- \(sum[i]\) 表示前i个路灯的总功率
我们设状态 \(f[l][r][i]\) 表示:
- 当 \(i=0\) 时,老张关了编号 \([l,r]\) 范围内的所有灯,并且此时老张在第 \(l\) 盏灯处(最左边)的最少消耗电量;
- 当 \(i=1\) 时,老张关了编号\([l,r]\) 范围内的所有灯,并且此时老张在第 \(r\) 盏灯处(最右边)的最少消耗电量。
则我们可以得出状态转移方程如下:
\[f[l][r][0] = min(f[l+1][r][0] + (sum[l] + sum[n] - sum[r]) * (p[l+1] - p[l]), f[l+1][r][1] + (sum[l] + sum[n] - sum[r]) * (p[r] - p[l])) \]
\[f[l][r][1] = min(f[l][r-1][0] + (sum[l-1] + sum[n] - sum[r-1]) * (p[r] - p[l]),f[l][r-1][1] + (sum[l-1] + sum[n] - sum[r-1]) * (p[r] - p[r-1])) \]
实现代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
int n, c;
long long p[maxn], // p[i]表示第i个路灯的位置
w[maxn], // w[i]表示第i个路灯的功率
sum[maxn], // sum[i]表示前i个路灯的总功率
f[maxn][maxn][2]; // f[l][r][0]表示关了[l,r]范围内的灯并且当前在l位置的最小功率;
// f[l][r][1]表示关了[l,r]范围内的灯并且当前在r位置的最小功率
const long long INF = (1LL<<60);
int main() {
cin >> n >> c;
for (int i = 1; i <= n; i ++) {
cin >> p[i] >> w[i];
sum[i] = sum[i-1] + w[i];
}
for (int i = 1; i <= n; i ++)
for (int j = i; j <= n; j ++)
f[i][j][0] = f[i][j][1] = INF;
f[c][c][0] = f[c][c][1] = 0; // 一开始在第c盏路灯不消耗电量
for (int len = 2; len <= n; len ++) {
for (int l = max(1, c-len+1); l+len-1 <= n; l ++) {
int r = l+len-1;
f[l][r][0] = min(f[l+1][r][0] + (sum[l] + sum[n] - sum[r]) * (p[l+1] - p[l]),
f[l+1][r][1] + (sum[l] + sum[n] - sum[r]) * (p[r] - p[l]));
f[l][r][1] = min(f[l][r-1][0] + (sum[l-1] + sum[n] - sum[r-1]) * (p[r] - p[l]),
f[l][r-1][1] + (sum[l-1] + sum[n] - sum[r-1]) * (p[r] - p[r-1]));
}
}
cout << min(f[1][n][0], f[1][n][1]) << endl;
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
边栏推荐
- 剑指 Offer II 014. 字符串中的变位词
- Interface control devaxpress WinForms -- new WXI skin grabs "fresh" view
- Mysql——》varchar
- Bubble sorting and optimization clear and intuitive C language
- nn.ModuleList()与nn.Sequential()
- hcip第一天总结
- Intranet penetration concise notes
- Flutter ITMS-90338: Non-public API usage - Frameworks/webview_ flutter_ wkwebview. framework
- Binary search (half search) summary
- Sword finger offer II 018 Valid palindrome
猜你喜欢

Flutter ITMS-90338: Non-public API usage - Frameworks/webview_ flutter_ wkwebview. framework

数据库之App.config配置文件错误

Sword finger offer II 018 Valid palindrome
Binary search (half search) summary

Sword finger offer II 020 Number of palindrome substrings

剑指 Offer II 020. 回文子字符串的个数

RHCSA第一天

Facial Emotion Recognition: State of the Art Performance on FER2013

剑指 Offer II 018. 有效的回文
Palindromes of past real questions of test questions date [11th session] [provincial competition] [group B]
随机推荐
Mysql——》事务
LEAK: ByteBuf.release() was not called before it‘s garbage-collected
if判断是否为空时的函数选择
MySQL -- how to solve the problem of data read consistency
Sword finger offer II 015 All modifiers in the string
Mysql——》事务的属性
ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks
洛谷P1028 数的计算 题解 动态规划入门题
[untitled]
Have you learned about arrays and slices in golang in go question bank · 1?
Solution to the C language problem of the sum of two numbers
Mysql——》varchar
BGP实验
ospf总结
Application of DFS and BFS in binary tree
剑指 Offer II 013. 二维子矩阵的和
Sword finger offer II 020 Number of palindrome substrings
BGP protocol experiment
Solution to C language problem of adding two numbers by force deduction
采云端&采云链:从订单协同到采购供应链,让采购供应链互联互通