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. Git------创建本地库时绿色标志不显示

    转载: http://blog.csdn.net/zixiao217/article/details/77018392 解决方法: 按Win+R键打开运行对话框,输入 regedit.exe ,准备修 ...

  2. Dubbo -- 系统学习 笔记 -- 示例 -- 分组聚合

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 分组聚合 按组合并返回结果,比如菜单服务,接口一样,但有多种实现,用group区分 ...

  3. NetBpm 数据库(9)

    原文:http://blog.csdn.net/adicovofer/article/details/1718592 关注NetBpm也很久了,可是一直没有静下心来研究,为了生活的琐事,太过浮躁……今 ...

  4. Android图片的合成示例

    package com.example.demo; import android.os.Bundle; import android.app.Activity; import android.grap ...

  5. Scala单例对象和伴生对象

    1.Scala单例对象 Scala单例对象是十分重要的,没有像在Java一样,有静态类.静态成员.静态方法,但是Scala提供了object对象,这个object对象类似于Java的静态类,它的成员. ...

  6. jenkins构建的robot result结果不更新

    描述:构建的结果不进行更新,仍然显示以往的构建结果 定位原因:pybot 命令中生成的结果文件保存路径与构建后robot结果显示路径不一致所致 解决办法:修改二者的结果保存路径一致

  7. python cookie

    http://www.jayconrod.com/posts/17/how-to-use-http-cookies-in-python

  8. JavaWeb学习总结(十六)Cookie保存中文内容

    Cookie的值保存中文内容,可以使用Java.net.URLDecoder进行解码. 示例: <%@page import="java.net.URLDecoder"%&g ...

  9. this关键字制定对象的属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. flask获取传入参数的两种方式

    #coding=utf-8 from flask import Flask from flask import request app = Flask(__name__) @app.route(&qu ...