poj3390

dp

View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

#define maxn 10005

int n, m;
int value[maxn];
int f[maxn];

void input()
{
scanf("%d%d", &m, &n);
for (int i = 1; i <= n; i++)
scanf("%d", &value[i]);
}

void work()
{
f[0] = 0;
value[0] = 0x3f3f3f3f;
for (int i = 1; i <= n; i++)
{
f[i] = f[i - 1] + (m - value[i]) * (m - value[i]);
int j = i - 1;
int temp = value[i];
while (value[j] + temp + 1 <= m)
{
temp += value[j] + 1;
f[i] = min(f[i], f[j - 1] + (m - temp) * (m - temp));
j--;
}
}
printf("%d\n", f[n]);
}

int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
input();
work();
}
return 0;
}

原文地址:https://www.cnblogs.com/rainydays/p/2209938.html