P9852 [ICPC2021 Nanjing R] Windblume Festival 题解(SPJ)

news2025/2/25 3:41:40

[ICPC2021 Nanjing R] Windblume Festival

单击此处下载原神

题面翻译

给一个长度为 n n n 环形整数序列 a a a, 每次操作可以任意选择一个下标 x x x,令 $ a_x = a_x - a_{(x\bmod n)+1}$,之后移除 a ( x   m o d   n ) + 1 a_{(x\bmod n)+1} a(xmodn)+1

最后会剩下一个元素,要求最大化这个元素。

数据范围 1 ≤ n ≤ 1 0 6 , − 1 0 9 ≤ a i ≤ 1 0 9 1\le n\le10^6,-10^9\le a_i\le 10^9 1n106,109ai109

题目描述

The Windblume Festival in Mondstadt is coming! People are preparing windblumes for Barbatos and for those they love and adore. The Windblume Festival is also an opportunity to improve the relationships people have.

During the festival, a famous game will be played every year, invented by Jean, the Acting Grand Master of the Knights of Favonius. In the game, n n n players numbered from 1 1 1 to n n n stand in a circle, each holding an integer with them. Each turn, one player will be removed. The game will end when there is only one player left.

For each turn, let k k k be the number of players remaining and a i a_i ai be the integer player i i i holds. Two adjacent players, x x x and ( x   m o d   k + 1 ) (x \bmod k + 1) (xmodk+1) are selected and player ( x   m o d   k + 1 ) (x \bmod k + 1) (xmodk+1) is removed from the game. Player x x x’s integer will then change from a x a_x ax to ( a x − a x   m o d   k + 1 ) (a_x - a_{x \bmod k + 1}) (axaxmodk+1). Player y y y in this turn will become player ( y − 1 ) (y - 1) (y1) in the next turn for all x < y ≤ k x < y \le k x<yk, though the integer they hold will not change.

Jean wants to know the maximum possible integer held by the last remaining player in the game by selecting the players in each round optimally.

输入格式

There are multiple test cases. The first line of the input contains one integer T T T indicating the number of test cases. For each test case:

The first line contains one integer n n n ( 1 ≤ n ≤ 1 0 6 1 \le n \le 10^6 1n106) indicating the initial number of players.

The next line contains n n n integers a i a_i ai ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 109ai109) where a i a_i ai is the integer held by player i i i at the beginning.

It is guaranteed that the sum of n n n of all test cases will not exceed 1 0 6 10^6 106.

输出格式

For each test case output one line containing one integer indicating the maximum possible integer.

样例 #1

样例输入 #1

5
4
1 -3 2 -4
11
91 66 73 71 32 83 72 79 84 33 93
12
91 66 73 71 32 83 72 79 84 33 33 93
13
91 66 73 71 32 83 72 79 84 33 33 33 93
1
0

样例输出 #1

10
713
746
779
0

提示

For the first sample test case follow the strategy shown below, where the underlined integers are the integers held by the players selected in each turn.

{ 1 ‾ , − 3 , 2 , − 4 ‾ } \{\underline{1}, -3, 2, \underline{-4}\} {1,3,2,4} (select x = 4 x = 4 x=4) → \to { − 3 , 2 , − 5 ‾ } \{-3, \underline{2, -5}\} {3,2,5} (select x = 2 x = 2 x=2) → \to { − 3 , 7 ‾ } \{\underline{-3, 7}\} {3,7} (select x = 2 x = 2 x=2) → \to { 10 } \{10\} {10}.

以上来自 以上来自 以上来自原神 洛谷 洛谷 洛谷

解题思路

我们先列出 3 种情况:只有负数,只有正数,正负数都有。

  • 只有负数:以 − 11 , − 4 , − 5 , − 45 −11,−4,−5,−45 11,4,5,45 为例,发现得数最大为: 57 57 57
  • 只有正数:以 11 , 4 , 5 , 45 11,4,5,45 11,4,5,45 为例,发现得数最大为: 57 57 57
  • 正负数都有:以 − 11 , 4 , − 5 , 45 −11,4,−5,45 11,4,5,45 为例,发现得数最大为: 65 65 65

我们发现,如果 n n n 个数中正负数都有,输出的值即为所有数的绝对值之和;如果 n n n 个数中只有正数或负数,那输出的数为所有数的绝对值之和,减去最小的绝对值的两倍。

说得简单一点,就是模拟。

