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).
题解:开启L,R数组记录最近的左边、右边的人,当选取一回队伍时要更新L,R数组。还有记录上每个数在输入时的位置,用于最后判断。
感受:打cf的div3时这个题超时,最后也没能过去,原因是没开启L,R数组,直接暴力找。最后div3为675名,如果做出来这个题能搞到100多名呀~~
#include<bits/stdc++.h>
using namespace std;
const int maxn=200020;
int position[maxn],L[maxn],R[maxn],ans[maxn];
int n,k,flag=1;
void Delete(int pos)
{
int l=pos,r=pos;
for(int i=0;i<=k;i++)//依次向左右找人
{
ans[l]=ans[r]=flag;//记录答案
l=L[l];//跑左边一个
r=R[r];//跑右边一个
}
R[l]=r;//由于开区间l,r内的数以被选取,则要更新左右数组
L[r]=l;
flag=flag%2+1; //换个Team
}
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
L[i]=i-1;//记录左右的人
R[i]=i+1;
position[x]=i;//记录每个值所在的位置
}
R[n+1]=n+1;//要加上防止越界
int cnt=n;
while(cnt)//一直找到0就好了
{
while(cnt&&ans[position[cnt]])//如果该数的位置上的数已经有队伍了,那么再往下找
cnt--;
Delete(position[cnt]);//以cnt为中心选取左右k个人,记录答案
}
for(int i=1;i<=n;i++)
cout<<ans[i];
return 0;
}