Gym 100513F - Ilya Muromets

本文深入探讨了伊利亚穆罗梅茨如何在有限的两次挥剑中,最大化地削减戈洛尼奇龙头部的火属性威力,以求在史诗般的战斗中取得胜利。通过分析头部数量、每次挥剑可斩数量及每个头部的火属性强度,提出了一种高效的战斗策略。

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

Ilya Muromets is alegendary bogatyr. Right now he is struggling against Zmej Gorynych, a dragonwith n headsnumbered from 1 to n from left to right.

Making one sweep of swordIlya Muromets can cut at most k contiguous heads ofZmej Gorynych. Thereafter heads collapse getting rid of empty space betweenheads. So in a moment before the second sweep all the heads form a contiguoussequence again.

As we all know, dragons canbreathe fire. And so does Zmej Gorynych. Each his head has a firepower. Thefirepower of the i-th head is fi.

Ilya Muromets has time forat most two sword sweeps. The bogatyr wants to reduce dragon's firepower asmuch as possible. What is the maximum total firepower of heads which Ilya cancut with at most two sword sweeps?

Input

The first line contains apair of integer numbers n and k (1 ≤ n, k ≤ 2·105) — thenumber of Gorynych's heads and the maximum number of heads Ilya can cut with asingle sword sweep. The second line contains the sequence of integer numbersf1, f2, ..., fn (1 ≤ fi ≤ 2000), where fi is the firepower ofthe i-th head.

Output

Print the required maximumtotal head firepower that Ilya can cut.

Sample test(s)

input

8 2
1 3 3 1 2 3 11 1

output

20

input

4 100
10 20 30 40

output

100

 

思路:

首先我是想先求一边连续K个数最大和,再把剩下的合并,再求一遍最大和

接着发现其实可以同时求两段K个数最大和


程序:
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
#include <cstdlib>
#include <cmath>
using namespace std;

const int N = 200005;

int max(int a, int b)
{
	if (a > b)
		return a;
	else
		return b;
}

struct node
{
	int x, y;
};

int f[N * 2];
__int64 sum[N * 2];

int main()
{
	int n, k;
	scanf("%d %d", &n, &k);

	memset(f, 0, sizeof(f));
	memset(sum, 0, sizeof(sum));

	int i;
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &f[i]);
		sum[i] = sum[i - 1] + f[i];
	}


	if (2 * k >= n)
	{
		printf("%I64d\n", sum[n]);
		return 0;
	}

	__int64 maxx = 0, maxy = 0;
	for (i = k; i <= n; i++)
	{
		if (sum[i] - sum[i - k] >= maxx)
		{
			maxx = sum[i] - sum[i - k];
		}
		maxy = max(maxy, maxx + sum[i + k] - sum[i]);
	}
	
	printf("%I64d\n", maxy);

	return 0;
}


### 使用PPO算法在gym-super-mario-bros环境中的实现 为了在 `gym-super-mario-bros` 游戏环境中应用近端策略优化 (Proximal Policy Optimization, PPO),可以按照以下方法构建模型并训练代理。以下是详细的说明: #### 安装依赖库 首先,确保安装必要的 Python 库来支持 `gym-super-mario-bros` 和强化学习框架 Stable Baselines3。 ```bash pip install nes-py gym-super-mario-bros stable-baselines3[extra] ``` 上述命令会安装 `nes-py`, `gym-super-mario-bros` 以及用于实现 PPO 的强化学习工具包 `Stable-Baselines3`[^1]。 --- #### 创建超级马里奥环境 通过导入 `SuperMarioBros-v0` 或其他变体创建游戏环境,并设置动作空间和观察空间。 ```python import gym_super_mario_bros from nes_py.wrappers import JoypadSpace from gym.spaces import Box from gym.wrappers import FrameStack from stable_baselines3.common.env_checker import check_env from stable_baselines3 import PPO # 初始化 Super Mario Bros 环境 env = gym_super_mario_bros.make('SuperMarioBros-v0') # 设置简化操作集 env = JoypadSpace(env, [['right'], ['right', 'A']]) # 将帧堆叠到一起以提供时间序列数据给神经网络 env = FrameStack(env, num_stack=4) # 验证环境是否兼容稳定基线的要求 check_env(env) ``` 此部分代码定义了一个简单的控制方案(右移或跳跃),并通过 `FrameStack` 提供连续四帧作为输入状态。 --- #### 训练PPO模型 使用 `stable-baselines3.PPO` 来初始化和训练代理。 ```python model = PPO( policy="CnnPolicy", env=env, verbose=1, tensorboard_log="./mario_ppo_tensorboard/" ) # 开始训练过程 model.learn(total_timesteps=int(1e6)) # 保存训练好的模型 model.save("ppo_mario") ``` 在此配置中: - **policy**: 使用卷积神经网络 (`CnnPolicy`) 处理图像型观测值。 - **total_timesteps**: 总共执行 $1 \times 10^6$ 时间步数进行训练。 - **tensorboard_log**: 可视化日志路径以便监控训练进展。 --- #### 测试已训练的模型 加载先前保存的模型并对环境运行推理测试。 ```python del model # 删除旧模型以防冲突 # 加载预训练模型 model = PPO.load("ppo_mario") state = env.reset() done = False while not done: action, _states = model.predict(state) state, reward, done, info = env.step(action) env.render() env.close() ``` 这段脚本展示了如何利用训练完成后的模型在游戏中做出决策。 --- ### 注意事项 1. 超参数调整对于性能至关重要。例如,更改学习率、批量大小或其他超参数可能显著影响收敛速度与最终效果。 2. 如果希望扩展功能,可考虑引入更复杂的奖励机制或者自定义环境封装器。 3. 对于更高难度级别(如世界 1-2 或以上),建议增加训练时间和样本数量。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值