P9842 [ICPC2021 Nanjing R] Klee in Solitary Confinement 题解(SPJ!!!)

[ICPC2021 Nanjing R] Klee in Solitary Confinement

题面翻译

给定 n , k n,k n,k 和一个长为 n n n 的序列,你可以选择对区间 [ l , r ] [l, r] [l,r] 的数整体加上 k k k,也可以不加。最大化众数出现次数并输出。

题目描述

Since the traveler comes, People in Monstadt suddenly raise great interest in computer programming and algorithms, including Klee, the Spark Knight of the Knights of Favonius.

Being sent to solitary confinement by Jean again, Klee decides to spend time learning the famous Mo’s algorithm, which can compute with a time complexity of O ( n 1.5 ) \mathcal{O}(n^{1.5}) O(n1.5) for some range query problem without modifications.

To check whether Klee has truly mastered the algorithm (or in fact making another bombs secretly), Jean gives her a problem of an integer sequence a 1 , a 2 , ⋯ , a n a_1, a_2, \cdots, a_n a1,a2,,an along with some queries [ l i , r i ] [l_i, r_i] [li,ri] requiring her to find the mode number in the contiguous subsequence a l i , a l i + 1 , ⋯ , a r i a_{l_i}, a_{l_i + 1}, \cdots, a_{r_i} ali,ali+1,,ari. The mode number is the most common number (that is to say, the number which appears the maximum number of times) in the subsequence.

With the help of Mo’s algorithm, Klee solves that problem without effort, but another problem comes into her mind. Given an integer sequence a 1 , a 2 , ⋯ , a n a_1, a_2, \cdots, a_n a1,a2,,an of length n n n and an integer k k k, you can perform the following operation at most once: Choose two integers l l l and r r r such that 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn and add k k k to every a i a_i ai where l ≤ i ≤ r l \le i \le r lir. Note that it is OK not to perform this operation. Compute the maximum occurrence of the mode number of the whole sequence if you choose to perform (or not perform) the operation optimally.

输入格式

There is only one test case in each test file.

The first line of the input contains two integers n n n and k k k ( 1 ≤ n ≤ 1 0 6 1 \le n \le 10^6 1n106, − 1 0 6 ≤ k ≤ 1 0 6 -10^6 \le k \le 10^6 106k106) indicating the length of the sequence and the additive number.

The second line of the input contains n n n integers a 1 , a 2 , ⋯ , a n a_1, a_2, \cdots, a_n a1,a2,,an ( − 1 0 6 ≤ a i ≤ 1 0 6 -10^6 \le a_i \le 10^6 106ai106) indicating the original sequence.

输出格式

Output one line containing one integer indicating the maximum occurrence of the mode number of the whole sequence after performing (or not performing) the operation.

样例 #1

样例输入 #1

5 2
2 2 4 4 4

样例输出 #1

5

样例 #2

样例输入 #2

7 1
3 2 3 2 2 2 3

样例输出 #2

6

样例 #3

样例输入 #3

7 1
2 3 2 3 2 3 3

样例输出 #3

5

样例 #4

样例输入 #4

9 -100
-1 -2 1 2 -1 -2 1 -2 1

样例输出 #4

3

提示

For the first sample test case, choose l = 1 l = 1 l=1 and r = 2 r = 2 r=2 and we’ll result in the sequence { 4 , 4 , 4 , 4 , 4 } \{4, 4, 4, 4, 4\} {4,4,4,4,4}. The mode number is obviously 4 4 4 which appears 5 5 5 times.

For the second sample test case, choose l = 4 l = 4 l=4 and r = 6 r = 6 r=6 and we’ll result in the sequence { 3 , 2 , 3 , 3 , 3 , 3 , 3 } \{3, 2, 3, 3, 3, 3, 3\} {3,2,3,3,3,3,3}. The mode number is 3 3 3 which appears 6 6 6 times.

For the fourth sample test case, choose not to perform the operation. The mode number is 1 1 1 and − 2 -2 2 which both appear 3 3 3 times.

以上来自洛谷 以上来自洛谷 以上来自洛谷
看完题目知道我为什么说”原神启动“了吧。(什么,不知道?一看你就没看这篇题解。)

