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 ...
随机推荐
- mac开机启动apache、memcached与mysql
一.开机自动启动apache方法 #sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist //开机启动 ...
- Axis2发布服务,支持Tomcat和Weblogic的SSHWeb项目部署
先说下遇到的问题,在SSHWeb项目中使用JDK自带的jar发布WebService(Endpoint.publish),在tomcat下可以正常发布,但是在Weblogic报奇葩错误,如Struts ...
- 1. Oracle安装部署文档
一. 部署环境步骤 1.1 软件环境 操作系统:CentOS release 6.5oracle安装包:linux.x64_11gR2_database_1of1.zip:linux.x64_11gR ...
- C语言中如何计算时间差
#include <time.h> #include <stdio.h> int main() { time_t start ,end ; ...
- js浮点精度问题
1.先看下图: 2.为什么呢? 3.其实最早自己见过这样的情况,因为不懂得其中的真正道理,每次都是“猜”,结果就可想而知了. ==========原因========== 4.在控制台出现这样的情况在 ...
- IOS 第三方支付的使用:支付宝
本文转载至 http://blog.csdn.net/u014011807/article/details/47726799 总结一下支付宝iOS使用步骤: 1 第三方支付:支付宝 使用过程: 1. ...
- Java EE发展史
前言 最近的这段时间一直在学习Java EE,刚刚完成了从0到1的蜕变,所以顺便整理一下我所了解到的Java EE,给刚入门学习的新人一些头绪,而所谓“启示录”,就是这个意思. 一.Java EE是什 ...
- Caused by: java.lang.OutOfMemoryError: Failed to allocate a 29433932 byte allocation with 14683576 free bytes and 14MB
解决Android 内存溢出 其实你可以添加在你的清单 android:hardwareAccelerated="false" android:largeHeap="tr ...
- C++播放wav音乐和音效
1. #include <mmsystem.h>#pragma comment(lib,"winmm.lib")PlaySound(TEXT("c:\\te ...
- docker 快速搭建Nexus3
1.拉取镜像 docker pull sonatype/nexus3 2.启动容器 : -p : -p : -v /mnt/gv0/nexus-data:/nexus-data sonatype/ne ...