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 | |||||||||||||
| |||||||||||||
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) | |||||||||||||
|
#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);
}