微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Codeforces Round #552 (Div. 3) E C++

题目:

  

E. Two Teams
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 (1kn21051≤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 (1ain1≤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.

思路:

  模拟。

  第一次找出最大的数字,放入第一队,然后将这个人左边和右边的k个人也放入第一队(已经有队伍的人就除外咯)。

  第二次也是上述步骤,然后放入第二队。

  重复上述步骤。

  (注意这n个人的编码能力为1~n的一个全排列。)

超时代码!!!!

 

#include<bits/stdc++.h>
using namespace std;
int n,k,d[200010],t=0;
int a[200010],b[200010];
int main ()
{
    cin>>n>>k;
    int i;
    for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[a[i]]=i;
        }
    for(int h=n;h>=1;h--){
            i=b[h];
            int flag=t%2+1;
        if(d[i]==0){
            d[i]=flag;
            int j=i+1,cnt=0;
            while(j<=n){
                if(d[j]) {
                    j++;
                    continue;
                }
                cnt++;
                d[j]=flag;
                if(cnt==k) break;
                j++;
            }
            cnt=0;
            j=i-1;
            while(j>=1){
                    if(d[j]) {
                    j--;
                    continue;
                }
                cnt++;
                d[j]=flag;
                if(cnt==k) break;
                j--;
            }
            t++;
        }
    }
    for(i=1;i<=n;i++) cout<<d[i];
   return 0;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。