nyoj 贪心(动态规划也可,重点是贪心策略)Jumping Cows

在一系列药剂中,寻找最佳组合以最大化跳跃能力。每轮选择一次药剂,奇数轮增加能力,偶数轮减少,直至完成所有药剂的分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7 2 1 8 4 3 5 6  可分成 7 (2 1) 8 (4 ,3) (5 6);  在奇数时间取上升序列中最大的,在偶数时间取下降序列中最小的。不过最后有一点需要注意,当整个序列进行完时,最后肯定是加上一个数,7 (2 1) 8 (4 3) (5 6) 1
给你n个数,找一些数(可以不连续,但顺序不可乱),奇数位置的数+,偶数位置的数-。最后得出的最大结果是?
从前往后搜,交替执行:
(1)找一个比相邻两个数都大的数,+;
(2)找一个比相邻两个数都小的数,-;

Jumping Cows

时间限制:3000 ms  |           内存限制:65535 KB
难度:2
描述
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.
输入
The first line consist only one integer N, indicates N cases follows. In each case, there are two lines:
* 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.
输出
For each case
* Line 1: A single integer that is the maximum possible jump.
样例输入
1
8
7
2
1
8
4
3
5
6
样例输出           17           

//7 2 1 8 4 3 5 6  可分成 7 (2 1) 8 (4 ,3) (5 6);  在奇数时间取上升序列中最大的,在偶数时间取下降序列中最小的。不过最后有一点需要注意,当整个序列进行完时,最后肯定是加上一个数,7 (2 1) 8 (4 3) (5 6) 1 
//给你n个数,找一些数(可以不连续,但顺序不可乱),奇数位置的数+,偶数位置的数-。最后得出的最大结果是?
//从前往后搜,交替执行:
//(1)找一个比相邻两个数都大的数,+;
//(2)找一个比相邻两个数都小的数,-;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int cases=scanner.nextInt();
		while(cases--!=0)
		{
			int number=scanner.nextInt();
			int result[]=new int[number+2];
			for(int i=1;i<=number;i++)
				result[i]=scanner.nextInt();
			int flag=1,sum=0;
			for(int j=1;j<=number;j++)
			{
				if(flag==1)
				{
					if(result[j]>=result[j-1]&&result[j]>=result[j+1])
					{
						sum+=result[j];flag=0;
					}
				}
				else {
					if(result[j]<=result[j-1]&&result[j]<=result[j+1])
					{
						sum-=result[j];flag=1;
					}
				}
			}
			System.out.println(sum);
		}
	}
}
                                                        
                        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值