zoj3310 DP题
2015-11-24 18:57
218 查看
将自己历史的AC共享
如觉得还有问题 可参考其他相关文章
zoj3310 其他1
zoj3310 其他2
zoj3310 DP题
dp[i][0]表示当前点不放,dp[i][1]放;做2次dp,第一个放和不放//2111652 2010-03-15 15:58:41 Accepted 3310 C++ 220 11904 greentea@FireStar #include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <ctime> #include <utility> #include <stdexcept> using namespace std; #define inf (1<<30) #define PB push_back #define mset(x,a) memset(x,(a),sizeof(x)) #define SIZE(X) ((int)X.size()) #define dbg(x) cerr<<#x<<" : "<<x<<endl #define rg(x,y) (x=x>y?x:y) typedef vector<int> VI; typedef vector<char> VC; typedef vector<string> VS; typedef long long LL; typedef unsigned long long uLL; #define twoL(X) (((LL)(1))<<(X)) const double PI=acos(-1.0); const double eps=1e-11; template <class T> T sqr(T x) {return x*x;} template <class T> T gcd(T a, T b) {if(a<0) return (gcd(-a,b)); if(b<0) return (gcd(a,-b)); return (b==0)?a:gcd(b,a%b);} template <class T> T lcm(T a, T b) {return a*b/gcd(a,b);} LL toLL(string s) { istringstream sin(s); LL t; sin>>t; return t;} int toInt(string s) {istringstream sin(s); int t; sin>>t; return t;} string toString(LL v) {ostringstream sout; sout<<v; return sout.str();} string toString(int v) {ostringstream sout; sout<<v; return sout.str();} #define FOREACH(it, a) for(typeof((a).begin()) it = (a).begin(); it!=(a).end(); ++it) #define ALL(x) ((x).begin(), (x).end()) #define cross(a, b, c) ((c).x-(a).x)*((b).y-(a).y)-((b).x-(a).x)*((c).y-(a).y) #define sq_dist(p, q) ((p).x-(q).x)*((p).x-(q).x)+((p).y-(q).y)*((p).y-(q).y) #define FF(i,n) for(int i = 0; i < n; ++i) int n; int a[1000005]; int dp[1000005][2]; int main() { while( scanf("%d", &n) != EOF ) { int k; for( int i = 0; i < n; ++i ) { scanf("%d", &a[i]); if( i == 0 ) dp[0][1] = a[i], dp[0][0] = -inf; else if( i == 1 ) { dp[i][0] = max(dp[0][1], dp[0][0]); dp[i][1] = dp[i-1][0]+a[i]; } else { dp[i][0] = max(dp[i-1][0], dp[i-1][1]); dp[i][1] = dp[i-1][0] + a[i]; } } int ans = dp[n-1][0]; for( int i = 0; i < n; ++i ) { if( i == 0 ) 4000 dp[0][1] = -inf, dp[0][0] = 0; else if( i == 1 ) { dp[i][0] = max(dp[0][1], dp[0][0]); dp[i][1] = dp[i-1][0]+a[i]; } else { dp[i][0] = max(dp[i-1][0], dp[i-1][1]); dp[i][1] = dp[i-1][0] + a[i]; } } ans = max(ans, dp[n-1][1]); ans = max(ans, dp[n-1][0]); printf("%d\n", ans); } return 0; }
如觉得还有问题 可参考其他相关文章
zoj3310 其他1
zoj3310 其他2
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析
- C#获取关键字附近文字算法实例