比赛 |
20120709 |
评测结果 |
AAAAAEEEEE |
题目名称 |
磁性链 |
最终得分 |
50 |
用户昵称 |
CC |
运行时间 |
0.578 s |
代码语言 |
C++ |
内存使用 |
4.50 MiB |
提交时间 |
2012-07-09 11:47:04 |
显示代码纯文本
#include <cstdio>
#include <algorithm>
#include <cstring>
const int INF = 1000000000;
int n,m;
int a[1050],f[1050][1050];
int main() {
freopen("linka.in","r",stdin);
freopen("linka.out","w",stdout);
scanf("%d%d", &n, &m);
for (int i = 1;i <= m;i++) scanf("%d", &a[i]);
std::sort(a + 1,a + m + 1);
a[0] = 0;a[m + 1] = n + 1;
for (int i = 1;i <= m;i++) f[i][i] = (a[i + 1]- 1) - (a[i - 1] + 1);
for (int k = 2;k <= m;k++)
for (int i = 1;i <= n;i++) {
int j = i + k - 1,tmp = (a[j + 1] - 1) - (a[i- 1] + 1),o = INF;
for (int u = i;u <= j;u++)
o = std::min(o,f[i][u - 1] + f[u + 1][j]);
f[i][j] = o + tmp;
}
printf("%d\n", f[1][m]);
return 0;
}