传送门

解题思路

  贪心。对于一段区间中,可以将这段区间中相同的元素同时变成\(c\),但要付出的代价是区间中等于\(c\)的数的个数,设\(sum[i]\)表示等于\(c\)数字的前缀和,Max[i]表示数字\(i\)的最大个数。那么只要\(O(n)\)的扫一遍,维护一下每个数字的\(max\),具体做法是看一下\(Max[a[i]]\)大还是\(sum[i]\)大,如果\(sum\)大的话,说明前面都不变,直接把\(Max\)赋值成\(sum[i]+1\),否则直接让\(Max[i]++\),然后每次跟后面的\(sum\)并到一起更新一次答案。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector> using namespace std;
const int MAXN = 500005; inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
} int n,c,a[MAXN],sum[MAXN],Max[MAXN],ans,pre[MAXN]; inline int max(int x,int y){
return x>y?x:y;
} int main(){
n=rd(),c=rd();int now;
for(int i=1;i<=n;i++) {
sum[i]=sum[i-1];a[i]=rd();
if(a[i]==c) sum[i]++;
}
for(int i=1;i<=n;i++){
if(a[i]==c) continue;
now=sum[i];
if(now>=Max[a[i]]) Max[a[i]]=now+1;
else Max[a[i]]++;
ans=max(ans,Max[a[i]]+sum[n]-sum[i]);pre[a[i]]=i;
}
printf("%d\n",max(ans,sum[n]));
return 0;
}

CF 1082E Increasing Frequency(贪心)的更多相关文章

  1. Codeforces.1082E.Increasing Frequency(思路)

    题目链接 \(Description\) 给定\(n\)个数.你可以选择一段区间将它们都加上或减去任意一个数.求最终序列中最多能有多少个数等于给定的\(C\). \(n\leq5\times10^5\ ...

  2. Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency

    E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...

  3. CF1082E:E.increasing Frequency(贪心&最大连续和)

    You are given array a a of length n n . You can choose one segment [l,r] [l,r] (1≤l≤r≤n 1≤l≤r≤n ) an ...

  4. CF1082E Increasing Frequency (multiset+乱搞+贪心)

    题目大意: \(给你n个数a_i,给定一个m,你可以选择一个区间[l,r],让他们区间加一个任意数,然后询问一次操作完之后,最多能得到多少个m\) QWQ 考场上真的** 想了好久都不会,直到考试快结 ...

  5. CF Anya and Ghosts (贪心)

    Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. Codeforces Beta Round #11 A. Increasing Sequence 贪心

    A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...

  7. CF 115B Lawnmower(贪心)

    题目链接: 传送门 Lawnmower time limit per test:2 second     memory limit per test:256 megabytes Description ...

  8. CF Soldier and Badges (贪心)

    Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  9. cf 853 A planning [贪心]

    题面: 传送门 思路: 一眼看得,这是贪心[雾] 实际上,我们要求的答案就是sigma(ci*(ti-i))(i=1~n),这其中sigma(ci*i)是确定的 那么我们就要最小化sigma(ci*t ...

随机推荐

  1. subsequence 1

    题目链接 题意:给你两个字符串都是数字,让你求第一个字符串的子序列中大于第二个字符串的个数. 思路:dp[i][j] 表示 str1的前i个,匹配 str2的前 j 个的种类数,那么 if(s[i] ...

  2. [CSP-S模拟测试]:简单的填数(贪心+模拟)

    题目描述 对于一个长度为$n$,且下标从$1$开始编号的序列$a$,我们定义它是「合法的」,当且仅当它满足以下条件:·$a_1=1$·对于$i\in [1,n),a_i\leqslant a_{i+1 ...

  3. 筆記本 wifi走外网线 網卡走內網

    筆記本 wifi走外网线  網卡走內網 ,案列 -------------------------------------------------------- route print        ...

  4. context和getApplicationContext()的区别

    在android中常常会遇到与context有关的内容 浅论一下 context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder ...

  5. PHP基础知识总结(三) 流程控制、函数、类对象和数据库

    PHP基础语法 1.流程控制 条件语句:if  elseif  else / switch if($a == 1){ …… } elseif ($a == 2){ …… } else{ …… } 循环 ...

  6. struts2 paramsPrepareParamsStack拦截器简化代码(源码分析)

    目录 一.在讲 paramsPrepareParamsStack 之前,先看一个增删改查的例子. 1. Dao.java准备数据和提供增删改查 2. Employee.java 为model 3. E ...

  7. MySQL if,ifnull,nullif 函数

    一.IF IF(exPR1,expr2,expr3) 如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr ...

  8. 图片模式CMYK和RGB在浏览器下的变化

    手机浏览器里面不能显示出模式为CMYK的图片, 模式为CMYK的图片在IE浏览器里面显示和模式为RGB的一样 模式为CMYK的图片在火狐里面显示有色差

  9. 28. Python编写自动化测试用例

    接口文档已经提供了,requests库.unittest单元测试框架也已经介绍过,笔者相信读者朋友已经可以独立编写接口自动化测试用例了.但是有一些细节,我们需要聊一下.比如我们写登录接口测试用例,用户 ...

  10. JS检查断网

    window.addEventListener('load', function() { function updateOnlineStatus(event) { var condition = na ...