POJ 2181 -- Jumping Cows

Jumping Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7624   Accepted: 4586

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

Source

USACO 2003 U S Open Orange

思路:分奇偶考虑,奇数秒时比大,偶数秒时比小。但是目前并没有证明出这就是正确的(虽然找不到更优解)。

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 #define MAXN 150010
 8 using namespace std;
 9 int a[MAXN];
10 int main()
11 {
12     int n;
13     scanf("%d",&n);
14     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
15     int ans=0;
16     bool b=1;
17     for(int i=1;i<=n;i++)
18     {
19         if(b)
20         {
21             if(a[i]>a[i+1]) {ans+=a[i];b=0;}
22             else continue;
23         }
24         else
25         {
26             if(a[i]<a[i+1]) {ans-=a[i];b=1;}
27             else continue;
28         }
29     }
30     printf("%d",ans);
31     return 0;
32 }

POJ 2181

时间: 2024-10-11 00:46:05

POJ 2181 -- Jumping Cows的相关文章

POJ 2181 Jumping Cows DP

题意:n<=2e5件物品 按照1~n顺序选择物品,每件物品可以拿或者不拿,奇数次拿物品分数+a[i],偶数次拿分数-a[i] 问最大得分?显然每件物品有2种状态,第奇数次拿,第偶数次拿(不拿话 ans就不统计第i个)设dp[i][1,2] 前i件物品 最后一件状态i为1,2;dp[i][1]=max(dp[j][2])+a[i] 1<=j<=i(j~i不拿) 保持前缀i中最大的dp[j][2]即可 #include <iostream> #include <algori

[poj2181]jumping cows

poj 2181 Jumping Cows 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

POJ 2182 Lost Cows.(线段树)

~~~~ 数据太水,暴力无压力,不过还是用线段树写了下,表现了楼主对线段树的无限热爱之情. ~~~~ 题目连接:http://poj.org/problem?id=2182 大致题意;牛牛因XXXXXX原因乱序成一排,现已知每头牛前面有多少头牛比它的编号小(第一头牛前面没有当然就不列粗来了),求每头牛的编号. 思路:从后往前扫描,遇到a,则说明它是剩余序列的第a+1头牛. ~~~~ 暴力代码:(157ms) #include<cstdio> #include<algorithm>

poj 2186 Popular Cows 【强连通】

题目:poj 2186 Popular Cows 题意:n头牛,其中存在一些牛相互崇拜,具有传递性,问有多少头牛是被其他所有牛崇拜的. 分析:建立一个有向图,然后强连通缩点,之后求出度为0的点,假如存在多个,那么ans = 0,因为缩点之后如果x崇拜y,x也崇拜z,那么肯定y和z不能互相崇拜,不满足. 然后求出度为0的这个点缩点前环上有多少个点就ans AC代码: #include <cstdio> #include <vector> #include <iostream&g

POJ 2186 Popular Cows --强连通分量

题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样,这个有向图就变成了一个有向无环图. 在这个新的图中,只需知道出度为0的点有几个即可. 如果出度为0的点超过1个,则输出0:否则输出出度为0的点所代表的那个强连通分支的分量数即可. 用Tarjan求强连通分量 代码: #include <iostream> #include <cstdio&g

二分搜索 POJ 2456 Aggressive cows

题目传送门 1 /* 2 二分搜索:搜索安排最近牛的距离不小于d 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 using namespace std; 8 9 const int MAXN = 1e5 + 10; 10 const int INF = 0x3f3f3f3f; 11 int x[MAXN]; 12 int n, m; 13 14 bool check(int d)

POJ 2186 Popular Cows (强连通分量)

POJ 2186 Popular Cows 链接:http://poj.org/problem?id=2186 题意:每头奶牛都梦想着成为牧群中最受奶牛仰慕的奶牛.在牧群中,有N 头奶牛,1≤N≤10,000,给定M 对(1≤M≤50,000)有序对(A, B),表示A 仰慕B.由于仰慕关系具有传递性,也就是说,如果A 仰慕B,B 仰慕C,则A 也仰慕C,即使在给定的M 对关系中并没有(A, C).你的任务是计算牧群中受每头奶牛仰慕的奶牛数量. 思路:首先可以知道,在同一个强连通分量内的点一定互

poj 2456 Aggressive cows,二分,最大化最小值

描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗.为了不让牛互相伤害.John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢? 输入 有多组测试数据,以EOF结束. 第

POJ 2186 Popular Cows -- tarjan 缩点

链接: POJ 2186 Popular Cows 题意: 每一头牛都希望在牛群里面备受瞩目,在一个牛群中有N头牛(1<=N<=10000),你被给予M(1<=M<=50000)个关系对,形式如(A,B),这意味着A牛认为B牛比它更受欢迎,由于这种欢迎度是满足传递性的,那么若是A牛认为B牛更受欢迎,B牛认为C牛更受欢迎,那么A牛也会认为C牛更受欢迎.你的任务是计算出被所有牛受欢迎的牛的个数. 输入: 第一行两个整数 N 和 M 第2 到 M + 1 行,两个分开的数 A,B,意味着