重点声明:我不玩原神。

解题思路

这一套 ICPC 的问题全是关于原神的诶,出题人什么了?

正片开始

我们枚举最后众数为 x x x,则每次只需要单独考虑 x x x x + k x+k x+k。我们事先可以将每个数按数值大小,将位置插入 vector,则可做到均摊 O ( n ) O(n) O(n)。如果使用 m a p map map 或者别的容器实现,则有运行超时的风险。

现在问题转化成有一个长度为 m m m 的序列,序列仅由 X X X Y Y Y 组成,用 X l , r X_{l,r} Xl,r Y l , r Y_{l,r} Yl,r​ 表示区间 [ l , r ] [l,r] [l,r] X X X Y Y Y 的个数,则我们需要选择一个区间 [ l , r ] [l,r] [l,r],使得 X 1 , l − 1 + Y l , r + X r + 1 , m X_{1,l−1}+Y_{l,r}+X_{r+1,m} X1,l1+Yl,r+Xr+1,m 最大。

简单转化一下,则对于每一个 r r r,我们需要最大化 X 1 , l − 1 + Y l , r + X r + 1 , m = X 1 , l − 1 + ( r − l + 1 ) − X l , r + X r + 1 , m X_{1,l−1}+Y_{l,r}+X_{r+1,m}=X_{1,l−1}+(r−l+1)−X_{l,r}+X_{r+1,m} X1,l1+Yl,r+Xr+1,m=X1,l1+(rl+1)Xl,r+Xr+1,m​。整理得到 ( 2 × X 1 , l − 1 − l ) + ( r + 1 − X 1 , r + X r + 1 , m ) (2\times X_{1,l−1}−l)+(r+1−X_{1,r}+X_{r+1,m}) (2×X1,l1l)+(r+1X1,r+Xr+1,m),即最大化 2 × X 1 , l − 1 − l 2\times X_{1,l−1}−l 2×X1,l1l,记录前缀最大值转移即可,时间复杂度 O ( m ) O(m) O(m)。综上,时间复杂度为 O ( n ) O(n) O(n)

然后,去写代码,复制到提交代码出,点击提交,就会 A C AC AC ?

AC Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int Maxn = 1e6 + 5;
int n, k, a[Maxn];
int tong[Maxn * 4][2], maxx, len;
vector<int> res[Maxn * 4];
int ans;
inline void work() {cin >> n >> k;for (int i = 1; i <= n; i++) {cin >> a[i];a[i] += 2e6;res[a[i]].push_back(a[i]);res[a[i] + k].push_back(a[i]);len = max(len, a[i] + k);maxx = max({maxx, (int)res[a[i]].size(), (int)res[a[i] + k].size()});}if (!k) {cout << maxx / 2 << endl;return;}int tmp;for (int i = 0; i <= len; i++) {if (res[i].size() == 0) {continue;}for (int j = 0; j < res[i].size(); j++) {tong[j + 1][0] = tong[j][0] + (res[i][j] == i);tong[j + 1][1] = tong[j][1] + (res[i][j] != i);}tmp = tong[1][0] - tong[1][1];for (int j = 1; j <= res[i].size(); j++) {tmp = max(tmp, tong[j - 1][0] - tong[j - 1][1]);ans = max(ans, tong[res[i].size()][0] + tong[j][1] - tong[j][0] + tmp);}}cout << ans << endl;
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);work();return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

制造领域 智能制造典型场景参考指引

智能制造场景是智能工厂的核心组成部分&#xff0c;是指面向制造过程各个环节&#xff0c;通过新一代信息技术、先进制造技术的深度融合&#xff0c;部署高档数控机床与工业机器人、增材制造装备、智能传感与控制装备、智能检测与装配装备、智能物流与仓储装备、行业成套装备等…

Windows 下 PYQT开发环境的搭建:

(1)环境搭建&#xff1a; PYQT 安装包&#xff1a; Anaconda&#xff1a; Anaconda3-2023.09-0-Windows-x86_64 pycharm&#xff1a; pycharm 2019.3 下载包&#xff1a; Anaconda:下载成功 |蟒蛇 (anaconda.com) pycharm: pycharm安装包_pycharm用copilotchat资源-CSD…

