[思维]The Enchanted Forest Codeforces1688D

Marisa comes to pick mushrooms in the Enchanted Forest.

The Enchanted forest can be represented by nn points on the XX-axis numbered 11 through nn. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by a1,a2,…,ana1,a2,…,an.

Marisa can start out at any point in the forest on minute 00. Each minute, the followings happen in order:

  • She moves from point xx to yy (|x−y|≤1|x−y|≤1, possibly y=xy=x).
  • She collects all mushrooms on point yy.
  • A new mushroom appears on each point in the forest.

Note that she cannot collect mushrooms on minute 00.

Now, Marisa wants to know the maximum number of mushrooms she can pick after kk minutes.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn, kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤1091≤k≤109) — the number of positions with mushrooms and the time Marisa has, respectively.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial number of mushrooms on point 1,2,…,n1,2,…,n.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print the maximum number of mushrooms Marisa can pick after kk minutes.

Example

input

4

5 2

5 6 1 2 3

5 7

5 6 1 2 3

1 2

999999

5 70000

1000000000 1000000000 1000000000 1000000000 1000000000

output

12

37

1000000

5000349985

题意: 在一个n格的地图上,有若干蘑菇,每格初始蘑菇数为ai,现在可以任选一个起点,同时可以在k秒内移动,每秒移动一格,移动到某格后可以获得格子内的蘑菇数,同时所有格子蘑菇数增加1,初始时间为0秒,问到k秒时能获得的最大蘑菇数。

分析: 当k <= n时最终答案就是长度为k的最大区间和加上k*(k-1)/2,当k > n时考虑最终状态各个格子上的蘑菇数,因为每秒全图上蘑菇数会增加n,而最终一定可以把全图的蘑菇都走遍,所以只需要计算出浪费的蘑菇数,然后用总蘑菇数一减就是能获得的蘑菇数,而要想浪费的蘑菇数最少就需要最后走到端点,此时每个格子上的蘑菇数为n,n-1,......,3,2,1可以发现其他任何方式浪费的蘑菇数都比这样多,所以可以选择在第一个格子等待,当剩余时间为n-1时一直向右走遍所有格子,这样答案就是sum+n*k-n*(n+1)/2。

这道题如果想到了站在原地不动就会比较简单,但是当时想到的是不断来回移动,虽然答案是一样的,但是实现起来就麻烦一点了。

具体代码如下:

简单版

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#define int long long
using namespace std;

int a[200005];
int c[200005];
const int inf = 0x3f3f3f3f3f3f3f3f;

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int n, k;
		scanf("%lld%lld", &n, &k);
		int sum = 0, mx = -inf;
		for(int i = 1; i <= n; i++){
			scanf("%lld", &a[i]);
			if(i <= k) sum += a[i];
			else sum = sum+a[i]-a[i-k];
			mx = max(mx, sum);
		}
		if(k <= n)
			printf("%lld\n", mx+k*(k-1)/2);
		else
			printf("%lld\n", sum+n*k-n*(n+1)/2);
	} 
	return 0;
}

复杂版

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#define int long long
using namespace std;

