尺取法 Codeforces580B Kefa and Company

传送门:点击打开链接

题意:n个人,告诉你n个人的工资,每个人还有一个权值,现在从这n个人中选出m个人,使得他们的权值之和最大

思路:先按照工资从小到大排序,那么每次最优的必然是一个子区间中的值。所以我们能确定,所有的答案此时都是子区间,。所以利用尺取法O(n)求出答案

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck printf("fuck")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;

const int MX = 1e5 + 5;

struct Data {
    int a, b;
    bool operator<(const Data &P)const {
        return a < P.a;
    }
} A[MX];

int main() {
    int n, d; //FIN;
    while(~scanf("%d%d", &n, &d)) {
        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &A[i].a, &A[i].b);
        }
        sort(A + 1, A + 1 + n);

        int L = 1, R = 0;
        LL ans = 0, sum = 0;
        while(L <=  n) {
            while(R + 1 <= n && A[R + 1].a - A[L].a < d) sum += A[++R].b;
            ans = max(ans, sum);
            sum -= A[L++].b;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值