Algorithm


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 11 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 (1kn21051≤�≤�≤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 n integers a1,a2,,an�1,�2,…,�� (1ain1≤��≤�), 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 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).

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 2e5 + 10;
int n, k, a[N], seg[4 * N], prv[N], nxt[N], res[N];
vector <int> t1, t2;
set <int> st_inc, st_dec;

void build(int at, int l, int r) {
  if(l == r) {
    seg[at] = l;
    return;
  }

  int mid = (l + r) >> 1;
  build(at  < < 1, l, mid);
  build(at  < < 1 | 1, mid + 1, r);

  if(a[seg[at  < < 1]] > a[seg[at  < < 1 | 1]])
    seg[at] = seg[at  < < 1];
  else
    seg[at] = seg[at  < < 1 | 1];
}

int s, e;
int get(int at, int l, int r) {
  if(l> e || r  < s)
    return 0;
  
  if(l >= s && r  <= e)
    return seg[at];
  
  int mid = (l + r) >> 1;

  int gl = get(at  < < 1, l, mid);
  int gr = get(at  < < 1 | 1, mid + 1, r);

  if(a[gl] > a[gr])
    return gl;
  return gr;
}

int tar;
void update(int at, int l, int r) {
  if(l > tar || r  < tar)
    return;

  if(l == r) {
    a[l] = -1;
    return;
  }

  int mid = (l + r) >> 1;
  update(at  < < 1, l, mid);
  update(at  < < 1 | 1, mid + 1, r);

  if(a[seg[at  < < 1]] > a[seg[at  < < 1 | 1]])
    seg[at] = seg[at  < < 1];
  else
    seg[at] = seg[at  < < 1 | 1];
}

int main() {
  a[0] = -1;
  scanf("%d %d", &n, &k);
  for(int i = 1; i  <= n; ++i) {
    scanf("%d", a + i);
    st_inc.insert(i);
    st_dec.insert(-i);
    nxt[i] = i + 1;
    prv[i] = i - 1;
  }
  
  build(1, 1, n);

  s = 1, e = n;
  for(int i = 0, mxi, flst, blst; t1.size() + t2.size()  < n; ++i) {
    flst = blst = -1;
    mxi = get(1, 1, n);

    for(int j = mxi, cnt = 0; cnt <= k && j  <= n; ++cnt, j = nxt[j]) {
      tar = flst = j;
      update(1, 1, n);
      if(i % 2 == 0)
        t1.push_back(j);
      else
        t2.push_back(j);
      st_inc.erase(tar);
      st_dec.erase(-tar);
    }

    for(int j = prv[mxi], cnt = 1; cnt  <= k && j > 0; ++cnt, j = prv[j]) {
      tar = blst = j;
      update(1, 1, n);
      if(i % 2 == 0)
        t1.push_back(j);
      else
        t2.push_back(j);
      st_inc.erase(tar);
      st_dec.erase(-tar);
    }

    if(flst != -1) {
      if(st_dec.upper_bound(-nxt[flst]) == st_dec.end())
        prv[nxt[flst]] = 0;
      else
        prv[nxt[flst]] = -(*st_dec.upper_bound(-nxt[flst]));
    }

    if(blst != -1) {
      if(st_inc.upper_bound(prv[blst]) == st_inc.end())
        nxt[prv[blst]] = n + 1;
      else
        nxt[prv[blst]] = *st_inc.upper_bound(prv[blst]);
    }
  }

  for(int i = 0; i  < t1.size(); ++i)
    res[t1[i]] = 1;
  for(int i = 0; i  < t2.size(); ++i)
    res[t2[i]] = 2;
  
  for(int i = 1; i  <= n; ++i)
    printf("%d", res[i]);
  puts("">;

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5 2
2 4 5 3 1

Output

x
+
cmd
11111
Advertisements

Demonstration


Codeforces Solution Two Teams, E. Two Teams-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+