您的位置:首页 > 其它

zoj3310 DP题

2015-11-24 18:57 218 查看
将自己历史的AC共享

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zoj dp 3310 zju 算法