Codeforces E. Two Teams

5581 단어 stl모방하다
E. Two Teams
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn 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 ii-th student has integer programming skill aiai. All programming skills are distinct and between 11 and nn, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and kk closest students to the left of him and kk closest students to the right of him (if there are less than kk 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 nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n), where aiai is the programming skill of the ii-th student. It is guaranteed that all programming skills are distinct.
Output
Print a string of nn characters; ii-th character should be 1 if ii-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 33, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position 44, and the row becomes [2,1][2,1] (students with programming skills [3,4,5][3,4,5] join the first team). Then the second coach chooses the student on position 11, and the row becomes empty (and students with programming skills [1,2][1,2] join the second team).
In the third example the first coach chooses the student on position 11, and the row becomes [1,3,5,4,6][1,3,5,4,6] (students with programming skills [2,7][2,7] join the first team). Then the second coach chooses the student on position 55, and the row becomes [1,3,5][1,3,5] (students with programming skills [4,6][4,6] join the second team). Then the first coach chooses the student on position 33, and the row becomes [1][1] (students with programming skills [3,5][3,5] join the first team). And then the second coach chooses the remaining student (and the student with programming skill 11 joins the second team).
In the fourth example the first coach chooses the student on position 33, and the row becomes [2,1][2,1] (students with programming skills [3,4,5][3,4,5] join the first team). Then the second coach chooses the student on position 11, and the row becomes empty (and students with programming skills [1,2][1,2] join the second team).
제목의 대의: 일부 학생들은 능력치가 있고 두 명의 코치가 선출한다. 규칙은 다음과 같다. 매번 능력치가 가장 큰 사람을 선택한 다음에 그의 좌우 양쪽의 k사람을 선택한다.두 코치가 한 번 선택하고 마지막에 한 명씩 내보내면 몇 번째 코치가 뽑는다.
문제풀이 사고방식: 모든 사람의 위치와 능력치를 보존하고 능력치가 높은 순서에서 낮은 순서로 정렬한다. 한 대열로 능력이 높은 학생에서 낮은 학생의 번호를 보존하고 한 set으로 전체 수조의 위치를 보존한다. 매번 대열에서 가장 큰 한 사람의 위치를 꺼내 set에서 찾고 좌우 양쪽을 각각 k회 훑어본다. 훑어보는 사람의 위치는 모두 한 vector에 저장한다.마지막으로 이 벡터를 훑어보고 표시를 하면 몇 번째 코치가 뽑은 사람이고 set에서 삭제하면 됩니다.이렇게 시뮬레이션을 하면 set이 비어 있을 때까지 해결됩니다.
제목 링크:https://codeforces.com/contest/1154/problem/E
/*
@Author: Top_Spirit
@Language: C++
*/
#include 
using namespace std ;
typedef long long ll ;
typedef pair < int, int > P ;
const int Maxn = 2e5 + 10 ;

int n, k ;

int main (){
    cin >> n >> k ;
    vector < P > a(n) ;
    for (int i = 0; i < n; i++){
        cin >> a[i].first ;
        a[i].second = i ;
    }
    sort(a.rbegin(), a.rend()) ;
    queue < int > que ;
    for (int i = 0; i < n; i++) que.push(a[i].second) ;
    set < int > se ;
    for (int i = 0; i < n; i++) se.insert(i) ;
    string ans(n, '0') ;
    int cnt = 0 ;
    while (!se.empty()){
        while (!se.count(que.front())) que.pop() ;
        int index = que.front() ;
        que.pop() ;
        vector < int > add ;
        auto it = se.find(index) ;
        for (int i = 0; i <= k; i++){
            add.push_back(*it) ;
            if (it == se.begin()) break ;
            it-- ;
        }
        it = next(se.find(index)) ;
        for (int i = 0; i < k; i++){
            if (it == se.end()) break ;
            add.push_back(*it) ;
            it++ ;
        }
        for (auto i : add){
            ans[i] = '1' + cnt ;
            se.erase(i) ;
        }
        cnt ^= 1 ;
    }
    cout << ans << endl ;
    return 0 ;
}

좋은 웹페이지 즐겨찾기