SRM 586 div2 250

Source: Internet
Author: User
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> SPreference1AndPreference2, 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.
Preference2Describes 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
- Preference1Will contain n elements, where n is between 2 and 50, inclusive.
- ElementsPreference1Will contain each of the numbers from 1 to n exactly once.
- Preference2Will contain n elements.
- ElementsPreference2Will 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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.