POJ-2181 Jumping Cows(贪心)
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.
OutputLine 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(贪心)的更多相关文章
- POJ 2181 Jumping Cows
Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6398 Accepted: 3828 Desc ...
- poj 2456 Aggressive cows 贪心+二分
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25944 Accepted: 11982 ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- POJ 2456 Agressive cows(二分)
POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...
- POJ 2392 Space Elevator(贪心+多重背包)
POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
随机推荐
- Missing iOS Distribution signing identity
打包上传appstore的时候报错如下: 解决方法: Download https://developer.apple.com/certificationauthority/AppleWWDRCA.c ...
- ant 打批量渠道包,第三方项目作为library打包出错的问题
ant批量打包不解释了.详情请看前面其它的blog! 这里说一下 开发中,非常可能遇到引用第三方的项目作为library情况.这样的情况下该怎么打包呢? 第一:改动build.xml 第二:在init ...
- Git Step by Step – (3) Git对象模型
前面一篇文章介绍了本地仓库的一系列操作,下面我们将进一步了解Git的工作原理,介绍Git对象模型. 刚开始使用Git的时候,对Git对象模型.工作原理并不理解,但是经过一段时间的使用.熟悉之后,然后再 ...
- exchange 2003配置ASSP 反垃圾邮件
Exchange上第三方反垃圾邮件用得比较多的是ORF,它直接运行在虚拟SMTP服务上,配置非常的方便.ASSP(https://sourceforge.net/projects/assp/) 是一个 ...
- 【代码审计】DouPHP_v1.3代码执行漏洞分析
0x00 环境准备 DouPHP官网:http://www.douco.com/ 程序源码下载:http://down.douco.com/DouPHP_1.3_Release_20171002. ...
- 【代码审计】EasySNS_V1.6 前台XSS跨站脚本漏洞分析
0x00 环境准备 EasySNS官网:http://www.imzaker.com/ 网站源码版本:EasySNS极简社区V1.60 程序源码下载:http://es.imzaker.com/ind ...
- iOS开发--设置UIButton
1.设置title [btn setTitle: @"search" forState: UIControlStateNormal]; 2.设置字体 //[btn setFont: ...
- SaltStack salt-ssh 用法
以下在 master 操作: (1) 我们在安装部署 SaltStack 的时候,需要安装 salt 客户端,还要与 salt 服务端进行认证才能互相通信(2) 如果我们使用 salt-ssh 的方式 ...
- Unity使用OpenGL绘制经纬线圈
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LatLonGr ...
- 用MyEclipse将Maven Dependencies中的jar包导出
1.右击pom.xml文件,选择Run As ——> Maven build… 2.在打开的页面中,如图输入“dependency:copy-dependencies”,后点击“Run”即可 ...