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

  1. 8
  2. 7
  3. 2
  4. 1
  5. 8
  6. 4
  7. 3
  8. 5
  9. 6

Sample Output

  1. 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])表示当前取的是第偶数个。
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. int num[];
  7. int dp[][];
  8.  
  9. int main()
  10. {
  11. int n;
  12. scanf("%d", &n);
  13. for (int i = ; i <= n; i++)
  14. {
  15. scanf("%d", &num[i]);
  16. }
  17. for (int i = ; i <= n; i++)
  18. {
  19. dp[i][] = max(dp[i - ][], dp[i - ][] + num[i]);
  20. dp[i][] = max(dp[i - ][], dp[i - ][] - num[i]);
  21. }
  22. printf("%d\n", max(dp[n][], dp[n][]));
  23. return ;
  24. }

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. 如何检查SQL Server索引填充因子

    假如您有一个盛满水的玻璃杯,您要尝试再向这个玻璃杯中加水.结果会怎样呢?水会溢出来. SQL Server 的情况也是如此.当索引页填充满时,如果尝试添加新行,则 SQL Server 会将大约一半的 ...

  2. Uva 10635 Prince and Princess (LCS变形LIS)

    直接LCS是时间复杂度是O(p*q)的,但是序列元素各不相同,只要把其中一个序列映射成有序的, 另外一个序列再做相同的映射,没有的直接删掉,就变成了求另一个序列LIS. #include<bit ...

  3. 换个语言学一下 Golang (3)——数据类型

    在 Go 编程语言中,数据类型用于声明函数和变量. 数据类型的出现是为了把数据分成所需内存大小不同的数据,编程的时候需要用大数据的时候才需要申请大内存,就可以充分利用内存. Go 语言按类别有以下几种 ...

  4. 使用控件的Tag属性传递信息

    实现效果: 知识运用: Control类的Tag属性 //获取或设置包含 有关控件的数据的对象 public object Tag {get;set;} 实现代码: private void Form ...

  5. falling object思路总结

    1.用检测的方法把falling object标记为一个类别,然后检测出类别.这种方式不可行的原因:因为falling object可能是任何东西,所以可能是一个路锥,也可能是一个玻璃瓶,还可能是掉下 ...

  6. windows系统下的两个批处理命令

    启动应用:***.exe 关闭应用:taskkill /f /im ***.exe 保存为.bat文件

  7. Yii2应用的运行过程

    每一个框架都有一个入口脚本,Yii2也不例外.一般来说,对于Web应用的入口脚本是YiiBasePath/frontend/web目录下的index.php. 先观察这个文件: <?php de ...

  8. 【上下界网络流 二分】bzoj2406: 矩阵

    感觉考试碰到上下界网络流也还是写不来啊 Description Input 第一行两个数n.m,表示矩阵的大小. 接下来n行,每行m列,描述矩阵A. 最后一行两个数L,R. Output 第一行,输出 ...

  9. jquery图片切换插件jquery.cycle.js参数详解

    转自:国人的力量 blog.163.com/xz551@126/blog/static/821257972012101541835491/ 自从使用了jquery.cycle.js,我觉得再也不用自己 ...

  10. javascript实现原生ajax的几种方法介绍

    自从javascript有了各种框架之后,比如jquery,使用ajax已经变的相当简单了.但有时候为了追求简洁,可能项目中不需要加载jquery这种庞大的js插件.但又要使用到ajax这种功能该如何 ...