Codeforces 552 E. Two Teams
2 seconds
256 megabytes
standard input
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.
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.
Print a string of nn characters; ii-th character should be 1 if ii-th student joins the first team, or 2 otherwise.
5 2
2 4 5 3 1
11111
5 1
2 1 3 5 4
22111
7 1
7 2 1 3 5 4 6
1121122
5 1
2 4 5 3 1
21112
In the first example the first coach chooses the student on a position 33, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position 44, and the row becomes [2,1][2,1] (students with programming skills [3,4,5][3,4,5] join the first team). Then the second coach chooses the student on position 11, and the row becomes empty (and students with programming skills [1,2][1,2] join the second team).
In the third example the first coach chooses the student on position 11, and the row becomes [1,3,5,4,6][1,3,5,4,6] (students with programming skills [2,7][2,7] join the first team). Then the second coach chooses the student on position 55, and the row becomes [1,3,5][1,3,5] (students with programming skills [4,6][4,6] join the second team). Then the first coach chooses the student on position 33, and the row becomes [1][1] (students with programming skills [3,5][3,5] join the first team). And then the second coach chooses the remaining student (and the student with programming skill 11 joins the second team).
In the fourth example the first coach chooses the student on position 33, and the row becomes [2,1][2,1] (students with programming skills [3,4,5][3,4,5] join the first team). Then the second coach chooses the student on position 11, and the row becomes empty (and students with programming skills [1,2][1,2] join the second team).
题目链接 : http://codeforces.com/contest/1154/problem/E
题意 :
给你n个值(1~n且不重复),选中最大的数,并将它左边k个元素和右边k个元素(可能不足,不足的部分不考虑)选中标记为 1 ,这2*k+1个元素同时从数组中删除。重复操作,但是标记为2,结束一轮。
重复任意多轮,直至所有数都被标记(1 or 2)。
当时并没有好的思路,赛后看的别人的代码才会的。
大佬的AK日常: https://www.jianshu.com/p/fd764cf686e9
考虑到数据是1~n的全排列, 所以我们可以用数组来将元素的位置离散出来。
实现类似于双链表的数据结构,用于快速找到当前元素的左右相邻元素。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int maxn = 2e5+;
int n, k;
int sk[maxn], le[maxn], ri[maxn];
int arr[maxn], ans[maxn]; void remo(int ind){
le[ri[ind]] = le[ind];
ri[le[ind]] = ri[ind];
}
int main(){
memset(ans,,sizeof(ans));
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
scanf("%d",&arr[i]);
sk[arr[i]] = i;
le[i] = i-;
ri[i] = i+;
}
ans[] = ans[n+] = -;// 边界
int team = ;
for(int i=n;i>=;i--){
int index = sk[i];
if(ans[index])
continue;
remo(index);
ans[index] = team;
int l = le[index], r = ri[index];
for(int i=;i<k;i++,l=le[l]){
if(ans[l])
break;
remo(l);
ans[l] = team;
}
for(int i=;i<k;i++,r=ri[r]){
if(ans[r])
break;
remo(r);
ans[r] = team;
}
team = - team;
}
for(int i=;i<=n;i++)
printf("%d",ans[i]);
putchar('\n');
return ;
}
Codeforces 552 E. Two Teams的更多相关文章
- Codeforces 544E K Balanced Teams (DP)
题目: You are a coach at your local university. There are nn students under your supervision, the prog ...
- codeforces 1133E K Balanced Teams
题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的 ...
- 【CODEFORCES】 B. Random Teams
B. Random Teams time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces 1133E - K Balanced Teams - [DP]
题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...
- codeforces 552 E. Vanya and Brackets 表达式求值
题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...
- codeforces#552 D. Vanya and Triangles(几何)
题意:给出n个不同的点,问能组成多少个不同的三角形 题解:对于每个点对,我们生成一个直线,用a*x+b=y表示,用map记录ab,这样就确定了一个直线,这样我们就能算出有多少点是共线的,这样复杂度就是 ...
- codeforces 552 C Vanya and Scales
这个题的意思就是给出一个数m.以及一个以1为首元素.w为比例常数的等比数列,数列长度为101,数列中每一个数字最多仅仅能用一次.问是否存在xa+wb+--=wc+wd+--+we+m. 非常显然,换句 ...
- 51nod 1596 搬货物【贪心/二进制】
1596 搬货物 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 取消关注 现在有n个货物,第i个货物的重量是 2wi ...
- Codeforces Round #552 (Div. 3) E. Two Teams (模拟,优先队列,双向链表)
题意:有\(n\)个队员站成一排,有两个教练分别选人,每次选当前剩余人中的能力值最大的那个以及他两边相邻的\(k\)个人,问最后每个人所在队伍情况. 题解:优先队列模拟,以及双向链表,先用结构体存入每 ...
随机推荐
- flashcache中内存与磁盘,磁盘与磁盘的io
flashcache中跟磁盘相关的读写分为以下两类: 1)磁盘跟内存的交互 2)磁盘跟磁盘之前的交互 比如说读不命中时就是直接从磁盘读,属于第1种情况,那读命中呢?也是属于第1种情况,不过这时候是从S ...
- Microsoft Visio / Project professional 2013 官方版本(下载)
Microsoft Visio微软开发的一款软件, 它有助于 IT 和商务专业人员轻松地可视化.分析和交流复杂信息. 它能够将难以理解的复杂文本和表格转换为一目了然的 Visio 图表. 该软件通过创 ...
- C++继承与多态练习--计算图形面积
1.目的: /*设计一个计算图形面积的类库. 类库的顶层是一个抽象类,并且提供三个纯虚函数:显示数据成员.返回面积和返回体积. Class Shape { virtual void showData( ...
- 01.centos7环境准备
博客为日常工作学习积累总结: 1.环境准备: 系统版本:CentOS-7-x86_64-Minimal-1810.iso 运行环境:虚拟机windows上的VM 15 系统安装:参照老男孩运维要求 2 ...
- 关于json时间数据格式转换与修改
使用easyui获取JSON时间数据时间数据时,通常是一长串的数字而不是我们想要的类似2018-11-01的普通时间格式. 此时我们就需要使用到关于JSON时间的格式化,而将时间转化成我们想要的格式. ...
- mvc.net路由中带特殊字符如【.*/\】等时遇到的天坑
用mvc.net的路由做网站伪静态时出现的天坑,自己一直没测试出来,竟然要靠客户被坑了后才知道 解决办法 参考https://stackoverflow.com/questions/16581184/ ...
- PHP程序员的技术成长规划 第二阶段:提高阶段
第二阶段:提高阶段 (中级PHP程序员) 重点:提高针对LNMP的技能,能够更全面的对LNMP有熟练的应用.目标:能够随时随地搭建好LNMP环境,快速完成常规配置:能够追查解决大部分遇到的开发和线上环 ...
- JVM,JMM,虚拟机栈,本地方法栈
JVM 虚拟机栈 本地方法栈:本地方法(使用native关键词修饰的方法,是由JVM底层用C,C++实现的),运行这部份代码使用的栈就是本地方法栈
- jenkins + ansible + docker 代码集成发布
一.环境搭建 1. 安装Java 配java_home, /etc/profile 2.安装Jenkins 下载war包,用 Java -jar Jenkins.war或者 把war包放tomca ...
- scala (4) 可变数组和不可变数组
在scala中数组分为不可变长数组(在immutable包下)和可变长数组(在mutable包下) 不可变长数组指的是长度不可变,但是数组中角标对应的元素的值是可变的 可变数组指的是长度和数组中角标对 ...