cf 580B

B. Kefa and Company
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.

Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!

Input

The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.

Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type misi(0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.

Output

Print the maximum total friendship factir that can be reached.

Sample test(s)
input
4 5
75 5
0 100
150 20
75 1
output
100
input
5 100
0 7
11 32
99 10
46 8
87 54
output
111
Note

In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.

In the second sample test we can take all the friends.

//求出每个数开始到d内的值

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
const int maxn=100000+10;
const int inf=(1<<30);

struct Node
{
    __int64 num,val;
}p[maxn];

bool cmp(Node a,Node b)
{
    return a.num<b.num;
}
__int64 ans[maxn];
int main()
{
    int n,d;
    while(~scanf("%d%d",&n,&d))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%I64d%I64d",&p[i].num,&p[i].val);
        }
        memset(ans,0,sizeof(ans));
        sort(p,p+n,cmp);
        __int64 cnt=0;
        for(int i=0;i<n;i++)  
        {
            if(abs(p[i].num-p[cnt].num)<d)
                ans[cnt]+=p[i].val;
            else
            {
                cnt++;
                ans[cnt]=ans[cnt-1]-p[cnt-1].val;
                i--;
            }
        }
        sort(ans,ans+cnt+1);
        printf("%I64d\n",ans[cnt]);
    }

    return 0;
}


Codeforces Round 1490 Problem B 的题目名称为 "Balanced Remainders"。题目大意是给定一个长度为 $n$ 的数组 $a$,其中每个元素对 3 取余的结果为 0、1 或 2。目标是通过最少的操作次数使得这三个余数的数量相等。每次操作允许将某个元素的值增加 1,这会导致其对 3 取余的结果循环变化(例如,余数为 2 的元素加 1 后余数变为 0,依此类推)。 ### 解题思路 1. **统计余数分布**:首先统计余数为 0、1、2 的数量。 2. **模拟余数调整**:根据余数调整的规则,模拟如何通过最少的操作次数平衡余数分布。具体来说: - 余数 0 的元素加 1 后余数变为 1。 - 余数 1 的元素加 1 后余数变为 2。 - 余数 2 的元素加 1 后余数变为 0。 3. **循环调整**:通过循环调整余数分布,计算每一步调整所需的操作次数,并取最小值。 ### 代码示例 以下是一个实现该问题的 C++ 代码示例: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> cnt(3, 0); for (int x : a) { cnt[x % 3] += 1; } int target = n / 3; int ans = 0; // 循环调整余数分布 for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (cnt[j] > target) { int diff = cnt[j] - target; ans += diff * i; cnt[j] -= diff; cnt[(j + 1) % 3] += diff; break; } } } cout << ans << "\n"; } return 0; } ``` ### 相关问题 1. 如何解决 Codeforces Round 1490 Problem B 的变种问题,其中目标是平衡余数为 0、1、2 的数量? 2. 在 "Balanced Remainders" 问题中,如果允许减少元素值,如何调整解法? 3. 如何优化 Codeforces Round 1490 Problem B 的时间复杂度以处理更大的输入规模? 4. 如果余数的调整规则发生变化,例如余数 0 加 1 后变为 2,该如何调整解法? 5. 如何将 "Balanced Remainders" 问题扩展到其他模数(如 4 或 5)的情况?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值