博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZJUT OJ 1007
阅读量:6153 次
发布时间:2019-06-21

本文共 2270 字,大约阅读时间需要 7 分钟。

Jumping Cows 

Time Limit:1000MS  Memory Limit:32768K

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:

872184356

Sample Output:

17

Source:

USACO 2003 U S Open Orange
 
code:
1 /* 2 贪心算法。注意边际条件。  3 */ 4 #include 
5 #include
6 using namespace std; 7 8 int main() 9 {10 for (int n; cin >> n;) {11 int strength = 0,12 count = 1;13 vector
ve;14 for (int i = 0; i < n; i++) {15 int p;16 cin >> p;17 ve.push_back(p);18 }19 for (int j = 0; j < ve.size() - 1; j++) {20 if (count % 2 == 0 && ve[j] < ve[j + 1]) {21 strength -= ve[j];22 count++;23 } 24 if (count % 2 != 0 && ve[j] > ve[j + 1]) {25 strength += ve[j];26 count++;27 } 28 }29 if (count % 2 != 0) strength += ve[ve.size() - 1];30 cout << strength << endl;31 }32 }

 

转载于:https://www.cnblogs.com/findingsea/archive/2012/09/09/2677736.html

你可能感兴趣的文章
while()
查看>>
常用限制input的方法
查看>>
IIS7下使用urlrewriter.dll配置
查看>>
并行程序设计学习心得1——并行计算机存储
查看>>
bulk
查看>>
C++ 迭代器运算
查看>>
【支持iOS11】UITableView左滑删除自定义 - 实现多选项并使用自定义图片
查看>>
【算法笔记】多线程斐波那契数列
查看>>
java8函数式编程实例
查看>>
jqgrid滚动条宽度/列显示不全问题
查看>>
在mac OS10.10下安装 cocoapods遇到的一些问题
查看>>
css技巧
查看>>
Tyvj 1728 普通平衡树
查看>>
javascript性能优化
查看>>
多路归并排序之败者树
查看>>
java连接MySql数据库
查看>>
转:Vue keep-alive实践总结
查看>>
深入python的set和dict
查看>>
C++ 11 lambda
查看>>
Android JSON数据解析
查看>>