题目https://www.luogu.org/problemnew/show/P1220

题意:给定n盏灯的位置和功率,初始时站在第c盏处。

关灯不需要时间,走的速度是1单位/秒。问把所有的灯关掉,最少功率是多少。

思路:看上去是区间dp还挺清楚的。因为关灯不需要时间,既然路过了就顺便关了吧。所以肯定是中间某一段的灯都被关了,两端各有一段亮着。

所以我们可以用$dp[i][j]$表示i~j号灯都被关了。但是最后关的是$i$还是$j$还是有差别的,所以还需要一维来标记。

因为需要区间和,所以再用$sum$维护区间前缀和

最后得到状态转移方程

$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]))$

要注意两个情况灯的功率是不同的。

初始情况和结束情况都比较好判断。

中间更新的话其实就是枚举关了的灯的数量,然后枚举起点就可以了。

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n;
const int maxn = ;
int c;
int light[maxn], sum[maxn], pos[maxn];
int dp[maxn][maxn][]; int main()
{
memset(dp, 0x3f, sizeof(dp));
scanf("%d%d", &n, &c);
for(int i = ; i <= n; i++){
scanf("%d%d", &pos[i], &light[i]);
sum[i] = sum[i - ] + light[i];
}
dp[c][c][] = dp[c][c][] = ;
for(int num = ; num <= n; num++){
for(int l = ; l + num - <= n; l++){
int r = l + num - ;
int x = sum[l] + sum[n] - sum[r], y = sum[l - ] + sum[n] - sum[r - ];
dp[l][r][] = min(dp[l][r][], dp[l + ][r][] + (pos[l + ] - pos[l]) * x);
dp[l][r][] = min(dp[l][r][], dp[l + ][r][] + (pos[r] - pos[l]) * x);
dp[l][r][] = min(dp[l][r][], dp[l][r - ][] + (pos[r] - pos[r - ]) * y);
dp[l][r][] = min(dp[l][r][], dp[l][r - ][] + (pos[r] - pos[l]) * y);
}
} printf("%d\n", min(dp[][n][], dp[][n][])); }

最新文章

  1. C# listview 单击列头实现排序 &lt;二&gt;
  2. Senparc.Weixin.MP SDK 微信公众平台开发教程(九):自定义菜单接口说明
  3. mysql管理知识点
  4. PHP get_class_methods函数用法
  5. mysql计划任务
  6. Social networks and health: Communicable but not infectious
  7. php5.3不支持 ereg、ereg_replace等函数问题,如提示:Deprecated: Function ereg() is deprecated
  8. 运行phpize时出现:Cannot find autoconf. Please check your autoconf installation
  9. Fling!
  10. HDU 4883 TIANKENG’s restaurant (贪心)
  11. 【转载】FaceBook - How to add a Privacy Policy to your Apps?
  12. iOS 多线程NSThread理解与场景示例
  13. CAS单点登陆 SSO
  14. webpack2使用ch7-loader解析css 自动添加浏览器前缀
  15. 禁用大陆ip段
  16. ue4 笔记
  17. 顶层const
  18. .net core 路由处理请求流程图
  19. video组件的使用
  20. 关于EasyPoi导出Excel

热门文章

  1. 验证您的Shell为Bash
  2. IO模式和IO多路复用详解
  3. 开源库SRT编译指南
  4. has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
  5. Excel去除空行
  6. 强连通图 Tarjan算法
  7. (转)Linux之split命令详解
  8. MapReduce的输出格式
  9. SQL判断经纬度在矩形内
  10. Unity GL 画圆