Jumping Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6398   Accepted: 3828

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
题目大意:从一列数字中按照编号从小到大有选择的取数,若取到的数字的为第奇数个则加上该数,否则减去该数,问取到的数的最大总和。
解题方法:动态规划,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的更多相关文章

  1. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  2. POJ 2456 Agressive cows(二分)

    POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...

  3. POJ-2181 Jumping Cows(贪心)

    Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7329 Accepted: 4404 Descript ...

  4. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  5. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  6. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  7. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  8. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  9. poj 2182 Lost Cows(段树精英赛的冠军)

    主题链接:http://poj.org/problem? id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. CentOS-语言设置

    查看所有的locale语言 # locale -a # locale -a|grep en 查看当前操作系统使用的语言 # echo $LANG 设置系统locale语言为中文环境(永久生效) # v ...

  2. 在Python中使用help帮助

    在Python中使用help帮助 >>> import numpy >>> help(numpy.argsort) Help on function argsort ...

  3. java基础—线程(一)

    一.线程的基本概念

  4. HTML5语义

    语义通俗化为意义,也就是语义化的元素等于意义化的元素,看到这个元素的名称,就知道这个元素的意义,是拿来做什么用的,这就是HTML5的一个新特性,一个具有语义化的元素能够清楚的把元素的意义告诉浏览器和开 ...

  5. GCD和NSThread延时执行对比

    1.NSThread: [self performSelector:@selector(performSome:) withObject:self afterDelay:.f]; [[self cla ...

  6. Angular-constructor和ngOnInit区别

    参考文档:https://blog.csdn.net/u010730126/article/details/64486997 总结:constructor做依赖注入,避免业务操作: ngOninit做 ...

  7. 学习笔记(五): Feature Crosses

    目录 Feature Crosses Encoding Nonlinearity Kinds of feature crosses Glossay Crossing One-Hot Vectors P ...

  8. MySQL查询显示连续的结果

    #mysql中 对于查询结果只显示n条连续行的问题# 在领扣上碰到的一个题目:求满足条件的连续3行结果的显示 X city built a new stadium, each day many peo ...

  9. k8s的Pod控制器

    pod的配置清单常见选项: apiVersion,kind,metadata,spec,status(只读) spec: containers: nodeSelector: nodeName: res ...

  10. LeetCode1089复写零

    问题: 给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移. 注意:请不要在超过该数组长度的位置写入元素. 要求:请对输入的数组 就地 进行上述修改,不要 ...