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 
#include 
#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 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;
}

 

你可能感兴趣的:(STL)