Codeforces 1154E Two Teams

时间:2024-01-04 09:28:38

题目链接:http://codeforces.com/problemset/problem/1154/E

题目大意:

  有n个队员,编号1~n,每个人的能力各自对应1~n中的一个数,每个人的能力都不相同。有1号教练和2号教练,他们轮流从剩余队伍里选人,轮到某位教练选时,它总是选剩余队员中能力最强的人和他左右各k个人。问选完的时候每个人的组号。

分析:

  什么结构能快速查找到队员呢?当然是数组啦!什么结构能频繁删改呢?当然是链表啦!所以就用承载在数组上的链表来做啦!

代码如下:

 #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef set< int > SI;
typedef vector< int > VI;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ;
const LL ONE = ; struct Node{
int value, pos;
Node* prev = NULL;
Node* next = NULL;
}; int n, k, ans[maxN];
Node nodes[maxN];
int to[maxN];
int f = ; int main(){
scanf("%d%d\n", &n, &k);
For(i, , n) {
scanf("%d", &nodes[i].value);
nodes[i].pos = i;
to[nodes[i].value] = i;
} Rep(i, n + ) nodes[i].next = &nodes[i + ];
Rep(i, n + ) nodes[i + ].prev = &nodes[i]; int i = n;
while(i >= ) {
if(ans[to[i]] != ) {
--i;
continue;
} ans[to[i]] = f;
// 处理左边k个
int cnt = ;
Node *p = nodes[to[i]].prev;
while(p->prev != NULL && cnt < k) {
ans[p->pos] = f;
p = p->prev;
++cnt;
}
// 处理右边k个
cnt = ;
Node *q = nodes[to[i]].next;
while(q->next != NULL && cnt < k) {
ans[q->pos] = f;
q = q->next;
++cnt;
} // 删掉选走的
p->next = q;
q->prev = p; f = f == ? : ;
} For(i, , n) printf("%d", ans[i]);
printf("\n");
return ;
}