SRM 586 DIV2 250

Problem Statement

 The boys in the yard are going to play soccer. There are N+2 boys, two of which have been chosen as captains. Now they want to divide the other N children into two teams. For convenience, we number the boys who are not captains from 1 to N.

The division into teams works as follows. Initially, the first captain chooses one person for his team. Then, the second captain chooses one boy from those who are left. Then the first captain chooses again, and so on. The process continues until there are no more boys left.

You are given vector <int>s preference1 and preference2, each containing N elements. preference1[0] is the number of the boy whom the first captain regards to be the best player, preference1[1] is the next best player according to the first captain, and so on. preference2 describes the second captain's assessment in the same fashion. The captains pick the players greedily, i.e., each of them always chooses the boy whom he considers to be the strongest between the children not yet chosen.

Return a string consisting of N characters. Character i in the returned string must be '1' if the boy i+1 will be assigned to the first captain's team and '2' otherwise.

Definition

 
Class:TeamsSelection
Method:simulate
Parameters:vector <int>, vector <int>
Returns:string
Method signature:string simulate(vector <int> preference1, vector <int> preference2)
(be sure your method is public)
 
 

Constraints

-preference1 will contain N elements, where N is between 2 and 50, inclusive.
-Elements of preference1 will contain each of the numbers from 1 to N exactly once.
-preference2 will contain N elements.
-Elements of preference2 will contain each of the numbers from 1 to N exactly once.

Examples

0) 
 
{1, 2, 3, 4}
{1, 2, 3, 4}
Returns: "1212"
There are 4 boys to be divided between the two captains. Both captains believe that boy 1 plays best, then come boy 2 and boy 3, and boy 4 plays worst. Thus, the first captain will choose boy 1, the second captain will choose boy 2, since boy 1 is already assigned to a team, then the first captain will choose boy 3 and in the end the second captain will take boy 4.
1) 
 
{3, 2, 1}
{1, 3, 2}
Returns: "211"
The first captain will choose boy 3, the second captain will choose boy 1 and then the first captain will choose boy 2. Note that when there is an odd number of children, the first team ends up one man larger.
2) 
 
{6, 1, 5, 2, 3, 4}
{1, 6, 3, 4, 5, 2}
Returns: "212211"
3) 
 
{8, 7, 1, 2, 4, 5, 6, 3, 9}
{7, 2, 4, 8, 1, 5, 9, 6, 3}
Returns: "121121212"



#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;

class TeamsSelection
{
public:
    string simulate(vector<int>, vector<int>);
};

string TeamsSelection::simulate(vector<int>a, vector<int>b)
{
    char tmp[50];
    bool visit[53];
    int N = a.size();
    memset(visit,false,sizeof(visit));
    int flag1 = 0,flag2 = 0;
    for(int j = 0; j < N; j++)
    {
        for(int i = flag1; i < N; i++)
        {
            if(!visit[a[flag1]])
            {
                visit[a[flag1]] = true;
                tmp[a[flag1]-1] = '1';
                flag1++;
                break;
            }
            flag1++;
        }
        for(int i = flag2; i < N; i++)
        {
            if(!visit[b[flag2]])
            {
                visit[b[flag2]] = true;
                tmp[b[flag2]-1] = '2';
                flag2++;
                break;
            }
            flag2++;
        }
    }
    tmp[N] = '\0';
    return string(tmp);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值