POJ 2181 Jumping Cows DP

题意:n<=2e5件物品 按照1~n顺序选择物品,每件物品可以拿或者不拿,奇数次拿物品分数+a[i],偶数次拿分数-a[i] 问最大得分?
显然每件物品有2种状态,第奇数次拿,第偶数次拿(不拿话 ans就不统计第i个)
设dp[i][1,2] 前i件物品 最后一件状态i为1,2;
dp[i][1]=max(dp[j][2])+a[i] 1<=j<=i(j~i不拿) 保持前缀i中最大的dp[j][2]即可

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
const int N=2e5+20;
const int inf=2e8;
int n,a[N];
int dp[N][3];
int main()
{
	while(cin>>n)
	{
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		memset(dp,0,sizeof(dp));
		int ans=0,fir=0,sec=0;
		for(int i=1;i<=n;i++)
		{
			dp[i][1]=sec+a[i];
			dp[i][2]=fir-a[i];
			sec=max(sec,dp[i][2]);
			fir=max(fir,dp[i][1]);
			ans=max(ans,max(dp[i][1],dp[i][2]));
		}
		cout<<ans<<endl;

	}
	return 0;
}

  

时间: 2024-10-12 01:48:35

POJ 2181 Jumping Cows DP的相关文章

POJ 2181 -- Jumping Cows

Jumping Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7624   Accepted: 4586 Description Farmer John's cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump. The

POJ 3671 Dining Cows (DP)

题目大意:给你一串只有1,2的数字,让你改变最少的次数,让这个序列变成非递减的. 思路:动态规划,判断分界点,开一个dp[30010][2]的数组,其中dp[i][j]表示把第i个数改成j最少要花多少次 那么状态转移方程就列出来了: 令a=1 j!=a[i] 0 j==a[i] 那么dp[i][1]=dp[i-1][1]+a; dp[i][2]=min(dp[i-1][1],dp[i-1][2])+a; #include <iostream> using namespace std; int

POJ 3671 Dining Cows (DP,LIS, 暴力)

题意:给定 n 个数,让你修改最少的数,使得这是一个不下降序列. 析:和3670一思路,就是一个LIS,也可以直接暴力,因为只有两个数,所以可以枚举在哪分界,左边是1,右边是2,更新答案. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #includ

[poj2181]jumping cows

poj 2181 Jumping Cows Description Farmer John's cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump. The local witch doctor has mixed up P (1 <= P <= 150,000) potions to aid the

poj 3186 Treats for the Cows dp

#include <cstdio> #include <algorithm> using namespace std; #define maxn 2100 int dp[maxn][maxn]; int val[maxn]; int n; int main() { while(scanf("%d",&n)!=EOF) { int i,j; for(i=1;i<=n;i++) { scanf("%d",&val[i]);

POJ 1948 Triangular Pastures(DP)

Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite. I. M. Hei, the lead cow pasture architect, is in charge of creating a triangu

POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了. 本题要求装满的,故此增加个限制就可以了. #include <stdio.h> #include <stdlib.h> #include <string.h> inline int min(int a, int b) { return a < b? a : b; } c

POJ 3280 Cheapest Palindrome DP题解

看到Palindrome的题目,首先想到的应该是中心问题,然后从中心出发,思考如何解决. DP问题一般是从更加小的问题转化到更加大的问题,然后是从地往上 bottom up地计算答案的. 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需改动 else tbl[id][i] = min(tbl[!id][i+1] + cost[cowID[i]-'a'], tbl[!id][i

POJ 2342 (树形DP)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure