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.mzph.cn/news/629322.shtml

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

相关文章

MySQL命令大全和实例

文章目录 1. 数据库管理2. 表操作3. 数据操作&#xff08;CRUD&#xff09;4. 条件查询与排序5. 聚合函数和分组6. 用户权限管理7. 其他操作8. 视图操作9. 索引操作10. 子查询与连接查询11. 插入多行数据12. 删除满足特定条件的表中所有数据13. 清空表&#xff08;保留表结构&a…

rust跟我学五:是否安装双系统

图为RUST吉祥物 大家好,我是get_local_info作者带剑书生,这里用一篇文章讲解get_local_info是怎么得到检测双系统的。 首先,先要了解get_local_info是什么? get_local_info是一个获取linux系统信息的rust三方库,并提供一些常用功能,目前版本0.2.4。详细介绍地址:[我的Ru…

IOS-高德地图路径绘制显示交通状况-Swift

本文基于&#xff1a;高德地图路径绘制进行了路径绘制的优化&#xff0c;添加了根据交通信息&#xff08;是否拥堵&#xff09;来显示路况&#xff0c;效果如图&#xff1a; 图标资源&#xff1a; custtexture_bad custtexture_green custtexture_slow custtexture_serio…

关于C语言整型提升的讲解

目录 1.什么是整型提升 2.整型提升的意义 3.整型提升是怎么提升的 4.整型提升的实例 1.什么是整型提升 C语言中的整型算术运算总是以缺省&#xff08;默认&#xff09;整型类型的精度来进行的。为了获得这个精度&#xff0c;表达式中的字符和短整型操作数在使用之前会被转换…

Vue键盘按键别名/事件说明及案例

Vue中的按键别名&#xff08;&#xff09;&#xff1a; 回车 > enter 删除 > delete &#xff08;退格 、 删除 按键&#xff09; 退出 > esc 空格 > space 换行 > tab &#xff08;必须配合keydown去使用&#xff09; 上 > up 下 > down 左 > …

ChatGPT和文心一言哪个好用?

#ChatGPT 和文心一言哪个更好用&#xff1f;# 在当今信息爆炸的时代&#xff0c;人们对于文本生成和创作工具的需求越来越高。在这个背景下&#xff0c;ChatGPT和文心一言作为备受瞩目的工具&#xff0c;各自拥有独特的功能和用途。在本文中&#xff0c;我们将深入探讨这两个工…

stm32 - GPIO高级用法

stm32 - GPIO高级用法 PWMPWM / LEDPWM / 电机 PWM PWM / LED PWM波通过改变占空比可以改变LED的亮度 PWM信号调节LED亮度时&#xff0c;信号频率保持不变&#xff0c;即一个周期时间不变&#xff0c;改变的是脉冲的高电平的时间&#xff0c;即LED的导通时间&#xff0c;占空比…

web练习2

需求 1.计算用户指定的数值内的奇数和。例如用户输入的是10则计算13579的和 <!doctype html> <html lang"en"> <head><meta charset"utf-8"><title>作业1</title></head> <body> <script>//计算用…

AIGC商用实例—大模型技术助力AI测谎仪,实现视频通话实施测谎!

大家好&#xff0c;我是千寻哥&#xff0c;最近一段时间&#xff0c;给大家分享了不少的AI绘画相关的项目教程&#xff0c;很多星友都反映真的不错&#xff0c;我自己也是感觉很有意义&#xff01; 哈哈哈&#xff0c;今天我在看到了一个项目柑感觉是一个不错的idea&#xff0…

什么是池化层?

池化层&#xff08;Pooling Layer&#xff09;是卷积神经网络&#xff08;CNN&#xff09;中的一个重要组件&#xff0c;用于减少特征图&#xff08;feature maps&#xff09;的维度&#xff0c;同时保留重要的特征信息。以下是池化层的几个关键特点&#xff1a; 降低维度&…

为什么要用B+树

B树的优势 支持范围查询&#xff1a;B树在进行范围查询时&#xff0c;只需要从根节点一直遍历到叶子节点&#xff0c;因为数据都存储在叶子节点上&#xff0c;而且叶子节点之间有指针连接&#xff0c;可以很方便的进行范围查询 支持排序&#xff1a;B树的叶子节点按照关键字顺…

Mybatis配置两个数据源

1.配置两个数据源 # 数据源1配置 datasource1.urljdbc:oracle:thin://your_oracle_host1:your_oracle_port1/your_oracle_sid1 datasource1.usernameYourSchema1 datasource1.passwordyour_password1# 数据源2配置 datasource2.urljdbc:oracle:thin://your_oracle_host2:your_…

使用 Python 创造你自己的计算机游戏(游戏编程快速上手)第四版:第十五章到第十八章

十五、反转棋游戏 原文&#xff1a;inventwithpython.com/invent4thed/chapter15.html 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 在本章中&#xff0c;我们将制作反转棋&#xff0c;也称为黑白棋或奥赛罗。这个双人棋盘游戏是在网格上进行的&#xff0c;因此我们…

2024“华数杯”(A题)放射性 Tritium 污染问题国际大学生数学建模竞赛| 建模秘籍文章代码思路大全

铛铛&#xff01;小秘籍来咯&#xff01; 小秘籍希望大家都能轻松建模呀&#xff0c;华数杯也会持续给大家放送思路滴~ 抓紧小秘籍&#xff0c;我们出发吧~ 完整内容可以在文章末尾领取&#xff01; 问题重述&#xff1a; 2024 “Huashu Cup” 国际数学建模大赛 - Proble…

机器学习平台建设(一)

一、概述 下图是较简化的机器学习平台架构&#xff0c;概括了机器学习平台的主要功能和流程。本章会进行简要介绍&#xff0c;在功能章节再展开详述。机器学习最主要的三个步骤可概括为&#xff1a;数据处理、建模以及部署。 数据处理&#xff0c;即所有和数据相关的工作&…

一种基于YOLO改进的高效且轻量级的表面缺陷检测网络, NEU-DET和GC10-DET涨点明显

&#x1f4a1;&#x1f4a1;&#x1f4a1;本文摘要&#xff1a;一种基于YOLO改进的高效且轻量级的表面缺陷检测&#xff0c; 在NEU-DET和GC10-DET任务中涨点明显 目录 1.轻量且高效的YOLO 1.1 SCRB介绍 1.1.1 ScConv介绍 1.2 GSConvns 1.3 od_mobilenetv2_050 1.4 对应ya…

linux多进程基础(6):setitimer(间隔定时器)和signal(信号处理函数)

1.setitimer函数 setitimer函数用于设置一个间隔定时器,它会在指定的时间间隔到达时向进程发送一个信号。其定义如下: #include <sys/time.h> int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value); 该函数一共有三个参数,其…

什么是ChatGPT,什么是大模型prompt

什么是ChatGpt ChatGPT是一个由美国的OpenAI公司开发的聊天机器人&#xff0c;它使用了大型语言模型&#xff0c;现在有GPT-3、GPT-3.5、GPT-4.0多个版本&#xff0c;目前还在快速发展&#xff0c;通过监督学习和强化学习进行了微调。它可以根据用户的提示执行各种语言任务&am…

【SimPy系列博客之官方example学习与解读】—— Example 3: Car Wash

Hello&#xff0c;CSDN的各位小伙伴们&#xff0c;又见面啦&#xff01;今天我们要学习的例程是&#xff1a;Car Wash&#xff01;我们开始吧&#xff01; 例程背景 这个例程相对于example 2来说会简单一些&#xff0c;有一个洗车厂&#xff0c;里面有若干台洗车机器&#xf…

【Python】P3 循环语句

循环语句 Python 中的通过 for、while 建立循环。 for 循环&#xff1a; 每次循环时&#xff0c;变量会被赋值为迭代对象中的下一个元素&#xff0c;然后执行缩进的代码块。 for i in range(5): # i为变量&#xff0c;range(5)为迭代对象print(i)while循环&#xff1a; 如…