zoukankan      html  css  js  c++  java
  • stl应用

    http://codeforces.com/problemset/problem/1154/E

    E. Two Teams
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    There are n

    students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.

    The i

    -th student has integer programming skill ai. All programming skills are distinct and between 1 and n

    , inclusive.

    Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and k

    closest students to the left of him and k closest students to the right of him (if there are less than k

    students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).

    Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.

    Input

    The first line of the input contains two integers n

    and k (1kn2105

    ) — the number of students and the value determining the range of chosen students during each move, respectively.

    The second line of the input contains n

    integers a1,a2,,an (1ain), where ai is the programming skill of the i

    -th student. It is guaranteed that all programming skills are distinct.

    Output

    Print a string of n

    characters; i-th character should be 1 if i

    -th student joins the first team, or 2 otherwise.

    Examples
    Input
    Copy
    5 2
    2 4 5 3 1
    
    Output
    Copy
    11111
    
    Input
    Copy
    5 1
    2 1 3 5 4
    
    Output
    Copy
    22111
    
    Input
    Copy
    7 1
    7 2 1 3 5 4 6
    
    Output
    Copy
    1121122
    
    Input
    Copy
    5 1
    2 4 5 3 1
    
    Output
    Copy
    21112
    
    Note

    In the first example the first coach chooses the student on a position 3

    , and the row becomes empty (all students join the first team).

    In the second example the first coach chooses the student on position 4

    , and the row becomes [2,1] (students with programming skills [3,4,5] join the first team). Then the second coach chooses the student on position 1, and the row becomes empty (and students with programming skills [1,2]

    join the second team).

    In the third example the first coach chooses the student on position 1

    , and the row becomes [1,3,5,4,6] (students with programming skills [2,7] join the first team). Then the second coach chooses the student on position 5, and the row becomes [1,3,5] (students with programming skills [4,6] join the second team). Then the first coach chooses the student on position 3, and the row becomes [1] (students with programming skills [3,5] join the first team). And then the second coach chooses the remaining student (and the student with programming skill 1

    joins the second team).

    In the fourth example the first coach chooses the student on position 3

    , and the row becomes [2,1] (students with programming skills [3,4,5] join the first team). Then the second coach chooses the student on position 1, and the row becomes empty (and students with programming skills [1,2] join the second team).

    题意:给你n个数排成一排,两个教练轮流在队伍中找到最大的那个数,

    并将该数两边k个同时提出队伍,然后第二教练重复上述步骤,直到队伍没人结束。

    问每个数被哪一个教练选中。

    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1000000007
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    
    
    
    int main()
    {
        int n , k ;
        scanf("%d%d" , &n , &k);
        vector<pair<int , int> >v(n);//给定容量的vector
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%d" , &v[i].first);
            v[i].second = i ;
        }
        sort(v.rbegin() , v.rend());//反相迭代降序,正向就为升序
        queue<int>q;
        for(int i = 0 ; i < n ; i++)
        {
            q.push(v[i].second);
        }
        set<int>s;
        for(int i = 0 ; i < n ; i++)
        {
            s.insert(i);
        }
        int who = 0 ;
        string ans(n , '0');//赋值为n个0字符
        while(!s.empty())
        {
            while(!s.count(q.front()))
            {
                q.pop();
            }
            int pos = q.front();
            vector<int>v1;
            auto it = s.find(pos);
            for(int i = 0 ; i <= k ; i++)
            {
                v1.push_back(*it);
                it++ ;
                if(it == s.end())
                    break ;
            }
            it = prev(s.find(pos));//迭代器的前一个
            // it = next(s.find(pos)) 迭代器的后一个
            for(int i = 0 ; i < k ; i++)
            {
                v1.push_back(*it);
                if(it == s.begin())
                    break ;
                it-- ;
            }
            for(auto it : v1)
            {
                s.erase(it);
                ans[it] = '1'+ who;
            }
            who ^= 1 ;
        }
        cout << ans << endl;
    
    
        return 0 ;
    }
  • 相关阅读:
    单片机编程积累算法
    关于GSM基站定位
    GSM模块fibocom G510使用记录
    指爱 打字比赛记录
    硬件和软件工程师
    GPS模块启动模式说明
    阻容降压电路分析
    饮水机电路-工作剖析
    跑步,去
    day01 IT知识架构,操作系统简介
  • 原文地址:https://www.cnblogs.com/nonames/p/11818937.html
Copyright © 2011-2022 走看看