当前位置: 代码迷 >> 综合 >> HDU 3506 Monkey Party(动态规划)
  详细解决方案

HDU 3506 Monkey Party(动态规划)

热度:30   发布时间:2023-11-22 13:28:51.0

文章目录

  • 题目
  • AC代码


题目

本题链接:Monkey Party

本博客给出本题截图
在这里插入图片描述

AC代码

代码

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 2010, INF = 0x3f3f3f3f;int f[N][N];
int t[N];
int s[N];
int w[N][N];int main()
{
    int n;while(~scanf("%d", &n)){
    for (int i = 1; i <= n; i ++ ){
    cin >> t[i];t[i + n] = t[i];}memset(f, 0x3f, sizeof f);for (int i = 1; i <= 2 * n; i ++ ) {
    f[i][i] = 0;w[i][i] = i;s[i] = s[i - 1] + t[i];}for (int len = 2; len <= n; len ++ )for (int i = 1; i + len - 1 <= 2 * n; i ++ ){
    int j = i + len - 1;for (int k = w[i][j - 1]; k <= w[i + 1][j]; k ++ ){
    if (f[i][j] > f[i][k] + f[k + 1][j] + s[j] - s[i - 1]){
    f[i][j] = f[i][k] + f[k + 1][j] + s[j] - s[i - 1];w[i][j] = k;}}}int res = INF;for (int i = 1; i <= n; i ++ )res = min(res, f[i][i + n - 1]);cout << res << endl;}return 0;
}