Jumping Cows

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 7329 Accepted: 4404

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

之所以说是贪心,因为这道题目用到了贪心的思想。加谷峰,减谷底,可以保证最后的值是最优的

#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h> using namespace std;
#define MAX 150000
int a[MAX+5];
int n;
int flag;
int ans;
int main()
{
while(scanf("%d",&n)!=EOF)
{
ans=0;
flag=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(a[i]>=a[i-1]&&a[i]>=a[i+1]&&flag==0)
{
ans+=a[i];
flag=1;
}
if(a[i]<=a[i-1]&&a[i]<=a[i+1]&&flag==1)
{
ans-=a[i];
flag=0;
}
}
printf("%d\n",ans);
}
return 0;
}

POJ-2181 Jumping Cows(贪心)的更多相关文章

  1. POJ 2181 Jumping Cows

    Jumping Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6398   Accepted: 3828 Desc ...

  2. poj 2456 Aggressive cows 贪心+二分

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25944   Accepted: 11982 ...

  3. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  4. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  5. POJ 2456 Agressive cows(二分)

    POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...

  6. POJ 2392 Space Elevator(贪心+多重背包)

    POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...

  7. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  8. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  9. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

随机推荐

  1. feed流拉取,读扩散,究竟是啥?

    from:https://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651961214&idx=1&sn=5e80ad6f2 ...

  2. C# Serializable对象序列化的作用

    http://www.cnblogs.com/linlf03/archive/2011/11/03/2234424.html 1.序列化定义:将对象转换为容易传输的格式的过程.例如,可以序列化一个对象 ...

  3. Masonry — 使用纯代码进行iOS应用的autolayout自适应布局

    本文转载至   http://www.ios122.com/2015/09/masonry/ 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstrain ...

  4. 查询SQlServer相同表结构差异

    USE [数据库名] GO ); ); ); ); SET @DataName1='库1'; SET @DataName2='库2'; SET @TableName1='表1'; SET @Table ...

  5. Objective-c官方文档 怎么使用对象

    版权声明:原创作品,谢绝转载!否则将追究法律责任.   对象发送和接受消息 尽管有不同的方法来发送消息在对象之间,到目前位置是想中括号那样[obj doSomeThing]:左边是接受消息的接收器,右 ...

  6. 【Python3】 PyV8的安装与使用

    centos7  python3.6.4 1 安装boost  :  PyV8 依赖于Boost yum install boost yum install boost-devel yum insta ...

  7. liunx trac 邮件提示功能

    http://trac.edgewall.org/wiki/TracNotification官网上提供的方法.个人觉得不是清楚,不过还是有参考价值的.以下写下自己的添加过程,以作记录. 1.the [ ...

  8. springbatch---->springbatch的使用(一)

    这里我们通过一个简单的实例来对springbatch做一个入门的体会.刚刚好,看见你幸福的样子,于是幸福着你的幸福. springbatch的入门案例 简单项目的结构如下: 一.引入maven的spr ...

  9. 【Ubuntu】更新系统时出现Hash校验和不符的错误(已解决)

    在使用 sudo apt-get update && sudo apt-get upgrade 命令更新系统时出现类似这样的错误信息: W: 无法下载 bzip2:/var/lib/a ...

  10. CSS学习之浮动

    首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...