POJ 2181 Jumping Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6398 | Accepted: 3828 |
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
题目大意:从一列数字中按照编号从小到大有选择的取数,若取到的数字的为第奇数个则加上该数,否则减去该数,问取到的数的最大总和。
解题方法:动态规划,dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + num[i])表示当前取的是第奇数个,dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - num[i])表示当前取的是第偶数个。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; int num[];
int dp[][]; int main()
{
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &num[i]);
}
for (int i = ; i <= n; i++)
{
dp[i][] = max(dp[i - ][], dp[i - ][] + num[i]);
dp[i][] = max(dp[i - ][], dp[i - ][] - num[i]);
}
printf("%d\n", max(dp[n][], dp[n][]));
return ;
}
POJ 2181 Jumping Cows的更多相关文章
- 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-2181 Jumping Cows(贪心)
Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7329 Accepted: 4404 Descript ...
- 二分搜索 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 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- poj 2182 Lost Cows(段树精英赛的冠军)
主题链接:http://poj.org/problem? id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
随机推荐
- document.all.item作用
1.document.all.myCheckBox和 document.all.item通过控件的名字定位控件,item()中是控件的名字例如:<input type="checkbo ...
- 关于日志造成的频繁的IO
记录日志可能消耗大量的IO [Q] 每次写入都是一个IO操作 即使是同一个文件 两次写入也要打开两次IO操作 [F] 设想有这样一个扩展 把php中要记录的日志 用文件名 和 内容的方式记录在内存中 ...
- SAP公有云和私有云解决方案概述
SAP公有云解决方案见下图最右侧,比较著名的有SAP SuccessFactors和SAP Cloud for Customer(C4C)等,作为SAP软件即服务(SaaS)的解决方案. 而最左侧的S ...
- HDU 5459 Jesus Is Here (递推,组合数学)
有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...
- 设置DataGridView单元格的文本对齐方式
实现效果: 知识运用: DataGridViewCellStyle类的Alignment属性 //获取或设置DataGridView单元格内的单元格内容的位置 public DataGridV ...
- Problem T: 结构体--学生信息排序
Problem T: 结构体--学生信息排序 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2219 Solved: 1305[Submit][Sta ...
- 绘制方式和OpenGL枚举对应关系
绘制方式和OpenGL枚举对应关系 图元类型 OpenGL枚举量 点 GL_POINTS 线 GL_LINES 条带线 GL_LINE_STRIP 循环线 GL_LINE_LOOP 独立三角形 GL_ ...
- python 函数内使用全局变量
x = def change_global(): global x x = x + change_global() print(x) result: 2
- 解决vs 编译的bug“请检查是否是磁盘空间不足、路径无效或权限不够”
昨晚用vs编译遇到一个问题,编译一半发现硬盘没空间,一直重启vs,重启电脑, 删除pdb文件都没用,之后尝试重新生成解决方案,就解决了.这个是vs的一个bug
- cocos2d-x中的基本动作
判断一个精灵被点击: 1.层要接收点击消息.2.回调函数中取得点击坐标.3.取得精灵用boudingBox().containsPoint函数判断.(或使用 convertTouchToNodeSpa ...