ann8497

urinal problem samsung

Aug 17th, 2019
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. /*
  2. It is a well-researched fact that men in a restroom generally prefer to maximize their distance from already occupied stalls, by occupying the middle of the longest sequence of unoccupied places
  3.  
  4. 10
  5. 10 1
  6. 10 2
  7. 10 3
  8. 10 4
  9. 10 5
  10. 10 6
  11. 10 7
  12. 10 8
  13. 10 9
  14. 10 10
  15. output is the following
  16.  
  17. 0000100000
  18. 0000100100
  19. 0100100100
  20. 0100100110
  21. 0100110110
  22. 0110110110
  23. 0110110111
  24. 0110111111
  25. 0111111111
  26. 1111111111
  27.  
  28. VERIFIED AND TESTED
  29. */
  30.  
  31. #include<iostream>
  32. using namespace std;
  33.  
  34. void solve(int n, int k, int *a){
  35.    
  36.    for(int person= 1; person<=k; person++){
  37.  
  38.      int last = 0; int len = 0; int ans = 0;
  39.  
  40.      for(int j =0; j<n; j++){
  41.  
  42.          if(a[j] == 0)len++;
  43.          if(a[j] == 1)len = 0;
  44.          if(len>=ans){
  45.               last = j;
  46.               ans = len;
  47.            }
  48.      }
  49.  
  50.      int s = last-ans+1;
  51.      int mid = (s+last)/2;
  52.      a[mid] = 1;
  53.       //cout<<ans<<" "<<s<<" "<<last<<endl;
  54.  
  55.    }
  56. }
  57.  
  58. int main(){
  59.  
  60.   int t; cin>>t;
  61.   while(t--){
  62.     int n; int k;
  63.     cin>>n>>k;
  64.     int a[n];
  65.     for(int i = 0; i<n; i++)a[i] = 0;
  66.     solve(n,k,a);
  67.     for(int i = 0; i<n; i++)cout<<a[i];
  68.     cout<<endl;
  69.   }
  70.  
  71.   return 0;
  72. }
Add Comment
Please, Sign In to add comment