传送门

解题思路

  贪心。对于一段区间中,可以将这段区间中相同的元素同时变成\(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. taomcat中catalina.out文件

    项目中发现在linux环境下布署的tomcat所占用的磁盘空间越来越大,是catalina.out 文件,每天几乎是2个G,发现可能会影响到tomcat服务(没确定)正常访问.文件这么大,其实已经无法 ...

  2. linux中软连接和硬链接的区别

    linux中创建软连接和硬链接的方法: 软连接: ln -s oldfile slink 硬连接: ln oldfile hlink linux中创建软连接和硬链接的区别:        原理上,硬链 ...

  3. Webx.0-Web3.0:Web3.0

    ylbtech-Webx.0-Web3.0:Web3.0 Web3.0只是由业内人员制造出来的概念词语,最常见的解释是,网站内的信息可以直接和其他网站相关信息进行交互,能通过第三方信息平台同时对多家网 ...

  4. quartz的初步总结及配置优化

    1.scheduler 1. Scheduler就是Quartz的大脑,所有任务都是由它来设施.Scheduler包含一个两个重要组件: JobStore和ThreadPool.JobStore是会来 ...

  5. 用processing生成屏保程序

    想法 利用随机数控制圆圈的大小.位置以及颜色,可以产生随机的美感. 让小球动起来,并且在屏幕边界处产生反弹效果. 代码 1: float circle_x = (float) 0.0; 2: floa ...

  6. centos为用户添加sudo功能

    su chmod a+w /etc/sudoers vim /etc/sudoers [找到root ALL=(ALL) ALL这行] 复制出新的一行,并且将root改为daniel(当前用户名) c ...

  7. 2015 ACM-ICPC 亚洲区上海站 A - An Easy Physics Problem (计算几何)

    题目链接:HDU 5572 Problem Description On an infinite smooth table, there's a big round fixed cylinder an ...

  8. 2018 ICPC Asia Singapore Regional A. Largest Triangle (计算几何)

    题目链接:Kattis - largesttriangle Description Given \(N\) points on a \(2\)-dimensional space, determine ...

  9. Reactor 反应堆设计模式

    为了应对高并发的服务器端开发,微软在2009年提出了一种更优雅地实现异步编程的方式Reactive Programming即反应式编程.随后其他技术紧随其后,比如ES6通过引入类似的异步编程方式等. ...

  10. 泛微oa系统com.eweaver.base.DataAction文件sql参数sql注入

    URL/ServiceAction/com.eweaver.base.DataAction?sql=select%201,2,3,4,5,6,7,8,9,233%20from%20DUAL%20