B. Kefa and Company

Topic links: http://codeforces.com/problemset/problem/580/B

 

Subject to the effect: n personal, tell your n personal wages, but the difference between the income of your guests can not exceed a given number. Everyone there is a weight, and now individuals selected from n m individual, making them the largest sum of the weights

 

Ideas: First, everyone sort of wages, then ruler emulated find a qualified us to calculate the range of weight for each person within this range. Then find the maximum weights

 

AC Code:

 1 #include <cstdio>
 2 #include <string>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cstdbool>
 6 #include <string.h>
 7 #include <math.h>
 8 
 9 using namespace std;
10 
11 typedef long long LL;
12 
13 typedef struct Node{
14     int value;
15     int friendship;
16 }Node;
17 
18 bool cmp (Node a,Node b)
19 {
20     return a.value < b.value;
21 }
22 
23 int main()
24 {
25     int n,d;
26     cin >> n >> d;
27     Node a[n];
28     for (int i=0;i<n;i++)
29     {
30         cin >> a[i].value >> a[i].friendship;
31     }
32     sort(a,a+n,cmp);
33     int l = 0,r = 0;
34     LL sum = 0,ans = 0;
35     while (l<n)
36     {
37         while (r < n && a[r].value - a[l].value < d)
38         {
39             sum += a[r].friendship;
40             r++;
41         }
42         ans = max(ans,sum);
43         sum -= a[l].friendship;
44         l++;
45     }
46     cout << ans << endl;
47     return 0;
48 }

 

Guess you like

Origin www.cnblogs.com/-Ackerman/p/11165897.html