利用 ChatGPT 高效搜索:举一反三的思考方式,高效查找解决方案

文章目录 基础思路举一反三Go 语言 Web 框架延伸思考思考结论 本文只是我的一些尝试&#xff0c;基于 ChatGPT 实现系统化快速搜索某编程语言的特定领域相关包或者基于其他语言类推荐落地方案的尝试。 这篇文章中描述的方式不一定是好方式&#xff0c;但应该会有一定的启示作用…

【Go学习】macOS+IDEA运行golang项目,报command-line-arguments,undefined

写在前面的话&#xff1a;idea如何配置golang&#xff0c;自行百度 问题1&#xff1a;通过idea的terminal执行go test报错 ✘ xxxxxmacdeMacBook-Pro-3  /Volumes/mac/.../LearnGoWithTests/hello  go test go: go.mod file not found in current directory or any parent …

Spring Boot的创建和使用

Spring的诞生是为了简化Java程序开发的&#xff0c;而Spring Boot的诞生是为了简化Spring程序开发的。 目录 Spring Boot 的优点 Spring Boot 项目创建 Spring Boot 目录介绍 Spring Boot 运行 Spring Boot 的优点 快速集成框架&#xff0c;Spring Boot 提供了启动添加依赖…

数据库面试题(开发者必看)

数据库面试题(开发者必看) 数据库常见面试题(开发者篇) 什么是存储过程&#xff1f;有哪些优缺点&#xff1f; 什么是存储过程&#xff1f;有哪些优缺点&#xff1f; 存储过程就像我们编程语言中的函数一样&#xff0c;封装了我们的代码(PLSQL、T-SQL)。 存储过程的优点&…

【机器学习300问】12、为什么要进行特征归一化?

当线性回归模型的特征量变多之后&#xff0c;会出现不同的特征量&#xff0c;然而对于那些同是数值型的特征量为什么要做归一化处理呢&#xff1f; 一、为了消除数据特征之间的量纲影响 使得不同指标之间具有可比性。例如&#xff0c;分析一个人的身高和体重对健康的影响&…

【C++入门到精通】智能指针 auto_ptr、unique_ptr简介及C++模拟实现 [ C++入门 ]

阅读导航 引言一、std::auto_ptr1. 简介2. 使用示例3. C模拟实现 二、std::unique_ptr1. 简介2. 使用示例3. C模拟实现 温馨提示 引言 在 C 中&#xff0c;智能指针是一种非常重要的概念&#xff0c;它能够帮助我们自动管理动态分配的内存&#xff0c;避免出现内存泄漏等问题。…

YOLOv5改进 | 检测头篇 | 利用DynamicHead增加辅助检测头进行针对性检测(让小目标无所遁形)

一、本文介绍 本文给大家带来的改进机制是针对性的改进,针对于小目标检测增加P2层,利用DynamicHead(原版本一比一复现,全网独一份,不同于网上魔改版本)进行检测,其中我们增加P2层其拥有更高的分辨率,这使得模型能够更好地捕捉到小尺寸目标的细节。在这些的基础上配合Dyn…

使用@Slf4j后引入log,idea标红

引入Slf4j注解 idea标红Cannot resolve symbol ‘log’ 引入Lombok插件 如果在Marketplace查不到时&#xff0c;不妨关闭菜单再打开试下

Qt 状态机框架:The State Machine Framework (二)

传送门: Qt 状态机框架:The State Machine Framework (一) Qt 状态机框架:The State Machine Framework (二) 1、利用并行态避免态的组合爆炸 假设您想在单个状态机中对汽车的一组互斥属性进行建模。假设我们感兴趣的属性是干净与肮脏&#xff0c;以及移动与不移动。需要四个…

汇川PLC与工业远程透传模块远程上下载程序

准备工作 一台可联网操作的电脑一台单网口的远程透传网关及博达远程透传配置工具网线一条&#xff0c;用于实现网络连接和连接PLC一台汇川 H5U-1614MTD-A8 PLC及其编程软件一张4G卡或WIFI天线实现通讯(使用4G联网则插入4G SIM卡&#xff0c;WIFI联网则将WIFI天线插入USB口&…