[POJ 3168 Treats for the Cows]区间dp

本文介绍了一道经典的区间动态规划题目——《Treats for the Cows》的解题思路与实现细节。通过该题,读者将了解如何利用区间DP解决特定类型的问题,并掌握求解最大价值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[POJ 3168 Treats for the Cows]区间dp

分类:区间dp

1. 题目链接

[POJ 3168 Treats for the Cows]

2. 题意描述

n 个美味排成一列,编号为1,2,...,n,第 i 个美味价值为vi。每次只能挑选两端的美味,第 a 次挑选到第i个美味,可获得价值 via 。求可以获得的最大的价值。

3. 解题思路

最简单的区间dp。
sum(x,y) 表示区间x,y的区间价值和。预处理出前缀和之后可以O(1)求出。
dp[i][j]=max(dp[i+1][j]+vi+sum(i+1,j),dp[i][j1]+vj+sum(i,j1))

4. 实现代码

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

typedef long long LL;
typedef long double LB;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const LB eps = 1e-8;
const int MAXN = 2000 + 5;

template <typename T>
inline bool scan_d (T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return 0; //EOF
    while (c != '-' && (c < '0' || c > '9') ) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
template<typename T>
void print(T x) {
    static char s[65], *s1; s1 = s;
    if (!x) *s1++ = '0';
    if (x < 0) putchar('-'), x = -x;
    while(x) *s1++ = (x % 10 + '0'), x /= 10;
    while(s1-- != s) putchar(*s1);
}
template<typename T> void println(T x) { print(x); putchar('\n');}

LL n, v;
LL dp[MAXN][MAXN], sum[MAXN];

inline LL s(int i, int j) { return sum[j] - sum[i - 1]; }
template<typename T> inline void umax(T& a, T b) { a = max(a, b); }

int main() {
#ifdef ___LOCAL_WONZY___
    freopen("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
    while(scan_d(n)) {
        sum[0] = 0;
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; ++i) {
            scan_d(v);
            dp[i][i] = v;
            sum[i] = sum[i - 1] + v;
        }
        for(int k = 2; k <= n; k++) {
            for(int i = 1, j; (j = i + k - 1) <= n; ++i) {
                umax(dp[i][j], dp[i + 1][j] + s(i + 1, j) + dp[i][i]);
                umax(dp[i][j], dp[i][j - 1] + s(i, j - 1) + dp[j][j]);
            }
        }
        println(dp[1][n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值