Jumping Cows

来源:互联网 发布:日语视频同声翻译软件 编辑:程序博客网 时间:2024/04/26 21:01

PKU.2181

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

My Program

    #include<stdio.h>
    
int main()
    
{
        
int n,i,t,max,min,s=0;
        scanf(
"%d",&n);
        scanf(
"%d",&max);min=0;
        
for(i=1;i<n;i++)
        
{
            scanf(
"%d",&t);
            
if((min==0||t<min)&&!(max!=0&&t>max))
            
{
                min
=t;
                s
+=max;
                max
=0;
                
continue;
            }

            
if(t>max)
            
{
                max
=t;
                s
-=min;
                min
=0;
            }

        }

        s
+=max;
        printf(
"%d ",s);
        
return 0;
    }

 

 YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

 农夫想给奶牛增加弹跳力,兽医便配置了一些药。
 这些药在奇数时间吃会增加弹跳力,
 但是在偶数时间吃则会减少弹跳力。
 每种药有不同的效果,但是必须严格按照配置的顺序来吃,
 也可以跳过一些药不吃,但是一旦跳过了就不能再吃以前的药了。
 奶牛从时间1开始吃药。
 现在给你一个吃药的序列,
 问牛最多能增加多少弹跳力?


【粗略分析】

 经过观察可以发现,为得到最优解,
 每奇数次都是加上递增数列中最大的数,
 每偶数次则是减去递减数列中最小的数,
 符合贪心法的特点,因此本题应该使用贪心法来写 = =。

 因为药丸数P的范围在1 <= P <= 150,000;
 所以全部输入再计算明显会比较慢?
 因此定义了max,min两个变量分别记录递增数列最大值和递减数列最小值,
 根据输入情况进行判定、操作。
 本算法时间复杂度为o(n)。

【C++源代码】

    #include<stdio.h>
    
int main()
    
{
        
int n,i,t,max,min,s=0;
        scanf(
"%d",&n);
        scanf(
"%d",&max);min=0;
        
for(i=1;i<n;i++)
        
{
            scanf(
"%d",&t);
            
if((min==0||t<min)&&!(max!=0&&t>max))
            
{
                min
=t;
                s
+=max;
                max
=0;
                
continue;
            }

            
if(t>max)
            
{
                max
=t;
                s
-=min;
                min
=0;
            }

        }

        s
+=max;
        printf(
"%d ",s);
        
return 0;
    }

【注意事项】

 ※ 要给min赋值时该数必须<max才能算是递减数列。
 ※ 第一回只找max加,min初始为0。
 ※ 次数一定为奇数,因此最后还要再加一次max。


【点评】

 贪心法好好用 >_<
 跑题一下:
 可能是因为这题数据比较多的关系,
 起先用cin,G++时224k,140ms,受惊吓,
 改成scanf,GCC的格式(不过编译器选错成G++了懒得改)只用148k,31ms,
 再次验证了相同情况下C相对效率较高 = = ||

原创粉丝点击