Codeforces 1154E Two Teams

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

题目大意:

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

分析:

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

代码如下:

  1 #pragma GCC optimize("Ofast")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4  
  5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 13  
 14 #define pr(x) cout << #x << " = " << x << "  "
 15 #define prln(x) cout << #x << " = " << x << endl
 16  
 17 #define LOWBIT(x) ((x)&(-x))
 18  
 19 #define ALL(x) x.begin(),x.end()
 20 #define INS(x) inserter(x,x.begin())
 21  
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,inf,sizeof(a))
 24 #define msM(a) memset(a,-1,sizeof(a))
 25 
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30  
 31 template<typename T1, typename T2>
 32 istream &operator>>(istream &in, pair<T1, T2> &p) {
 33     in >> p.first >> p.second;
 34     return in;
 35 }
 36  
 37 template<typename T>
 38 istream &operator>>(istream &in, vector<T> &v) {
 39     for (auto &x: v)
 40         in >> x;
 41     return in;
 42 }
 43  
 44 template<typename T1, typename T2>
 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 46     out << "[" << p.first << ", " << p.second << "]" << "\n";
 47     return out;
 48 }
 49  
 50 typedef long long LL;
 51 typedef unsigned long long uLL;
 52 typedef pair< double, double > PDD;
 53 typedef set< int > SI;
 54 typedef vector< int > VI;
 55 const double EPS = 1e-10;
 56 const int inf = 1e9 + 9;
 57 const LL mod = 1e9 + 7;
 58 const int maxN = 2e5 + 7;
 59 const LL ONE = 1;
 60 
 61 struct Node{
 62     int value, pos;
 63     Node* prev = NULL;
 64     Node* next = NULL;
 65 };
 66 
 67 int n, k, ans[maxN];
 68 Node nodes[maxN];
 69 int to[maxN]; 
 70 int f = 1;
 71 
 72 int main(){
 73     scanf("%d%d\n", &n, &k);
 74     For(i, 1, n) {
 75         scanf("%d", &nodes[i].value);
 76         nodes[i].pos = i;
 77         to[nodes[i].value] = i;
 78     }
 79     
 80     Rep(i, n + 1) nodes[i].next = &nodes[i + 1];
 81     Rep(i, n + 1) nodes[i + 1].prev = &nodes[i];
 82     
 83     int i = n;
 84     while(i >= 1) {
 85         if(ans[to[i]] != 0) {
 86             --i;
 87             continue;
 88         }
 89         
 90         ans[to[i]] = f;
 91         // 处理左边k个 
 92         int cnt = 0;
 93         Node *p = nodes[to[i]].prev;
 94         while(p->prev != NULL && cnt < k) {
 95             ans[p->pos] = f;
 96             p = p->prev;
 97             ++cnt;
 98         }
 99         // 处理右边k个 
100         cnt = 0;
101         Node *q = nodes[to[i]].next;
102         while(q->next != NULL && cnt < k) {
103             ans[q->pos] = f;
104             q = q->next;
105             ++cnt;
106         }
107         
108         // 删掉选走的 
109         p->next = q;
110         q->prev = p;
111         
112         f = f == 1 ? 2 : 1;
113     }
114     
115     For(i, 1, n) printf("%d", ans[i]);
116     printf("\n");
117     return 0;
118 }
View Code

 

posted @ 2019-04-18 20:18  梦樱羽  阅读(428)  评论(0编辑  收藏  举报
Live2D