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. hdu----(2848)Repository(trie树变形)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  2. hdu-----(3746)Cyclic Nacklace(kmp)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. springMvc配置编码过滤器

    在web.xml中配置 <!-- 编码过滤器 --> <filter> <filter-name>characterEncodingFilter</filte ...

  4. (转载整理)SAP ERP常用表

    abap 常用表 GL部分:FAGLFLEXT(FMGLFLEXT)   新总账汇总表 GLT0        旧总帐汇总表           SKA1        总账科目主记录 (科目表)   ...

  5. bug数量问题研究

    最近感觉很扯蛋的事情就是测试人员提bug的问题.先说下前提,公司测试会以提bug数量来做为一部分员工绩效的成份.再说一下公司从需求到开发 到测试,先是需求出一个文档,开发根据文档做功能的开发,然后测试 ...

  6. js私有共有成员

      在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScr ...

  7. 如何管理好项目的DLL

    .net fx自带的dll net fx自带的dll,直接添加,注意.net fx版本即可. 第三方类库 如果是第三方类库,使用NuGet从NuGet官网下载,比如json.net,jQuery等. ...

  8. Jquery.Page.js 分页插件的使用

    1.简单直接贴代码 需要引用以下样式和脚本 <link href="~/Scripts/Page/pager.css" rel="stylesheet" ...

  9. 谈谈CSS的布局,display、position、float

    前言 前端一直是我的一个很大的缺憾,这段时间痛顶思痛,决定好好的把前台的东西加强,这不,在学习了一段时间js之后,在做一些小练习,却发现最基本的一些css知识却还不了解,所以便有了这篇博文. 块级元素 ...

  10. PULL解析

    PULL解析类似于SAX解析,都采用事件驱动(利用getEventType()方法)方式进行解析,当PULL解析器开始解析之后,可以不断地调用PULL解析器的next()方法获取下一个解析事件(开始文 ...