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.mfbz.cn/a/328778.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Python密码本连接wifi

有时候我们会忘记自己的Wi-Fi密码&#xff0c;或者需要连接某个Wi-Fi网络以满足合法需求。本文将介绍如何使用Python编程语言编写一个简单的连接Wi-Fi的程序。 一、密码本准备 在进行wifi猜测时&#xff0c;其实就是列出各种可能的密码&#xff0c;用来尝试去访问目标wifi&…

学习k8s的应用(三)

一、k8s部署ngnix 1、一些查看命令 1-1、所有命令空间 kubectl get pod --all-namespaces kubectl get svc --all-namespaces1-2、指定命令空间 kubectl get pod -n yabin kubectl get svc -n yabin2、单节点集群兼容 # 因为目前只有一个master节点&#xff0c;默认安装后…

Mac python爬虫学习

首先推荐几个 必须要掌握的类库 Requests: HTTP for Humans 它是以这么一句话介绍自己的&#xff0c;为人类使用的HTTP库 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html 中文文档 Beautifulsoup 用Beautiful Soup解析网站源代码 代替正则 https://…

【计算机网络】第七,八,九章摘要重点

第七章网络管理 1.计算机网络面临的两大威胁&#xff1f; 恶意程序有&#xff1a;计算机病毒&#xff0c;计算机蠕虫&#xff0c;特洛伊木马&#xff0c;逻辑炸弹&#xff0c;后门入侵和流氓软件。 2.安全的计算机网络四个目标&#xff1a; 机密性&#xff0c;端点鉴别&…

File.mkdir与File.mkdirs区别String.replace方法返回值

1、File.mkdir与File.mkdirs区别 File fnew File("C:\\a\\b"); mkdir 只创建最后一级目录 f.mkdir();只会创建b 若没有a 创建失败 mkdirs如上所述 创建a,b 当不确定目录是否存在时&#xff0c;最好用mkdirs 判断文件是否存在 文件夹是否存在 2、String.replace…

保送阿里云的云原生学习路线

近期好多人都有咨询学习云原生有什么资料。与其说提供资料不如先说一说应该如何学习云原生。 Linux基础知识&#xff1a;云原生技术通常在Linux环境中运行&#xff0c;因此建议首先掌握Linux的基础知识&#xff0c;包括命令行操作、文件系统、权限管理等。 容器化技术&#x…

Vue-24、Vue过滤器

1、效果 2、过滤器实现 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>过滤器</title><script type"text/javascript" src"https://cdn.jsdelivr.net/npm/vue2/dist/vue.…

二叉树的遍历 Java

二叉树的遍历 递归法前序遍历中序遍历后序遍历改进 迭代法前序、后序遍历中序遍历 二叉树的统一迭代法(未完成)Java 中 null、NULL、nullptr 区别 public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) { this.val val; }TreeNode(in…

大数据技术原理与应用期末复习(林子雨)

大数据技术原理与应用期末复习&#xff08;林子雨&#xff09; Hadoop的特性HBase编程实践NoSQL的四大类型键值数据库优点&#xff1a;缺点&#xff1a; 列族数据库优点&#xff1a;缺点&#xff1a; 文档数据库优点&#xff1a;缺点&#xff1a; 图数据库优点&#xff1a;缺点…

模拟瑞幸小程序购物车

是根据渡一袁老师的大师课写的&#xff0c;如有什么地方存在问题&#xff0c;还请大家指出来哟ど⁰̷̴͈꒨⁰̷̴͈う♡&#xff5e; index.html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta http-e…

新增PostgreSQL数据库管理功能,1Panel开源面板v1.9.3发布

2024年1月15日&#xff0c;现代化、开源的Linux服务器运维管理面板1Panel正式发布v1.9.3版本。 在这一版本中&#xff0c;1Panel新增了PostgreSQL数据库管理功能&#xff0c;并且支持设置PHP运行环境扩展模版。此外&#xff0c;我们进行了30多项功能更新和问题修复。1Panel应用…

如何应对Android面试官->RecyclerView回收复用LayoutManager,实战探探划一下

前言 上章我们讲了右半部分&#xff0c;本章我们讲解左半部分&#xff1b; 如何复用原理 我们在滑动的时候&#xff0c;才会触发 RecyclerView 的回收复用&#xff0c;所以我们从 RecyclerView 的 onTouchEvent 方法入手&#xff1b;我们来看下滑动的时候&#xff0c;是怎么…

SQL实践:利用tag检索文件的多种情况讨论(二)

在上一篇文章SQL实践&#xff1a;利用tag检索文件的多种情况讨论中&#xff0c;我们介绍了在使用外键的方式为数据关联tag后&#xff0c;如何筛选&#xff1a; 如何筛选包含某一个tag的数据如何筛选包含且只包含某一个tag的数据如何筛选包含多个指定tag的数据 这篇文章主要是…

LiveGBS流媒体平台GB/T28181功能-基础配置接入控制白名单黑名单配置控制设备安全接入设备单独配置接入密码

LiveGBS基础配置接入控制白名单黑名单配置控制设备安全接入设备单独配置接入密码 1、白名单配置应用场景2、接入控制2.1、白名单2.2、黑名单 3、搭建GB28181视频直播平台 1、白名单配置应用场景 LiveGBS国标流媒体服务&#xff0c;支持白名单配置。 可在设备注册前&#xff0…

机器学习_梯度下降

文章目录 什么是梯度梯度下降梯度下降有什么用 什么是梯度 计算梯度向量其几何意义&#xff0c;就是函数变化的方向&#xff0c;而且是变化最快的方向。对于函数f(x)&#xff0c;在点(xo,yo)&#xff0c;梯度向量的方向也就是y值增加最快的方向。也就是说&#xff0c;沿着梯度…

使用 Elasticsearch 和 LlamaIndex 进行高级文本检索:句子窗口检索

2023 年是检索增强生成 (RAG) 的一年&#xff0c;人们探索了许多用例&#xff0c;并使用该技术开发了数百种产品。 从 Q/A 聊天机器人到基于上下文的代理&#xff0c;RAG 的使用一直是 LLM 申请快速增长的主要因素。 支持不断发展的社区以及 Langchain 和 LlamaIndex 等强大框架…

Controller层自定义注解拦截request请求校验

一、背景 笔者工作中遇到一个需求&#xff0c;需要开发一个注解&#xff0c;放在controller层的类或者方法上&#xff0c;用以校验请求参数中(不管是url还是body体内&#xff0c;都要检查&#xff0c;有token参数&#xff0c;且符合校验规则就放行)是否传了一个token的参数&am…

Java工具类汇总

&#x1f497;wei_shuo的个人主页 &#x1f4ab;wei_shuo的学习社区 &#x1f310;Hello World &#xff01; ExcelUtils public class ExcelUtils {/*** 注入的具有排序功能的handle*/private static final SortRowWriteHandler SORT_ROW_WRITE_HANDLER new SortRowWriteHan…

linux 网络文件共享服务

存储类型 DAS 直连式存储 SAN 存储区域网络 NAS 网络附近存储 FTP文件传输协议 文件传输协议 FTP 早期的三个应用级协议之一&#xff0c;基于c/s架构 数据传输格式&#xff1a;二进制&#xff08;默认&#xff09;和文本 tcp 21端口&#xff08;权限&#xff0c;…

centos7配置时间同步网络时间

centos7配置时间同步网络时间 1、安装 NTP 工具。 sudo yum install -y ntp2启动 NTP 服务。 sudo systemctl start ntpd3、将 NTP 服务设置为开机自启动。 sudo systemctl enable ntpd4、验证 date
最新文章