cf 552 (Div. 3)Two Teams

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 1  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  (1≤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,…,an  (1≤ai≤n ), 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.
5 2
2 4 5 3 1
11111
7 1
7 2 1 3 5 4 6
1121122
定個目標,用stl容器寫出讓自己汗顏的最最優美代碼。

#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
bool one[N]; //是否1隊
int main(){
	map<int, int> pos, val; //位置順序 值順序
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; ++i){
		int v;
		scanf("%d", &v);
		pos[i] = v;
		val[v] = i;
	}
	int flag = 1;
	while (!val.empty()){
		int p = val.rbegin()->second; //最大值位置
		for (int i = 0; i < k; ++i) //左側
		{
			auto it = pos.lower_bound(p);
			if (it == pos.begin())
				break;
			--it;
			if (flag) one[it->first] = 1;
			val.erase(it->second);
			pos.erase(it);
		}
		for (int i = 0; i < k; ++i) //右側
		{
			auto it = pos.upper_bound(p);
			if (it == pos.end())
				break;
			if (flag) one[it->first] = 1;
			val.erase(it->second);
			pos.erase(it);
		}
		if (flag) one[p] = 1;
		val.erase(pos[p]);
		pos.erase(p);
		flag ^= 1;
	}
	for (int i = 1; i <= n; ++i)
		putchar(one[i] ? '1' : '2');
	cout << endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章