学校1020: Jumping Cows
来源:互联网 发布:卓讯数据库打开 编辑:程序博客网 时间:2024/05/01 04:58
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
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
典型的贪心:
大意:奇数项相加,偶数项相减;中间可以跳过某些项。使得总和最大;
本质上就是找到一个数列上的极大值和极小值!!!
方法:用k表示奇数还是偶数;a[i]与a[i+1]比较。
#include<stdio.h>#include<string.h>int main(){ int n,a[150001]; int i,j,k; int t,sum; while(scanf("%d",&n)!=EOF) { memset(a,0,sizeof(a)); for(i=0; i<n; i++) scanf("%d",&a[i]); sum=0; k=1; for(i=0; i<n; i++) { if(k%2==1) { if(a[i]<a[i+1]) continue; sum+=a[i]; k++; } else if(k%2==0) { if(a[i]>a[i+1]) continue; sum-=a[i]; k++; } } printf("%d\n",sum); }}
0 0
- 学校1020: Jumping Cows
- Jumping Cows
- POJ 2181-Jumping Cows
- nyoj 356 Jumping Cows
- POJ 2181 Jumping Cows
- poj2181 hdu2713 Jumping Cows
- poj 2181 Jumping Cows
- poj 2181 Jumping Cows
- nyoj 356-Jumping Cows
- poj 2181 Jumping Cows
- poj 2181 Jumping Cows
- 2181 Jumping Cows 解题报告
- poj 2181 Jumping Cows DP
- POJ 2181 JUMPING COWS(DP)
- POJ 2181 Jumping Cows [贪心]
- hdu(2713)Jumping Cows
- poj 2181 Jumping Cows 贪心
- 【POJ】2181-Jumping Cows 贪心
- php strstr查找字符串中是否包含某些字符的查找函数
- 0400000173
- javascript 字符串比较函数
- PHP5 字符串处理函数大全
- 项目有感
- 学校1020: Jumping Cows
- CocoaChina测试题 小记一
- Java接口 抽象类 内部类
- 两点距离
- 学校1022: 进制转换问题
- 小学生算术
- 友元函数的几点注意事项和实例
- A+B Problem(V)
- UML之活动图