[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 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
/翻譯
農民約翰的奶牛喜歡跳過月亮,就像他們喜歡的童謠裏的奶牛一樣。不幸的是,奶牛不能跳躍。
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.
當地巫醫已經混合了P(1 < = < = 150000)藥水,以幫助奶牛們尋求跳躍。這些藥水必須按照它們創建的順序進行管理,盡管有些可能會被跳過。
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.
每種藥劑都有一種增強母牛跳躍能力的“力量”(1 < =力量= 500)。在一個奇數的時間段服用藥水會增加母牛的跳躍;在一個偶數的時間中服用藥水會降低跳躍。在服用任何藥劑之前,母牛的跳躍能力當然是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.
沒有藥水可以服用兩次,一旦奶牛開始服用藥水,每次一次必須服用一劑藥水,從時間1開始。每回合可跳過一個或多個藥水。
Determine which potions to take to get the highest jump.
確定哪種藥劑可以獲得最高的跳躍。
Emm貪心?
每次考慮當前是奇數還是偶數
奇數:盡可能大,比較是當前大還是後面的大
若當前大,取當前
偶數:盡可能小,比較是當前小還是後面的小
若當前小,去當前
1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<cstring>
6 #include<cmath>
7
8 using namespace std;
9
10 int ans,n,tmp,a[150101];
11 bool flag=1;
12 int read()
13 {
14 int x=0,f=1;
15 char ch=getchar();
16 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
17 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
18 return x*f;
19 }
20 int main()
21 {
22 n=read();
23 for(int i=1;i<=n;i++)a[i]=read();
24 for(int i=1;i<=n;i++)
25 {
26 if(flag==1 && a[i]>a[i+1])
27 {
28 ans+=a[i];
29 flag=0;
30 }
31 if(flag==0 && a[i]<a[i+1])
32 {
33 ans-=a[i];
34 flag=1;
35 }
36 }
37 printf("%d",ans);
38 system("pause");
39 return 0;
40 }
View Code[poj2181]jumping cows