传送门

解题思路

  贪心。对于一段区间中,可以将这段区间中相同的元素同时变成\(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. css实现文本溢出用...显示

    文本溢出省略号显示,要实现这个必须 要有四个条件: 1.须有容器宽度:width:value 2.强制文本在一行内显示:white-space:nowrap: 3.溢出内容隐藏:overflow:hi ...

  2. apue 第10章 信号signal

    每种信号都有名字,都是以SIG开头 信号机制最简单的接口是signal函数 #include <signal.h> typedef void (*sighandler_t)(int); s ...

  3. ubuntu配置px4编译环境

    一.主要参考的内容 px4的开发者手册 https://dev.px4.io/zh/setup/dev_env_linux.html 其中有的shell指令 权限设置 警告:永远不要使用sudo来修复 ...

  4. leetcode骚题目列表

    114,二叉树原地前序遍历转链表 令人不舒服的空间限制 4,O(logn)寻找两个数组的中位数 感觉诡异又很其妙的二分 279,判断一个数可拆成最少几个平方数的和 有O(n)解法,如果把sqrt视为O ...

  5. OC学习篇之---类目的概念和使用

    上一篇文章介绍了OC中的@class关键字的使用http://blog.csdn.net/jiangwei0910410003/article/details/41774747,这一篇我们介绍一下,O ...

  6. mysql负载高分析

    table_open_cache SHOW STATUS LIKE 'Open%tables'; SHOW global variables LIKE '%table%';    # 如果你发现 op ...

  7. Dart 和 Flutter 使用json_annotation和json_serializable来处理json数据教程

    在学习fultter的时候突然想到如何去处理从服务器获取的json或者将app中的对象数据转换成json上传给服务器 于是研究一下dart对json数据的处理 首先需要依赖下面的第三方库(这里要强调下 ...

  8. Qt获取文件路径、文件夹路径

    1.首先是选择文件 QString file_path = QFileDialog::getOpenFileName(this, "请选择文件路径...", "默认路径( ...

  9. HTML-参考手册: HTML ASCII

    ylbtech-HTML-参考手册: HTML ASCII 1.返回顶部 1. HTML ASCII 参考手册 ASCII 字符集被用于因特网上不同计算机间传输信息. ASCII 字符集 ASCII ...

  10. mac查看python安装路径

    1.terminal : input: which Python 或者 which Python3 2.terminal: input : python  --->import sys  --- ...