JZOJ 1406. 教主的花园
2017-06-14 19:58
239 查看
Description
教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值。教主最喜欢3种树,这3种树的高度分别为10,20,30。教主希望这一圈树种得有层次感,所以任何一个位置的树要比它相邻的两棵树的高度都高或者都低,并且在此条件下,教主想要你设计出一套方案,使得观赏价值之和最高。
Input
输入的第1行为一个正整数n,表示需要种的树的棵树。接下来n行,每行3个不超过10000的正整数ai,bi,ci,按顺时针顺序表示了第i个位置种高度为10,20,30的树能获得的观赏价值。
第i个位置的树与第i+1个位置的树相邻,特别地,第1个位置的树与第n个位置的树相邻。
Output
输出仅包括一个正整数,为最大的观赏价值和。Sample Input
41 3 2
3 1 2
3 1 2
3 1 2
Sample Output
11Data Constraint
Hint
【样例说明】第1~n个位置分别种上高度为20,10,30,10的树,价值最高。
【数据规模】
对于20%的数据,有n≤10;
对于40%的数据,有n≤100;
对于60%的数据,有n≤1000;
对于100%的数据,有4≤n≤100000,并保证n一定为偶数。
分析
比较明显的DP模型,只不过虽然是环状的,但是每个点的状态跟确定的区间长度无关,所以只需要枚举第1棵树的高度(特别注意当高度为20时要分下一棵树是10还是30),链状DP即可。f[i][1..4]表示分别前i棵树,第i棵树的高度为10(下棵树肯定要比它高),20(下棵树比它高),20(下棵树比它矮),30(下棵树肯定要比它矮)的最大价值。转移也显而易见了。
代码
#include <algorithm> #include <iostream> #include <cstring> #include <complex> #include <cstdio> #include <queue> #include <cmath> #include <map> #include <set> #define N 100010 #define INF 0x3f3f3f3f #define sqr(x) ((x) * (x)) #define pi acos(-1) int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();} while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();} return x * f; } int high [3]; int f [4]; void init() { f[1][0] = f[1][1] = f[1][2] = f[1][3] = -INF; } int n; void dp() { for (int i = 2; i <= n; i++) { f[i][0] = std::max(f[i - 1][2], f[i - 1][3]) + high[i][0]; f[i][1] = f[i - 1][3] + high[i][1]; f[i][2] = f[i - 1][0] + high[i][1]; f[i][3] = std::max(f[i - 1][1], f[i - 1][0]) + high[i][2]; } } int ans; void work() { init(); f[1][0] = high[1][0]; dp(); ans = std::max(ans, std::max(f [2], f [3])); init(); f[1][1] = high[1][1]; dp(); ans = std::max(ans, f [3]); init(); f[1][2] = high[1][1]; dp(); ans = std::max(ans, f [0]); init(); f[1][3] = high[1][2]; dp(); ans = std::max(ans, std::max(f [0], f [1])); } int main() { n = read(); for (int i = 1; i <= n; i++) high[i][0] = read(), high[i][1] = read(), high[i][2] = read(); work(); printf("%d\n",ans); }
相关文章推荐
- 【JZOJ 1792】教主的花园(二分)
- jzoj1792 教主的花园 (二分)
- JZOJ 1407. 教主的游乐场
- 洛谷 P1133 教主的花园
- JZOJ 1331——超级教主【dp】
- jzoj. 3522. 【NOIP2013模拟11.7B组】迷宫花园(maze)
- 【枚举】教主的花园
- 洛谷 P 1133 教主的花园
- LG-P1133 教主的花园
- 洛谷 P1133 教主的花园(多维DP)
- dp——洛谷P1133 教主的花园
- JZOJ 1331. 超级教主
- 【二分】教主的花园解题报告
- 教主的花园
- jzoj.1792 教主的花园//2018.2.7
- 教主的花园
- 教主的花园
- 洛谷1133 教主的花园
- 教主的花园(洛谷 1133)
- 洛谷 P1133 教主的花园