RainX_69

play game, BINARY SEARCH (IMPORTANT)

Jan 17th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | Source Code | 0 0
  1. https://www.lintcode.com/problem/1671/description?_from=cat
  2.  
  3. ---------------------------------------------------------------------------------------------------------------------------------------
  4.  
  5. N individuals are playing games, each game has a referee and N-1 civilian players. Given an array A, A[i] represents that the player i needs to be at least a civilian A[i] times, returning the minimum number of games played.
  6.  
  7. ∑Ai<=1e18
  8. 1 < n < 1000
  9.  
  10. Example 1:
  11. Input:A = [2, 2, 2, 2]
  12. Output : 3
  13. Explanation:
  14. A[0] = 2 means that player 0 needs to be at least 2 times civilian
  15. The first game: Player 0 serves as the referee, at this time A[0] = 0, A[1] = 1, A[2] = 1, A[3] =1
  16. Second game: Player 1 serves as the referee, at this time A[0] = 1, A[1] = 1, A[2] = 2, A[3] = 2
  17. The third game: Player 2 serves as the referee, at this time A[0] = 2, A[1] = 2, A[2] = 2, A[3] = 3
  18. At this point, each player has met the requirements, so you can play three games.
  19.  
  20. Example 2:
  21. Input:A = [84,53]
  22. Output : 137
  23. Explanation:
  24. The first game: Player 1 serves as the referee, at this time A[0] = 1, A[1] = 0
  25. .
  26. .
  27. .
  28. The 31st game: Player 1 serves as the referee, at this time A[0] = 31, A[1] = 0
  29. Thirty-second game: Player 0 serves as the referee, at this time A[1] = 31, A[1] = 1
  30. Thirty-third game: Player 1 serves as the referee, at this time A[1] = 32, A[1] = 1
  31. Thirty-fourth game: Player 0 serves as the referee, at this time A[1] = 32, A[1] = 2
  32. .
  33. .
  34. .
  35. The 137th game: Player 1 serves as the referee, at this time A[1] = 84, A[1] = 53
  36. At this point, each player has met the requirements, so you can play 137 games.
  37.  
  38. ---------------------------------------------------------------------------------------------------------------------------------------
  39.  
  40. class Solution {
  41. public:    
  42.     bool isOK(vector<int> &A, long long matches){
  43.         long long referee=0;
  44.         for(auto x: A){
  45.             referee+=(matches-x); // matches-x is the number of times this guy will referee
  46.         }
  47.         return referee>=matches;  // there must be aways sufficient amount of refree for matches
  48.     }
  49.  
  50.     long long playGames(vector<int> &a) {
  51.         long long low=*max_element(a.begin(),a.end());
  52.         long long high=accumulate(a.begin(),a.end(),0LL);
  53.         long long res=0;
  54.         while(low<=high){
  55.             long long mid=(low+high)/2;
  56.             if(isOK(a,mid)==true){
  57.                 high=mid-1;
  58.                 res=mid;
  59.             }
  60.             else{
  61.                 low=mid+1;
  62.             }
  63.         }
  64.         return res;
  65.     }
  66. };
Advertisement
Add Comment
Please, Sign In to add comment