# 题目题解

``````//#define fre yes

#include <cstdio>
#include <cstring>
#include <iostream>

const int N = 105;
int place[N], p[N], sum[N];
int f[N][N][2];

int main() {
static int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &place[i], &p[i]);
}
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + p[i];
}

memset(f, 1, sizeof(f));
f[m][m][0] = f[m][m][1] = 0;
for (int p = 1; p < n; p++) {
for (int i = 1, j = i + p; i <= n && j <= n; i++, j++) {
f[i][j][0] = std::min(f[i + 1][j][0] + (place[i + 1] - place[i]) * (sum[n] - sum[j] + sum[i]), f[i + 1][j][1] + (place[j] - place[i]) * (sum[n] - sum[j] + sum[i]));
//用小区间更新大区间
//place[i + 1] - place[i] -> 意思是走的秒数
//sum[n] - sum[j] + sum[i] -> 计算能量消耗
f[i][j][1] = std::min(f[i][j - 1][1] + (place[j] - place[j - 1]) * (sum[n] - sum[j - 1] + sum[i - 1]), f[i][j - 1][0] + (place[j] - place[i]) * (sum[n] - sum[j - 1] + sum[i - 1]));
}
}

printf("%d\n", std::min(f[1][n][0], f[1][n][1]));
//最后的状态不知道哪边更小 比一下
return 0;
}``````

「区间DP」关路灯

(0)
(0)