poj2181 jumping cow

umping Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7579   Accepted: 4559

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 cows in their quest to jump. These potions must be administered exactly in the order they were created, though some may be skipped.

Each potion has a ‘strength‘ (1 <= strength <= 500) that enhances the cows‘ jumping ability. Taking a potion during an odd time step increases the cows‘ jump; taking a potion during an even time step decreases the jump. Before taking any potions the cows‘ jumping ability is, of course, 0.

No potion can be taken twice, and once the cow has begun taking potions, one potion must be taken during each time step, starting at time 1. One or more potions may be skipped in each turn.

Determine which potions to take to get the highest jump.

Input

* Line 1: A single integer, P

* Lines 2..P+1: Each line contains a single integer that is the strength of a potion. Line 2 gives the strength of the first potion; line 3 gives the strength of the second potion; and so on.

Output

* Line 1: A single integer that is the maximum possible jump.

Sample Input

8
7
2
1
8
4
3
5
6

Sample Output

17

简单dp,设f[i][0]表示选了到i个,用完奇数时间的最大值,f[i][1]表示选到第i个,用完偶数时间的最大值,转移方程显而易见。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8 int n;
 9 int a[150005];
10 int f[2][2];
11 int main()
12 {
13     scanf("%d",&n);
14     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
15     int now=1;
16     bool hh=0;
17     for(int i=1;i<=n;i++)
18     {
19         hh^=1;
20         f[hh][1]=max(f[hh^1][1],f[hh^1][0]+a[i]);
21         f[hh][0]=max(f[hh^1][1]-a[i],f[hh^1][0]);
22     }
23     cout<<max(f[hh][1],f[hh][2]);
24     //system("pause");
25 }

时间: 2024-11-02 23:23:47

poj2181 jumping cow的相关文章

[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

洛谷P3120 [USACO15FEB]Cow Hopscotch

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John&apos;s cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but thi

POJ3615 Cow Hurdles【Floyd】

Cow Hurdles Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6155 Accepted: 2760 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are gett

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 3615 Cow Hurdles (Floyd算法)

Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6142   Accepted: 2752 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are

【Usaco2015 FEB】Cow Hopscotch (Gold)

Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has surp

【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has

【POJ 3615】Cow Hurdles

Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6486   Accepted: 2948 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are

洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles

洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to be able to use as little ene