码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-04-20 21:05:00      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:tput   rop   cte   tty   个人   ace   kill   can   property   

题目:

  

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;
}

 


}

改进:将已经有队伍的人删除。(借鉴了大佬的解法)
AC代码:
  
#include<bits/stdc++.h>
using namespace std;
int n,k,d[200010],t=0;
int a[200010],b[200010],l[200010],r[200010];
void eraser(int x)
{
    l[r[x]]=l[x];
    r[l[x]]=r[x];
}
int main ()
{
    cin>>n>>k;
    int i;
    for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[a[i]]=i;
            l[i]=i-1;
            r[i]=i+1;
        }
    for(int h=n;h>=1;h--){
            i=b[h];
            int flag=t%2+1;
        if(d[i]==0){
            eraser(i);
            d[i]=flag;
            int j=r[i],cnt=0;
            while(j<=n){
                if(d[j]) {
                    j++;
                    continue;
                }
                cnt++;
                d[j]=flag;
                eraser(j);
                if(cnt==k) break;
                j++;
            }
            cnt=0;
            j=l[i];
            while(j>=1){
                    if(d[j]) {
                    j--;
                    continue;
                }
                cnt++;
                d[j]=flag;
                eraser(j);
                if(cnt==k) break;
                j--;
            }
            t++;
        }
    }
    for(i=1;i<=n;i++) cout<<d[i];
   return 0;
}

 

 

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

标签:tput   rop   cte   tty   个人   ace   kill   can   property   

原文地址:https://www.cnblogs.com/-xyp/p/10742583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!