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

【题意】牛想跳上月球,但是他们无法跳跃,巫师发明了n颗药丸,只能使用一次并且按照给出顺序使用,奇数次是增加弹跳高度,偶数次降低,求最大能达到的最大高度。

【思路】求出结果最大的子序列,奇数位置+,偶数位置- 。发现只要找出整个序列的极大值点和极小值点就可,极大值点要+,极小值点要- 。找完后从头到尾扫一遍,根据需要找点(比如当前是奇数位置,那么就要找下一个极大值点;当前是偶数位置,那么就要找下一个极小值点)

参考:http://www.cnblogs.com/naturepengchen/articles/4025344.html

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int a[N],vis[N];
int main()
{
int n;
while(scanf("%d",&n))
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
if(a[]>a[]) vis[]=;
else vis[]=-;
for(int i=;i<n;i++)
{
if(a[i]>=a[i-]&&a[i]>=a[i+]) vis[i]=;
else if(a[i]<=a[i-]&&a[i]<=a[i+]) vis[i]=-;
}
if(a[n]>a[n-]) vis[n]=;
else vis[n]=;
int flag=,ans=;
for(int i=;i<=n;i++)
{
if(vis[i]==flag)
{
ans+=flag*a[i];
flag=-flag;
}
} printf("%d\n",ans);
}
return ;
}

Jumping Cows_贪心的更多相关文章

  1. hdoj--1087--Super Jumping! Jumping! Jumping!(贪心)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. 「LuoguP4753」濑 River Jumping(贪心

    Description 有一条宽度为 N 的河上,小D位于坐标为 0 的河岸上,他想到达坐标为 N 的河岸上后再回到坐标为 0 的位置.在到达坐标为 N 的河岸之前小D只能向坐标更大的位置跳跃,在到达 ...

  3. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  4. Codeforces Round #598 (Div. 3) A,B,C,D{E,F待补}

    A. Payment Without Change   #include<bits/stdc++.h> using namespace std; #define int long long ...

  5. POJ-2181 Jumping Cows(贪心)

    Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7329 Accepted: 4404 Descript ...

  6. Codeforces Round #598 (Div. 3) C. Platforms Jumping 贪心或dp

    C. Platforms Jumping There is a river of width n. The left bank of the river is cell 0 and the right ...

  7. BZOJ(begin) 1328 [Usaco2003 Open]Jumping Cows:贪心【波峰波谷模型】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1328 题意: 给你一个长度为n的正整数序列. 可以选任意个数字,只能从左往右选. 偶数 ...

  8. cf 11B Jumping Jack(贪心,数学证明一下,,)

    题意: 给一个数X. 起始点为坐标0.第1步跳1格,第2步跳2格,第3步跳3格,.....以此类推. 每次可以向左跳或向右跳. 问最少跳几步可以到坐标X. 思路: 假设X是正数. 最快逼近X的方法是不 ...

  9. P4753 River Jumping

    P4753 River Jumping 题目描述 有一条宽度为 NN 的河上,小D位于坐标为 00 的河岸上,他想到达坐标为 NN 的河岸上后再回到坐标为 00 的位置.在到达坐标为 NN 的河岸之前 ...

随机推荐

  1. HDUOJ 2672---god is a girl 《斐波那契数》

    god is a girl Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. listview加载性能优化ViewHolder

    在android开发中Listview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用户可以自由的定义listview每一列的布局, 但当listview有大量的数据需要加载的时候 ...

  3. 去除DataTable重复数据的三种方法

    业务需求 最近做一个把源数据库的数据批次导出到目标数据库.源数据库是采集程序采集而来的原始数据库,所以需要对其进行一些处理(过滤一些为空,长度太短或太长,非法字符,重复数据)然后在进行入库. 其中要避 ...

  4. ArangoDB介绍——未知架构和底层原理

    ArangoDB介绍 ArangoDB是一个开源NoSQL数据库,官网:https://www.ArangoDB.org/ArangoDB支持灵活的数据模型,比如文档Document.图Graph以及 ...

  5. C++调用父类的构造函数规则

    构造方法用来初始化类的对象,与父类的其它成员不同,它不能被子类继承(子类可以继承父类所有的成员变量和成员方法,但不继承父类的构造方法).因此,在创建子类对象时,为了初始化从父类继承来的数据成员,系统需 ...

  6. css改变图片的颜色

    参考大神张鑫旭:http://www.zhangxinxu.com/wordpress/2016/06/png-icon-change-color-by-css/ 主要知识点:border-right ...

  7. HADOOP :: java.lang.ClassNotFoundException: WordCount

    I am using eclipse to export the jar file of a map-reduce program. When i am run the jar using comma ...

  8. Visual Studio安装过程

    在这里需要先跟老师说一声抱歉,因为编写代码的愿意,我早在大一的时候就已经安装并且购买了正版的VS2013.所以今天在这里实在无法全部描述VS2013的安装过程. 然而,我所知的是,VS2013相对于我 ...

  9. JS事件大全

    橙色表示“非常常用”  绿色表示“常用” onClick IE3|N2|O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick IE4|N4|O 鼠标双击事件 onMouseD ...

  10. Android 自定义属性

    values新建一个attrs.xml<resource>    <declare-styleable name = "MyTextView">    &l ...