Jumping Cows_贪心
Description
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
* 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
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_贪心的更多相关文章
- hdoj--1087--Super Jumping! Jumping! Jumping!(贪心)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 「LuoguP4753」濑 River Jumping(贪心
Description 有一条宽度为 N 的河上,小D位于坐标为 0 的河岸上,他想到达坐标为 N 的河岸上后再回到坐标为 0 的位置.在到达坐标为 N 的河岸之前小D只能向坐标更大的位置跳跃,在到达 ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- 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 ...
- POJ-2181 Jumping Cows(贪心)
Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7329 Accepted: 4404 Descript ...
- 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 ...
- BZOJ(begin) 1328 [Usaco2003 Open]Jumping Cows:贪心【波峰波谷模型】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1328 题意: 给你一个长度为n的正整数序列. 可以选任意个数字,只能从左往右选. 偶数 ...
- cf 11B Jumping Jack(贪心,数学证明一下,,)
题意: 给一个数X. 起始点为坐标0.第1步跳1格,第2步跳2格,第3步跳3格,.....以此类推. 每次可以向左跳或向右跳. 问最少跳几步可以到坐标X. 思路: 假设X是正数. 最快逼近X的方法是不 ...
- P4753 River Jumping
P4753 River Jumping 题目描述 有一条宽度为 NN 的河上,小D位于坐标为 00 的河岸上,他想到达坐标为 NN 的河岸上后再回到坐标为 00 的位置.在到达坐标为 NN 的河岸之前 ...
随机推荐
- 关于SQL语句优化的一个问题
今天写了一个很简单的存储过程,结果一执行,40多秒,后来调整了一句话写法,瞬间出来,其实差别不大,如下: select item_no=vpc.ITEM_ID ,BL_QTY=sum(vpc.QTY_ ...
- Generic泛型
1.问题 未使用泛型时,元素的类型不安全:操作麻烦,可能需要强制转换import java.util.ArrayList;import java.util.List;import org.junit. ...
- Mysql date_sub函数使用
mysql中内置函数date_add和date_sub能对指定的时间进行增加或减少一个指定的时间间隔,语法如下: DATE_ADD(date,INTERVAL expr type) DATE_SUB( ...
- Octopus系列之重新规范了模板结构,大家快来看啊
模板结构我是这样来设计的:大家请看,下面四个关键字 frontend[前段]backend[后端]member[会员]widget[部件] 前段页面包括 _Frontend_header_1.html ...
- PHP多线程类
<?php /** * @title: PHP多线程类(Thread) * @version: 1.0 * @author: phper.org.cn < web@phper.org.cn ...
- 二模 (2) day2
第一题: 题目描述: 在一个长方形框子里,最多有 N(0≤N≤6)个相异的点.在其中任何-个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能放 ...
- ASP.NET MVC学习之路由篇(3)
根据路由输出链接 既然是网站开发自然少不了链接,我们已经学会了强大的路由,但是还缺少一步就是能够将这些路由的路径输出到页面,下面我们就开始学习如何输出路由路径. 首先我们的路由注册部分如下所示: 1 ...
- C++-sizeof和strlen的区别
一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等. 它的功能是:获得保 ...
- IE11无法 登陆银行网站
1,打开IE11,看着键盘,按住Alt+X,然后按字母O打开IE设置选项=>[安全]选项卡把安全级别拉到最下,关闭[启用保护模式] 2,点击[受信任的站点]将支付宝和农业银行网址添加进去,关闭选 ...
- DB2使用笔记
1.赋予用户LOAD权限的步骤 使用实例用户db2inst1登录数据库: 使用命令db2 update dbm cfg using sysadm_group dasadm1给管理员用户组d ...