AC Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int Maxn = 1e6 + 10;
int T, n, a[Maxn];
int minn = INT_MAX, sum1, sum2;
int ans;
inline void solve() {
	minn = INT_MAX, sum1 = sum2 = 0;
	ans = 0;
	cin >> n;
	if (n == 1) {
		cin >> a[0];
		cout << a[0] << endl;
		return;
	}
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	bool flag = 1;
	for (int i = 1; i <= n; i++) {
		if (a[i] > 0) {
			flag = 0;
			break;
		}
	}
	if (flag) {
		for (int i = 1; i <= n; i++) {
			a[i] = abs(a[i]);
		}
	}
	for (int i = 1; i <= n; i++) {
		minn = min(minn, a[i]);
		sum1 += a[i];
		sum2 += abs(a[i]);
	}
	if (minn < 0) {
		ans = sum2;
	} else {
		ans = sum1 - minn * 2;
	}
	cout << ans << endl;
}
inline void work() {
	cin >> T;
	while (T--) {
		solve();
	}
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	work();
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1391418.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

深度学习引领信息检索革新:从传统方法到神经网络信息检索的探索

目录 前言1 信息检索背景概述1.1 信息检索基本任务1.2 信息检索是NLU典型应用 2 信息检索任务定义2.1 检索阶段2.2 排序阶段2.3 关键技术和算法 3 评价指标3.1 MRR&#xff08;平均倒数排名&#xff09;3.2 MAP&#xff08;平均精度均值&#xff09;3.3 NDCG&#xff08;归一化…

Qt应用开发(安卓篇)——Linux下Qt15.5.2配置Android

目录 一、前言 二、Qt安装 三&#xff1a;JDK安装 四&#xff1a;安装SDK&#xff0c;NDK 五、其他事项 六、新建项目 一、前言 看网上教程&#xff0c;多数是windows环境下的&#xff0c;配置也很简单&#xff0c;想不到自己配置的时候却遇到很多问题&#xff0c;传了一…

如何保证新加入的依赖版本与当前项目的其他相关依赖版本兼容?或者如何确保依赖版本升级后适合当前项目?或者如何保证新引入的依赖版本适合当前项目?

如何保证新加入的依赖版本与当前项目的其他相关依赖版本兼容&#xff1f;或者如何确保依赖版本升级后适合当前项目&#xff1f;或者如何保证新引入的依赖版本适合当前项目&#xff1f; 如题&#xff0c;可通过maven仓库找出各个版本之间的对应关系举例 如题&#xff0c;可通过m…

哈希(包含闭散列和开散列实现)

STL提供了两种关联式容器——树型和哈希关联式容器&#xff0c;本章就是关于哈希关联式容器的介绍。 unordered_map unordered_map介绍 unordered_map是一种储存键值对(key,value)的关联式容器&#xff0c;能够通过key快速索引到其对应的value容器中&#xff0c;key值用于唯…

系统性学习vue-vuex

系统性学习vue-vuex 理解vuexvuex工作原理搭建vuex环境案例Vuex的开发者工具使用getters配置项mapState与mapGettersmapActions和mapMutationsvuex模块化namespace 理解vuex 概念&#xff1a; 专门在Vue中实现集中式状态&#xff08;数据&#xff09;管理的一个Vue插件&#xf…

黄金t+d与黄金期货交易的区别

在金融投资领域中&#xff0c;黄金是一种重要的避险工具和财富保值增值手段。对于投资者来说&#xff0c;了解并熟悉不同的黄金交易方式是至关重要的。其中&#xff0c;黄金TD和黄金期货交易是两种常见的黄金交易形式。那么&#xff0c;它们之间具体有哪些区别呢&#xff1f; 了…

WebGL中开发VR(虚拟现实)应用

WebGL&#xff08;Web Graphics Library&#xff09;是一种用于在浏览器中渲染交互式3D和2D图形的JavaScript API。要在WebGL中开发VR&#xff08;虚拟现实&#xff09;应用程序&#xff0c;您可以遵循以下一般步骤&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&a…

OpenJDK 和 OracleJDK 哪个jdk更好更稳定,正式项目用哪个呢?关注者

OpenJDK 和 OracleJDK&#xff1a;哪个JDK更好更稳定&#xff0c;正式项目应该使用哪个呢&#xff1f;我会从&#xff0c;从开源性质、更新和支持、功能差异等方面进行比较&#xff0c;如何选择&#xff0c;哪个jdk更好更稳定&#xff0c;正式项目用哪个呢&#xff0c;进行比较…

select子句简单查询

Oracle从入门到总裁:https://blog.csdn.net/weixin_67859959/article/details/135209645 目录 数据查询 起别名 连接 ​编辑 去重 ​编辑 另外补充几个不常用的命令 如果要进行查询,那么需要使用数据操纵语言&#xff08;Data Manipulation Language&#xff0c;DML&am…

yum仓库详解(命令+搭建)

目录 一、初步了解yum 1、yum简介 2、yum实现过程 二、yum配置文件及命令 1、 yum配置文件 1.1 主配置文件 1.2 仓库设置文件 1.3 日志文件 2、yum命令详解 三、搭建仓库的方法 1、搭建本地yum仓库 2、搭建阿里云仓库&#xff08;http方式外网环境&#xff09; 3、f…

搜索经典题——填充 9*9矩阵

题目&#xff1a;给定一个九行九列矩阵&#xff0c;填充矩阵元素&#xff0c;要求&#xff1a; 1、每一行每一列&#xff0c;每个小九宫格&#xff08;图片画粗的地方就是&#xff09;不能包含相同元素 2、每一行&#xff0c;每一列&#xff0c;每个小九宫格均会完整出现1-9的数…

pycharm学生认证免费使用专业版

进入pycharm官网Monthly and yearly plans with JetBrains Toolboxhttps://www.jetbrains.com/store/?fromMenu#discounts ​​​ 按照要求填写&#xff0c;但是如果遇到这个提示&#xff0c;恭喜你&#xff0c;你的学校获得了美国商务部认证。 ​ 遇到这个不要慌&#…

Spring Boot - Application Events 的发布顺序_ApplicationStartingEvent

文章目录 概述Code源码分析 概述 Spring Boot 的广播机制是基于观察者模式实现的&#xff0c;它允许在 Spring 应用程序中发布和监听事件。这种机制的主要目的是为了实现解耦&#xff0c;使得应用程序中的不同组件可以独立地改变和复用逻辑&#xff0c;而无需直接进行通信。 …

残差网络 ResNet

目录 1.1 ResNet 2.代码实现 1.1 ResNet 如上图函数的大小代表函数的复杂程度&#xff0c;星星代表最优解&#xff0c;可见加了更多层之后的预测比小模型的预测离真实最优解更远了&#xff0c; ResNet做的事情就是使得模型加深一定会使效果变好而不是变差。 2.代码实现 impo…

java版微信小程序商城 免 费 搭 建 java版直播商城平台规划及常见的营销模式有哪些?电商源码/小程序/三级分销

涉及平台 平台管理、商家端&#xff08;PC端、手机端&#xff09;、买家平台&#xff08;H5/公众号、小程序、APP端&#xff08;IOS/Android&#xff09;、微服务平台&#xff08;业务服务&#xff09; 2. 核心架构 Spring Cloud、Spring Boot、Mybatis、Redis …

用通俗易懂的方式讲解:使用 LlamaIndex 和 Eleasticsearch 进行大模型 RAG 检索增强生成

检索增强生成&#xff08;Retrieval-Augmented Generation&#xff0c;RAG&#xff09;是一种结合了检索&#xff08;Retrieval&#xff09;和生成&#xff08;Generation&#xff09;的技术&#xff0c;它有效地解决了大语言模型&#xff08;LLM&#xff09;的一些问题&#x…

车载音频EMI的产生及典型音频功放AW836XX的解决方案

之前针对 eCall的文章中有提到D类音频功放需要关注EMI问题&#xff08;点击文章回看《车载eCall系统音频应用解决方案》&#xff09;&#xff0c;在此展开此问题并寻求解决方案。 1. EMI定义与分类 电磁干扰&#xff08;Electromagnetic Interference&#xff0c;EMI&#xff…

企业为什么要选择软件定制开发?

引言&#xff1a;定制开发的兴起 在商业竞争日益激烈的今天&#xff0c;企业领导者们面临着一个重要的抉择&#xff1a;是选择通用软件解决方案&#xff0c;还是探寻更贴合企业需求的定制开发路径&#xff1f; 在企业决策软件解决方案时&#xff0c;通用软件和软件定制开发各…

IOS-高德地图连续定位-Swift

使用定位功能需要需要接入高德地图定位Api&#xff1a; pod AMapLocation配置Info 在info中新建一个名为Privacy - Location Temporary Usage Description Dictionary的字典&#xff0c;然后在这个字典下新建Privacy - Location When In Use Usage Description、Privacy - Lo…

应用案例 | Softing工业物联网连接解决方案助力汽车零部件供应商实现智能制造升级

随着业务的扩展和技术的进步&#xff0c;某国际先进汽车零部件供应商在其工业物联网的升级方案中使用了Softing的dataFEED OPC Suite——通过MQTT协议将现场控制器和数控系统的数据上传到其物联网云平台&#xff0c;从而实现了设备状态的远程监控&#xff0c;不仅能够提前发现设…