Codeforces-1154E

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.ios

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).less

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

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.code

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.orm

Output

Print a string of nn characters; ii-th character should be 1 if ii-th student joins the first team, or 2 otherwise.xml

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

題目大意:有一隊n個學生站成一排,每一個學生有着其獨特的編程能力值(保證這些值不重複),現有1號老師和2號老師輪流從隊伍中挑選人(1號老師先選),每一個老師每次會選擇當前隊伍中編程能力值最大的學生和其左右兩邊k個學生加入他的隊伍,學生被選擇後會離開隊伍而且帶上那個老師的序號(1或2),最後須要按學生的原順序輸出每個學生的序號。blog

題解:使用stl裏的鏈表便可完成該題的操做,詳細操做見代碼註釋。排序

#include<iostream> #include<algorithm> #include<string> #include<cstring> #include<map> #include<set> #include<stack> #include<queue> #include<list> #include<cstdio> #include<cmath> #include<cctype> #include<iomanip>
using namespace std; typedef long long ll; const int N = 200005; list<pair<int, int> > L;//構建一個內部爲pair的鏈表
list<pair<int, int> >::iterator ite[N];//創建相應的每一個學生的迭代器用於以後的訪問
pair<int, int> a[N];//記錄每一個學生的數據,first是能力值,second是原編號
bool vis[N];//vis記錄該編號是否訪問過
int  ans[N];//ans按編號記錄學生所屬是1仍是2
int main() { ios::sync_with_stdio(false); // freopen("D:\\in.txt","r",stdin); // freopen("D:\\out.txt","w",stdout);
    int n, k, team = 1;//team表明的是老師的編號,即只有1和2,初始爲1
    cin >> n >> k; for(int i = 1; i <= n; i++) { cin >> a[i].first;//輸入能力值
        a[i].second = i;//second是初始編號
        L.push_back(a[i]);//在鏈表中插入該學生
        ite[i] = --L.end();//相應的迭代器指向該學生
 } sort(a + 1, a + 1 + n);//排序,按學生的能力值從小到大
    for(int i = n; i >= 1; i--)//逆序遍歷學生
 { int t = a[i].second;//提出學生的原編號
        if(vis[t])//訪問過了,直接跳過下列步驟
            continue; list<pair<int, int> >::iterator l = ite[t], r = ite[t];//新建l和r兩個迭代器來進行操做
        for(int i = 1; i <= k; i++)//出了該學生以外還要選兩邊的k我的
 { //迭代器分別向左向右移動,注意越界問題
            if(l != L.begin()) l--; if(r != --L.end()) r++; } r++; while(l != r)//開始進行刪除(離隊)操做
 { ans[l->second] = team;//按編號記錄所在隊伍
            vis[l->second] = true;//標記爲以訪問
            l = L.erase(l);//刪除該節點
 } team = 3 - team;//該操做能夠使老師編號在1和2之間來回更換
 } for(int i = 1; i <= n; i++) { cout << ans[i];//輸出結果
 } //system("pause");
    return 0; }
本站公眾號
   歡迎關注本站公眾號,獲取更多信息