Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency
题目链接:https://codeforces.com/contest/1082/problem/E
题意:
给出n个数以及一个c,现在可以对一个区间上的数同时加上或减去一个值,问最后c的最多数量为多少。
题解:
这题挺有意思的,我们通过分析题目可以发现,对于每一个d,如果把[l,r]里面的d都变为c,则最后c的数量为cnt(c,1,l-1)+cnt(c,r+1,n)+cnt(d,l,r)。
这个式子变化一下,有:cnt(c,1,n)+cnt(d,l,r)-cnt(c,l,r)。
现在就只需要维护cnt(d,l,r)-cnt(c,l,r)的最大值就可以了。
这里有许多种维护方式,我说下我用的。
假设a[x1]=a[x2]=...a[xn]=d,那么对于[x1+1,x2-1]...[xn+1,n]这些区间,是没有d的,则我们需要维护的值就变为了-cnt(c,l,r),这里就相当于把连续的区间缩成了一个点。
为什么能缩点?我理解的就是选择的区间是一段连续的区间,缩点后的每个点,只要把当前的点加上的和为正(此时把当前连续的区间选完)就可以选择这一个点,否则就直接以下一个点为起点(当前区间就一个不选,之前已经保存了一个最大值了)。
所以对于一段连续的没有d的区间,要么选择连续的一段区间,要么直接不选,所以缩点后,用最大连续子段和的技巧就可以了~
代码如下:
#include <bits/stdc++.h>
using namespace std; const int N = 1e6 ;
int a[N];
int cnt[N];
int n,c,ans=-;
vector <int> pos[N];
int calc(int x){
if(x==c) return ;
vector <int> vec;
int len = pos[x].size();
if(len<=) return ;
vec.push_back(-cnt[pos[x][]-]);
vec.push_back();
for(int i=;i<len;i++){
vec.push_back(-cnt[pos[x][i]-]+cnt[pos[x][i-]]);
vec.push_back();
if(i==len-) vec.push_back(-cnt[n]+cnt[pos[x][i]]);
}
int tot = ,sum = ,l = vec.size();
for(int i=;i<l;i++){
sum+=vec[i];
if(sum>=tot){
tot=sum;
}else if(sum<) sum=;
}
return max(tot,sum);
}
int main(){
scanf("%d%d",&n,&c);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
cnt[i]=cnt[i-]+(a[i]==c);
pos[a[i]].push_back(i);
}
for(int i=;i<=N-;i++){
ans=max(calc(i)+cnt[n],ans);
}
printf("%d",ans);
return ;
}
Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency的更多相关文章
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition
C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...
- Educational Codeforces Round 55 (Rated for Div. 2)E
题:https://codeforces.com/contest/1082/problem/E 题意:给出n个数和一个数c,只能操作一次将[L,R]之间的数+任意数,问最后该序列中能存在最多多少个c ...
- Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】
传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...
- Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D
http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y or x-& ...
- Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】
传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...
- Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))
C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))
A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Educational Codeforces Round 55 (Rated for Div. 2)
D. Maximum Diameter Graph 题意 给出每个点的最大度,构造直径尽可能长的树 思路 让度数大于$1$的点构成链,考虑是否能在链的两端加度为$1$的点 代码 #include &l ...
随机推荐
- Leecode刷题之旅-C语言/python-83删除排序链表中的重复元素
/* * @lc app=leetcode.cn id=83 lang=c * * [83] 删除排序链表中的重复元素 * * https://leetcode-cn.com/problems/rem ...
- (数据科学学习手札32)Python中re模块的详细介绍
一.简介 关于正则表达式,我在前一篇(数据科学学习手札31)中已经做了详细介绍,本篇将对Python中自带模块re的常用功能进行总结: re作为Python中专为正则表达式相关功能做出支持的模块,提供 ...
- GET TIME
基本形式 GET TIME [FIELD tim]. オプション: ... FIELD tim 機能 FIELD オプションを使用しない場合. 日付および時刻のシステム項目 sy-datlo.sy-d ...
- AOP原理??
面向切面编程,在我们的应用中,经常需要做一些事情,但是这些事情与核心业务无关,比如,事务.日志.权限拦截.统一异常处理等等.Spring AOP使用了代理模式. Spring AOP底层利用两种代理模 ...
- Spring 中的文件上传与下载控制
先创建根应用上下文配置,WebDemo/src/main/java/com/seliote/webdemo/config/RootContextConfig.java package com.seli ...
- jmeter操作JDBC
1. 依次添加计划.线程组.JDBC Connection Configuration.JDBC Request.HTTP请求.Debug Sampler.察看结果树 在计划中导入mysql的jdbc ...
- npm命令 VS yarn命令
npm yarn 说明 npm init yarn init 在项目中引导创建一个package.json文件 npm install yarn install/yarn 安装所有依赖包(依据pa ...
- 在阿里云上遇见更好的Oracle(二)
从上一篇文章的反馈来看,大家还是喜欢八卦多过技术细节,那这一篇继续一些题外话,说说我对“去IOE”的看法. 对同一件事情,参与的没参与的人,讨论起来,都会有各自的立场.所以这里先申明一下,以下内容只是 ...
- LeetCode - 67. Add Binary(4ms)
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- Visual Studio 2015安装包
点击下载