lelouche29

Urinal problem Samsung

Sep 7th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. /*Men's restroom problem : 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. For detailed version, check the following link.
  2. */
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. struct pos{
  8.     int first,second;
  9. };
  10.  
  11. int main() {
  12.     int t=1;
  13.     while(t--){
  14.         int n;
  15.         cin>>n;
  16.  
  17.         int vis[n+2]={};
  18.         vis[0]=vis[n+1]=true;
  19.  
  20.         pos p;
  21.  
  22.         for(int fill=0; fill<n; fill++){
  23.             int start=0; int end=1;
  24.  
  25.             p={-1,-1};
  26.  
  27.             while(end<=n){
  28.                 while(vis[end]==false) end++;
  29.  
  30.                 if(p.first==-1)
  31.                     p={start,end};
  32.                 else if( end -start >= p.second - p.first )
  33.                     p={start,end};
  34.  
  35.                     start=end;
  36.                     end++;
  37.             }
  38.  
  39.             vis[(p.first + p.second)/2] = true;
  40.             for(int i = 1; i <= n; i++) (vis[i]==true)?cout<<"X":cout<<"_";
  41.             cout<<endl;
  42.         }
  43.     }
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment