题目:
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 (1≤k≤n≤2⋅1051≤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 (1≤ai≤n1≤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; }
原文地址:https://www.cnblogs.com/-xyp/p/10742583.html