POJ 2181

直接DP,设当前第i个数为结尾的算奇数次和偶数次的得到的最大值。分开设即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;

int main(){
	int oddn,even,n,tmp,to,te;
	while(scanf("%d",&n)!=EOF){
		for(int i=1;i<=n;i++){
			scanf("%d",&tmp);
			if(i==1){
				oddn=tmp;
				even=0;
			}
			else{
				to=even+tmp;
				te=oddn-tmp;
				oddn=max(oddn,to);
				even=max(te,even);
			}
		}
		printf("%d\n",max(oddn,even));
	}
	return 0;
}
	

  

posted @ 2015-02-25 21:52  chenjunjie1994  阅读(209)  评论(0编辑  收藏  举报