cfdiv3e two teams(set的用法记录)

E. Two Teams
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

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
inputCopy
5 2
2 4 5 3 1
outputCopy
11111
inputCopy
5 1
2 1 3 5 4
outputCopy
22111
inputCopy
7 1
7 2 1 3 5 4 6
outputCopy
1121122
inputCopy
5 1
2 4 5 3 1
outputCopy
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).

(1)每次找出剩下的数中的最大值,分别向左向右移动K个,把这范围的值划到一个队里(如果左边的数小于K个,则把左边的所有值都选上,右边也是如此)已经归队的数就从序列里删除,(2)
先第一队,再第二队,再第一队以此类推直到序列为空,每次都是执行(1)步骤。
思路:用一个set记录剩下的序列中还有第几个元素(按输入顺序),用另一个set记录还剩的元素已经这个元素是第几个元素(按输入顺序),因为set已经按字典序排好了,所有每次最后一个就是最大的一个,所有每次都可以直接得到剩下的序列中最大的值以及这个元素是第几个元素(按输入顺序),再用第一个set遍历左右(k范围)即可
ac代码
主要是set不太会用,导致各种毛病出现
这次记录怎么边遍历边删除

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
int p[200005],q[200005],time[200005];
int main()
{
	set<pair<int, int> >mp;
	set<int>ji;
	int n, k, temp = 1;
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> p[i];
		mp.insert({ p[i],i });
		ji.insert(i);
	}
	set<pair<int, int> >::iterator it;
	set<int>::iterator iterm;
	set<int>::iterator iterm2;
	for (int j = 0;; j++)
	{
		temp++;
		if (mp.size() == 0)
			break;
		it = mp.end();//利用set已经排好序的好处
		it--;
		int a = it->second;
		iterm = ji.find(a);
		iterm2 = iterm;
		for (int i = 1; i <= k&&iterm2!=ji.end(); i++)
			iterm2++;
		if (iterm2 == ji.end())iterm2--;//end()这里指的不是最后一个元素的迭代器
		int u = *iterm2;
		int jian = k;
		for (; iterm!=ji.begin(); iterm--)
		{
			if (!jian)break;
			jian--;
		}
		for (; iterm != ji.end(); )
		{
			if (*iterm == u) {
				mp.erase({ p[u],u });
				q[u] = temp % 2;
				ji.erase(u);
				break;
			}
			mp.erase({ p[*iterm],*iterm });
			q[*iterm] = temp % 2;
			ji.erase(iterm++);//边遍历边删
		}
	}
		for (int i = 1; i <= n; i++)
			cout << q[i] + 1;
		cout << endl; return 0;
}
在这里插入代码片
发布了109 篇原创文章 · 获赞 35 · 访问量 6027

猜你喜欢

转载自blog.csdn.net/weixin_43965698/article/details/89360207
two