Codeforces 552 E. Two Teams

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 (1kn21051≤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 (1ain1≤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
5 2
2 4 5 3 1
output
11111
input
5 1
2 1 3 5 4
output
22111
input
7 1
7 2 1 3 5 4 6
output
1121122
input
5 1
2 4 5 3 1
output
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).

题目链接 : http://codeforces.com/contest/1154/problem/E

题意 :

  给你n个值(1~n且不重复),选中最大的数,并将它左边k个元素和右边k个元素(可能不足,不足的部分不考虑)选中标记为 1 ,这2*k+1个元素同时从数组中删除。重复操作,但是标记为2,结束一轮。

  重复任意多轮,直至所有数都被标记(1 or 2)。

当时并没有好的思路,赛后看的别人的代码才会的。

大佬的AK日常: https://www.jianshu.com/p/fd764cf686e9

  考虑到数据是1~n的全排列, 所以我们可以用数组来将元素的位置离散出来。

  实现类似于双链表的数据结构,用于快速找到当前元素的左右相邻元素。

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

const int maxn = 2e5+5;
int n, k;
int sk[maxn], le[maxn], ri[maxn];
int arr[maxn], ans[maxn];

void remo(int ind){
    le[ri[ind]] = le[ind];
    ri[le[ind]] = ri[ind];
}
int main(){
    memset(ans,0,sizeof(ans));
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&arr[i]);
        sk[arr[i]] = i;
        le[i] = i-1;
        ri[i] = i+1;
    }
    ans[0] = ans[n+1] = -1;// 边界
    int team = 1;
    for(int i=n;i>=0;i--){
        int index = sk[i];
        if(ans[index])
            continue;
        remo(index);
        ans[index] = team;
        int l = le[index], r = ri[index];
        for(int i=0;i<k;i++,l=le[l]){
            if(ans[l])
                break;
            remo(l);
            ans[l] = team;
        }
        for(int i=0;i<k;i++,r=ri[r]){
            if(ans[r])
                break;
                remo(r);
                ans[r] = team;
        }
        team = 3 - team;
    }
    for(int i=1;i<=n;i++)
        printf("%d",ans[i]);
    putchar('
');
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/kongbb/p/10743056.html