Tuesday, February 8, 2011

TopCoder SRM 147, DIV II, PeopleCircle

Problem Statement

There are numMales males and numFemales females arranged in a circle. Starting from a given point, you count clockwise and remove the K'th person from the circle (where K=1 is the person at the current point, K=2 is the next person in the clockwise direction, etc...). After removing that person, the next person in the clockwise direction becomes the new starting point. After repeating this procedure numFemales times, there are no females left in the circle.

Given numMales, numFemales and K, your task is to return what the initial arrangement of people in the circle must have been, starting from the starting point and in clockwise order.

For example, if there are 5 males and 3 females and you remove every second person, your return String will be "MFMFMFMM".


class PeopleCircle {

public:
 
  string order(int m, int f, int K) {
    int tot=m+f;
    int flag[tot];
    memset(flag,1,sizeof(flag));
    string human;
    rep(i,0,tot)human+='M';
    int now=0;
    rep(i,0,f){
      for(int j =0;j<K-1;){
if(now>=tot)now=0;
if(flag[now]){
 now++;
 while(!flag[now])now++;
 j++;
}
else while(!flag[now])now++;
      }
      flag[now]=0;
      while(!flag[now])now++;
    }
    repsz(i,human)if(!flag[i])human[i]='F';
    return human;
  }
};

No comments:

Post a Comment