topcoder SRM 147

首先贴题目如下:

http://topcoder.bgcoder.com/print.php?id=204

TopCoder problem "PeopleCircle" used in SRM 147 (Division I Level One , Division II Level Two)



Problem Statement

 

There are numMales males and numFemales females arranged in a circle. Starting from a given point, you count clockwise and remove theK'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 procedurenumFemales 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".

 

Definition

 
Class:PeopleCircle
Method:order
Parameters:int, int, int
Returns:String
Method signature:String order(int numMales, int numFemales, int K)
(be sure your method is public)
 
 
 

Constraints

-numMales is between 0 and 25 inclusive
-numFemales is between 0 and 25 inclusive
-K is between 1 and 1000 inclusive
 

Examples

0) 
 
5
3
2
Returns: "MFMFMFMM"
Return "MFMFMFMM". On the first round you remove the second person - "M_MFMFMM". Your new circle looks like "MFMFMMM" from your new starting point. Then you remove the second person again etc.
1) 
 
7
3
1
Returns: "FFFMMMMMMM"
Starting from the starting point you remove the first person, then you continue and remove the next first person etc. Clearly, all the females are located at the beginning. Hence return "FFFMMMMMMM"
2) 
 
25
25
1000
Returns: "MMMMMFFFFFFMFMFMMMFFMFFFFFFFFFMMMMMMMFFMFMMMFMFMMF"
 
3) 
 
5
5
3
Returns: "MFFMMFFMFM"
Here we mark the removed people with '_', and the starting position with lower-case:
Number of      | People Remaining
Rounds         | (in initial order)
---------------+-----------------
 0             | mFFMMFFMFM
 1             | MF_mMFFMFM
 2             | MF_MM_fMFM
 3             | MF_MM_FM_m
 4             | M__mM_FM_M
 5             | M__MM__m_M
4) 
 
1
0
245
Returns: "M"
 


算法本身不难,本文提供2个。

  • 基于list的算法:
顺便作为list的练习,呵呵

算法挺容易想到的,就是构造list,模拟往后数的动作,往前回挪指针,然后list::insert进去,之后,把最后的位置设为初始位置,边界条件需要注意。另外的trick是换算reverse_iterator与iterator。

贴代码如下:

#include <iostream>
#include <list>
#include <string>
using namespace std;

void MoveStep(list<char>::iterator* it, int move_step, list<char>* l) {
  for (int i = 0; i < move_step; ++i) {
    if ((*it) == l->end()) {
      *it = l->begin();
    }
    ++(*it);
  }
}

string ListToString(const list<char>& l) {
  int idx = 0;
  string str(l.size(), '_');
  for (list<char>::const_iterator it = l.begin();
       it != l.end(); ++it) {
    str[idx++] = *it;
  }
  return str;
}

string Revert(int num_males, int num_females, int step) {
  list<char> l(num_males, 'M');
  list<char>::iterator add_it = l.begin();
  for (int i = 0; i < num_females; ++i) {
    // list::insert can't use reverse_iterator,                                                                                                             
    // and the inserted iterator will be before add_it.                                                                                                     
    // Since we can't use reverse_iterator, we ought to calculate                                                                                           
    // the steps to move after instead of the ones move back.                                                                                               
    l.insert(add_it, 'F');
    int size = l.size();
    int move_step = (size - (step % size));
    MoveStep(&add_it, move_step, &l);
  }

  cout << "before adjust:\t" << ListToString(l) << endl;

  // to set add_it as front.                                                                                                                                
  for (list<char>::iterator it = l.begin(); it != add_it;) {
    l.push_back(*it);
    ++it;
    // Note: pop_front() should be after iterator moves.                                                                                                    
    l.pop_front();
  }

  return ListToString(l);
}

int main() {
  cout << "5, 3, 2" << endl;
  cout << Revert(5, 3, 2) << endl;
  cout << "7, 3, 1" << endl;
  cout << Revert(7, 3, 1) << endl;
  cout << "25, 25, 1000" << endl;
  cout << Revert(25, 25, 1000) << endl;
  cout << "5, 5, 3" << endl;
  cout << Revert(5, 5, 3) << endl;
  cout << "1, 0, 245" << endl;
  cout << Revert(1, 0, 245) << endl;
}

  • 直接基于string的算法:

初始都是M的string,每次数K格,从0开始数,如果是M就+1,F就跳过,数够个数后将当前位置设成F,以此类推。通过mod的关系,可以做个优化。trick在于:move_step与count的定义与计算方式不同,见code注释

#include <iostream>
#include <string>

using namespace std;

string PeopleCircle(int num_males, int num_females, int step) {
  int len = num_males + num_females;
  string result(len, 'M');
  int index = 0;
  for (int i = 0; i < num_females; ++i) {
    int count = 0;
    // Including the 0th element, the actually move_step is step-1.                                                                                         
    int move_step = (step - 1) % (len - i);
    while (true) {
      if (result[index] != 'F') {
        ++count;
        // Including the 0th element, the count should reach original step mode.                                                                            
        if (count == move_step+1) {
          break;
        }
      }
      index = (index + 1) % len;
    }
    result[index] = 'F';
    index = (index + 1) % len;
  }
  return result;
}

int main() {
  cout << "5, 3, 2" << endl;
  cout << PeopleCircle(5, 3, 2) << endl;
  cout << "7, 3, 1" << endl;
  cout << PeopleCircle(7, 3, 1) << endl;
  cout << "25, 25, 1000" << endl;
  cout << PeopleCircle(25, 25, 1000) << endl;
  cout << "5, 5, 3" << endl;
  cout << PeopleCircle(5, 5, 3) << endl;
  cout << "1, 0, 245" << endl;
  cout << PeopleCircle(1, 0, 245) << endl;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值