Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://www.lintcode.com/problem/1671/description?_from=cat
- ---------------------------------------------------------------------------------------------------------------------------------------
- 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.
- ∑Ai<=1e18
- 1 < n < 1000
- Example 1:
- Input:A = [2, 2, 2, 2]
- Output : 3
- Explanation:
- A[0] = 2 means that player 0 needs to be at least 2 times civilian
- The first game: Player 0 serves as the referee, at this time A[0] = 0, A[1] = 1, A[2] = 1, A[3] =1
- Second game: Player 1 serves as the referee, at this time A[0] = 1, A[1] = 1, A[2] = 2, A[3] = 2
- The third game: Player 2 serves as the referee, at this time A[0] = 2, A[1] = 2, A[2] = 2, A[3] = 3
- At this point, each player has met the requirements, so you can play three games.
- Example 2:
- Input:A = [84,53]
- Output : 137
- Explanation:
- The first game: Player 1 serves as the referee, at this time A[0] = 1, A[1] = 0
- .
- .
- .
- The 31st game: Player 1 serves as the referee, at this time A[0] = 31, A[1] = 0
- Thirty-second game: Player 0 serves as the referee, at this time A[1] = 31, A[1] = 1
- Thirty-third game: Player 1 serves as the referee, at this time A[1] = 32, A[1] = 1
- Thirty-fourth game: Player 0 serves as the referee, at this time A[1] = 32, A[1] = 2
- .
- .
- .
- The 137th game: Player 1 serves as the referee, at this time A[1] = 84, A[1] = 53
- At this point, each player has met the requirements, so you can play 137 games.
- ---------------------------------------------------------------------------------------------------------------------------------------
- class Solution {
- public:
- bool isOK(vector<int> &A, long long matches){
- long long referee=0;
- for(auto x: A){
- referee+=(matches-x); // matches-x is the number of times this guy will referee
- }
- return referee>=matches; // there must be aways sufficient amount of refree for matches
- }
- long long playGames(vector<int> &a) {
- long long low=*max_element(a.begin(),a.end());
- long long high=accumulate(a.begin(),a.end(),0LL);
- long long res=0;
- while(low<=high){
- long long mid=(low+high)/2;
- if(isOK(a,mid)==true){
- high=mid-1;
- res=mid;
- }
- else{
- low=mid+1;
- }
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment