Topcoder SRM147 Div1 Level 1 PeopleCircle

問題


http://community.topcoder.com/stat?c=problem_statement&pm=1225&rd=4540

解法


一旦すべての文字を'M'にしておく.
この状態で,問題文の順番通りに'M'を'F'にしていけばいい.
ただし,次に'F'にする場所を数えるときに,すでに'F'になっているところは飛ばす.
(すでに'F'になっている場所はそこまでの作業で消えるので)
ある場所を'F'にした次に'F'にする場所は,'F'の次の 'M'から,その'M'を含めてK個目の'M'なので,
K個目の'M'が次の'F'にする場所になる.
(最初の場所を合計人数-1にしておくと最初からK個数えればいいことになる)

例 Testcase 3

MMMMMMMMMM
123       
  |
MMFMMMMMMM
   123
     |
MMFMMFMMMM
      123
        |
MMFMMFMMFM
23       1
 |
MFFMMFMMFM
   12 3
      |
MFFMMFFMFM  ...答え

コード


#include <bits/stdc++.h>
#include <stdint.h>
#include <sys/time.h>

class PeopleCircle {
public:
  int n;
  char ans[64];
  int next(int i) {
    return (i + 1) % n;
  }
  int next_M(int i) {
    i = next(i);
    while( ans[i] != 'M' ) i = next(i);
    return i;
  }
  std::string order(int numMales, int numFemales, int K) {
    n = numMales + numFemales;

    for(int i = 0; i < n; ++i) ans[i] = 'M';

    int i = n - 1;
    for(int j = 0; j < numFemales; ++j) {
      for(int k = 0; k < K; ++k) {
        i = next_M(i);
      }
      ans[i] = 'F';
    }

    return std::string(ans, n);
  }
};