[POJ 3168 Treats for the Cows]区间dp
分类:区间dp
1. 题目链接
[POJ 3168 Treats for the Cows]
2. 题意描述
有
n
个美味排成一列,编号为
3. 解题思路
最简单的区间dp。
sum(x,y)
表示区间x,y的区间价值和。预处理出前缀和之后可以O(1)求出。
dp[i][j]=max(dp[i+1][j]+vi+sum(i+1,j),dp[i][j−1]+vj+sum(i,j−1))
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;
}
本文介绍了一道经典的区间动态规划题目——《Treats for the Cows》的解题思路与实现细节。通过该题,读者将了解如何利用区间DP解决特定类型的问题,并掌握求解最大价值的方法。

被折叠的 条评论
为什么被折叠?



