题意分析:
两队轮流选人,每次选分最高的及左右各k个(不相邻!!!)
可以用数组模拟链表,每次选完人后,将区间两端相接
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 2e5 + 5;
typedef pair<int, int> pii;
priority_queue<pii> q;
int pre[maxn]; //左
int nxt[maxn]; //右
int vis[maxn]; //已被选
int ans[maxn];
int main(void)
{
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
q.push(make_pair(x, i));
pre[i] = i - 1;
nxt[i] = i + 1;
}
int p = 1;
while (q.size()) {
int u = q.top().second;
q.pop();
if (vis[u]) continue;
ans[u] = p & 1 ? 1 : 2;
int cnt = 0, ll = 0, rr = n + 1;
int i;
for (i = pre[u]; cnt < k && i; i = pre[i]) {
vis[i] = 1;
ans[i] = p & 1 ? 1 : 2;
cnt++;
}
ll = i; //记录区间左端
cnt = 0;
for (i = nxt[u]; cnt < k && i != n + 1; i = nxt[i]) {
vis[i] = 1;
ans[i] = p & 1 ? 1 : 2;
cnt++;
}
rr = i; //记录区间右端
pre[rr] = ll; //更新指针
nxt[ll] = rr;
p++;
}
for (int i = 1; i <= n; i++) cout << ans[i];
return 0;
}