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. .net读取xml文件中文乱码问题解决

    读取XML的编码方式Encoding.UTF8 要和<?xml version="1.0" encoding="utf-8"?>的encoding ...

  2. 如何查看,关闭和开启selinux

    以下介绍一下SELinux相关的工具/usr/bin/setenforce 修改SELinux的实时运行模式setenforce 1 设置SELinux 成为enforcing模式setenforce ...

  3. Cassandra——类似levelDB的基于p2p架构的分布式NOSQL数据库

    C: Consistency 一致性 • A: Availability 可用性(指的是快速获取数据) • P: Tolerance of network Partition 分区容忍性(分布式) 1 ...

  4. kafka consumer属性

    consumer属性 group.id:指定consumer group的唯一标识. consumer.id:唯一标识consumer.默认值为null,不指定时会自动生成. zookeeper.co ...

  5. BZOJ1727 [Usaco2006 Open]The Milk Queue 挤奶队列

    贪心...我怎么不会QAQ[捂脸熊] 对于1.2两头牛,如果1号牛要排在2号牛前面才能时间更少,则 $$max(A_1 + B_1 + B_2, \ A_1 + A_2 + B_2) \le max( ...

  6. 转 velocity 模板使用总结

    Velocity是一个基于java的模板引擎.它允许任何人仅仅简单的使用模板语言来引用由java代码定义的对象. 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一 ...

  7. CentOS查看软件源提供的软件版本命令

    yum list available第二列包更详细的说明:yum info packagename

  8. Can't connect to local MySQL server through socket 问题解决

    Fedora8启动mysql 报错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/m ...

  9. 常州培训 day7 解题报告

    最后一天..有些感慨,这七天被虐的感动万分 第一题: 题目大意: 求出 n*i(i=1,2,3....n) mod p的逆元  n<p<=3000000 ,p是质数. 之前写过了,懒得再写 ...

  10. [转载]android的消息处理机制(图+源码分析)——Looper,Handler,Message

    2013-12-18 14:17:33 转载自: http://www.cnblogs.com/codingmyworld/archive/2011/09/14/2174255.html 请跳转到转载 ...