int a[200005];
int c[200005];
const int inf = 0x3f3f3f3f3f3f3f3f;

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int n, k;
		scanf("%lld%lld", &n, &k);
		int sum = 0, mx = -inf;
		for(int i = 1; i <= n; i++){
			scanf("%lld", &a[i]);
			if(i <= k) sum += a[i];
			else sum = sum+a[i]-a[i-k];
			mx = max(mx, sum);
			c[i] = c[i-1]+2*i;
		}
		if(n == 1){
			printf("%d\n", a[1]+k-n);
			continue;
		}
		if(k <= n)
			printf("%lld\n", mx+k*(k-1)/2);
		else{ 
			int s = k%(n-1);
			if(s == 0) s = n-1; 
			int t = s*(s-1)/2;
			int path = k-s;
			printf("%lld\n", sum+t+(path/(n-1)-1)*c[n-1]+c[s-1]+(2*s-1+n+s-2)*(n-s)/2);
		}
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Fabric Loader是Minecraft的一个模组加载器,使用它可以加载一些自定义的模组来扩展游戏的功能。如果要让穿戴有“附魔保护”的鞋子的玩家在游戏中不受到坠落伤害,可以编写以下格式的Fabric Loader模组代码: ``` package net.example.mod; import net.minecraft.entity.Entity; import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import net.minecraft.world.World; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.player.AttackEntityCallback; public class ExampleMod implements ModInitializer { @Override public void onInitialize() { // Register the callback function for player attacks AttackEntityCallback.EVENT.register((PlayerEntity player, World world, Entity entity, int i, int j) -> { // Check if the player is wearing enchanted boots ItemStack boots = player.getEquippedStack(EquipmentSlot.FEET); if (boots.getItem() == Items.DIAMOND_BOOTS && boots.hasEnchant(Enchantments.FEATHER_FALLING)) { // Cancel fall damage entity.damage(DamageSource.FALL, 0f); } return ActionResult.PASS; }); } } ``` 这段代码注册了一个回调函数,当玩家攻击实体时会执行。在回调函数中,首先获取玩家当前穿戴的鞋子,然后检查鞋子是否为钻石鞋子,并且是否附有“附魔保护”的附魔。如果满足条件,则取消实体的坠落伤害。 因此,如果一个玩家在游戏中穿戴有这样的鞋子,那么即使他从高处掉落,也不会受到坠落伤害,可以起到保护作用。 ### 回答2: 要写一个Fabric Loader格式的Minecraft模组,使得穿戴带有附魔"摔落保护"的靴子的玩家不会踩坏耕地,可以按照以下步骤进行: 1. 先确定你已经安装了Fabric Loader和Fabric API,并在开发环境中设置好相关的文件结构和配置。 2. 创建一个新的Java类,命名为"EnchantmentProtectionMod"(可以根据个人喜好进行命名)。 3. 导入必要的类和接口,例如:`net.fabricmc.api.ModInitializer`和`net.fabricmc.fabric.api.event.player.BlockBreakEvents`。 4. 实现ModInitializer接口,并重写其中的`onInitialize`方法。 5. 在onInitialize方法中,注册一个监听器,监听玩家破坏方块的事件。 6. 在监听器的回调方法中,判断玩家所穿戴的靴子是否含有"摔落保护"附魔。 7. 如果玩家穿戴的靴子含有"摔落保护"附魔,判断即将被破坏的方块是否为耕地。 8. 如果即将被破坏的方块是耕地,取消方块破坏事件的监听,以避免玩家踩坏耕地。 9. 在完成代码编写后,进行编译并将生成的mod文件放入Minecraft的mod文件夹。 10. 启动Minecraft游戏,确保Fabric Loader正确加载并启用了你的mod。 11. 穿戴带有"摔落保护"附魔的靴子,并尝试在耕地上行走,确保不再踩坏耕地。 这是一个简单的实现示例,仅供参考。根据个人的需求和理解,可以进一步完善和优化这个mod。 ### 回答3: 要写一个Fabric Loader格式的Minecraft模组,使得穿戴带有附魔“摔落保护”的靴子的玩家不会踩坏耕地,可以按照以下步骤进行: 1. 首先,在你的开发环境中安装Fabric Loader和Minecraft开发工具包。 2. 创建一个新的Fabric mod项目,并编写mod的基本代码框架。 3. 定义一个新的附魔类型“耕地保护”,该附魔可以添加到靴子上。 4. 在靴子类中添加一个判断玩家是否穿着带有“耕地保护”附魔的靴子的方法。 5. 当玩家在踩踏方块时,通过这个方法判断玩家是否穿戴了带有“耕地保护”附魔的靴子。 6. 如果玩家穿戴了这样的靴子,则阻止玩家的踩踏行为对耕地方块造成破坏。 7. 添加其他必要的代码,如注册附魔、添加配置选项等。 8. 构建并测试你的模组,确保功能正常运作。 9. 发布你的模组,并与其他玩家共享。 通过上述步骤,你可以编写一个满足需求的Fabric Loader格式的Minecraft模组,使得穿戴带有附魔“摔落保护”的靴子的玩家不会踩坏耕地。模组的功能将增加游玩的便利性和可玩性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值