Linkin_Park

"Kefa and Company" task from Tarasov's contest

Feb 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. /*
  2. B. Kefa and Company
  3. time limit per test 2 seconds
  4. memory limit per test 256 megabytes
  5. input: standard input
  6. output: standard output
  7. Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
  8.  
  9. 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!
  10.  
  11. Input
  12. 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.
  13.  
  14. 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 mi, si (0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.
  15.  
  16. Output
  17. Print the maximum total friendship factir that can be reached.
  18. */
  19.  
  20. #include <iostream>
  21. #include <cstdio>
  22. #include <vector>
  23. #include <algorithm>
  24. #include <set>
  25. #include <map>
  26. #include <iomanip>
  27. #include <cmath>
  28. #include <iterator>
  29.  
  30. using namespace std;
  31.  
  32. typedef long long int64;
  33.  
  34. struct keshasFriend
  35. {
  36.     int64 money;
  37.     int64 friendship;
  38. };
  39.  
  40. bool operator < (keshasFriend & a, keshasFriend & b)
  41. {
  42.     return a.money < b.money;
  43. }
  44.  
  45. int main()
  46. {
  47.     int64 friendsQty, minDifference;
  48.     cin >> friendsQty >> minDifference;
  49.  
  50.     vector <keshasFriend> friends(friendsQty);
  51.  
  52.     for (int i = 0; i < friendsQty; i++)
  53.     {
  54.         cin >> friends[i]. money >> friends[i].friendship;
  55.     }
  56.  
  57.     sort(friends.begin(), friends.end());
  58.    
  59.     int64 minMoney = friends[0].money;
  60.     int64 maxFriendship = friends[0].friendship;
  61.     int64 tempFriendship = friends[0].friendship;
  62.  
  63.     for (int l = 0, r = 1; r < friendsQty;)
  64.     {
  65.         // With the help of two pointers we don't
  66.         // have to go through array more than 1 time
  67.  
  68.         if (friends[r].money - minMoney < minDifference)
  69.         {
  70.             // If the right border element is good for us,
  71.             // we increase temporary sum, check if it's
  72.             // greater than our max sum and move right pointer
  73.  
  74.             tempFriendship += friends[r].friendship;
  75.             r++;
  76.  
  77.             if (tempFriendship > maxFriendship)
  78.             {
  79.                 maxFriendship = tempFriendship;
  80.             }
  81.         }
  82.         else
  83.         {
  84.             // If the right border element is not what we're
  85.             // looking for, then we move the left pointer,
  86.             // and therefore change the value of min money
  87.             // (we know for sure that left border element
  88.             // is the smallest because we sorted array
  89.             // that way) and decrease the value
  90.             // of temporary sum by element that
  91.             // we left behind (l - 1)
  92.             l++;
  93.             minMoney = friends[l].money;
  94.             tempFriendship -= friends[l - 1].friendship;
  95.         }
  96.     }
  97.  
  98.     cout << maxFriendship << endl;
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment