CodeForces 1082 E Increasing Frequency

题目传送门

题意:给你n个数和一个c, 现在有一个操作可以使得 [ l, r ]区间里的所有数都加上某一个值, 现在问你c最多可以是多少。

题解:

pre[i] 代表的是 [1,i] 中 c 的个数是多少。

suf[i] 代表的是 [i,n] 中 c 的个数是多少。

我们可以处理出这些信息。

然后我们从后往前处理信息。

现在给定一个b[x], b[x] 记录的是 [l, r] 中 x 的个数 + [r+1, n] 中 c的个数。

那么 每次出现一个新的 x, 现在可以转移到b[x]的状态是 max(b[x], suf[i+1]) + 1。

然后我们对答案求最大就好了。

扫描二维码关注公众号,回复: 4329719 查看本文章
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod =  (int)1e9+7;
const int N = 5e5 + 100;
int a[N];
int b[N];
int suf[N];
int pre[N];
int main(){
    int n, c;
    scanf("%d%d", &n, &c);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    int ans = 0;
    for(int i = 1; i <= n; ++i) pre[i] = pre[i-1] + (a[i] == c);
    for(int i = n; i >= 1; --i){
        b[a[i]] = max(b[a[i]], suf[i+1]) + 1;
        suf[i] = suf[i+1] +  (c == a[i]);
        ans = max(ans, pre[i-1]+b[a[i]]);
    }
    cout << ans << endl;
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/MingSD/p/10052773.html
今日推荐