比赛 NOIP2023模拟赛1 评测结果 AAAAAAAAAA
题目名称 奇怪的监狱 最终得分 100
用户昵称 yrtiop 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2023-11-13 12:12:15
显示代码纯文本
#include <bits/stdc++.h>
#define pb emplace_back
#define fir first
#define sec second

using i64 = long long;
using pii = std::pair<int, int>;

constexpr int maxn = 105;
int n, m, pos[maxn], s[maxn], f[maxn][maxn];

void chkmin(int& x, int y) { if (y < x) x = y; return ; }

int main() {
	freopen("prison.in", "r", stdin);
	freopen("prison.out", "w", stdout);
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= m; ++i)
		scanf("%d", &pos[i]), s[i] = pos[i] - pos[i - 1] - 1;
	s[m + 1] = n - pos[m]; ++m;
	for (int i = 1; i <= m; ++i)
		s[i] += s[i - 1];
	memset(f, 0x3f, sizeof(f));
	for (int i = 1; i <= m; ++i)
		f[i][i] = 0;
	for (int len = 2; len <= m; ++len) {
		for (int i = 1; i + len - 1 <= m; ++i) {
			int j = i + len - 1;
			for (int k = i; k < j; ++k)
				chkmin(f[i][j], f[i][k] + f[k + 1][j] + s[j] - s[i - 1] + len - 2);
		}
	}
	printf("%d\n", f[1][m]);
	return 0;
}