Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*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.
- */
- #include <iostream>
- using namespace std;
- struct pos{
- int first,second;
- };
- int main() {
- int t=1;
- while(t--){
- int n;
- cin>>n;
- int vis[n+2]={};
- vis[0]=vis[n+1]=true;
- pos p;
- for(int fill=0; fill<n; fill++){
- int start=0; int end=1;
- p={-1,-1};
- while(end<=n){
- while(vis[end]==false) end++;
- if(p.first==-1)
- p={start,end};
- else if( end -start >= p.second - p.first )
- p={start,end};
- start=end;
- end++;
- }
- vis[(p.first + p.second)/2] = true;
- for(int i = 1; i <= n; i++) (vis[i]==true)?cout<<"X":cout<<"_";
- cout<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment