D. Boboniu Chats with Du

传送门

分析

我太菜了,菜到这道题都没写出来

这道题的意思是给你一组序列,如果其中某一个数字大于给定的数字m,那么后面d个数字就会变成0,给你n个数字进行组合,要求最后这组序列的和最大

首先为了保证归零的数字最少,那么我们可以将一个大于m的数字放在数列最后面,然后枚举一个有效的大于m的数字个数,然后计算有效的小于m的数字的个数,维护两个前缀和即可

代码

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>

using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll a[N],b[N],ans;
ll x,y;
int n,d,m;

int main(){
    scanf("%d%d%d",&n,&d,&m);
    for(int i = 0;i < n;i++){
        ll z;
        scanf("%lld",&z);
        if(z > m) a[++x] = z;
        else b[++y] = z;
    }
    sort(a + 1,a + x + 1),reverse(a + 1,a + x + 1);
    sort(b + 1,b + y + 1),reverse(b + 1,b + y + 1);
    for(int i = 1;i <= x;i++) a[i] += a[i - 1];
    for(int i = 1;i <= y;i++) b[i] += b[i - 1];
    for(int i = 0;i <= x;i++){
        if(n - i < 1ll * (i - 1) * d) continue;
        ll tmp = 0;
        ll res = min(1ll * y,1ll * x - i + y - 1ll * (i - 1) * d);
        tmp += b[res];
        tmp += a[i];
        ans = max(ans,tmp);
        
    }
    printf("%lld",ans);
}

猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/108003334
du
今日推荐