石子(环形)合并问题_dp
石子合并问题(第三章题目)
Time Limit: | 2000MS | Memory Limit: | 30000K |
#include
#include
#include
#include
#define N 210
#define INF 0x3f3f3f3f
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)>(b)?(b):(a)
using namespace std;
int n;
longa[N];
longres_min[N][N], res_max[N][N], s[N][N];
int main()
{
scanf("%d", &n);
int i, j, k, l;
for (i = 1;
i <= n;
i++)
{
scanf("%d", &a[i]);
a[i + n] = a[i];
} long last = clock();
for (i = 1;
i <= 2 * n - 1;
i++)
{
longsum = 0;
for (j = i;
j <= 2 * n - 1;
j++)
{
sum += a[j];
s[i][j] = sum;
}
}
for (k = 2;
k <= n;
k++)
{
for (i = 1;
i <= 2 * n - k+ 1;
i++)
{
j = i + k - 1;
res_max[i][j] = res_max[i + 1][j] + s[i][j];
res_min[i][j] = res_min[i + 1][j] + s[i][j];
for(l = i + 1;
l < j;
l++)
{
res_min[i][j] = min(res_min[i][j], res_min[i][l] + res_min[l + 1][j] + s[i][j]);
res_max[i][j] = max(res_max[i][j], res_max[i][l] + res_max[l + 1][j] + s[i][j]);
}
}
}
long resmin = INF, resmax = -INF;
for (i = 1;
i <= n;
i++)
{
resmin = min(resmin, res_min[i][i + n -1]);
resmax = max(resmax, res_max[i][i + n -1]);
}
printf("%lld\n%lld\n", resmin,resmax);
long cur = clock();
double tm = (double(cur) - last) / CLOCKS_PER_SEC;
printf("时间:%lf\n", tm);
return 0;
}
推荐阅读
- 如何将多个小分子文件合并为单个
- JAVA|JAVA 最好用的大文件切割与合并
- Python实用技法第33篇(字符串连接及合并)
- 两个已排序数组的合并
- git|git 合并两个远程分支
- Leetcode数组easy|Leetcode数组easy | 88. 合并两个有序数组
- Qt实战|Qt+OpenCV联合开发(十三)--通道分离与合并
- Leetcode617.|Leetcode617. 合并二叉树
- 【golang】leetcode初级-合并两个有序数组&第一个错误版本
- 每日一练(14)(合并两个排序的链表)