C. Alyona and mex

Problem Description:

Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.

Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].

Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.

You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.

The mex of a set S is a minimum possible non-negative integer that is not in S.

Input:

The first line contains two integers n and m (1 ≤ n, m ≤ 105).

The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].

Output:

In the first line print single integer — the maximum possible minimum mex.

In the second line print n integers — the array a. All the elements in a should be between 0 and 109.

It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.

If there are multiple solutions, print any of them.

Sample Input:

5 3

1 3

2 5

4 5

Sample Output:

2

1 0 2 1 0

这题我觉得是思维的题,如果直接告诉这么实现的话,几乎都会,但根据题意能想到这一层次的人却不是很多,所以应更注重思维的锻炼。

【题目链接】C. Alyona and mex

【题目类型】构造

&题意:

要构造出一个数组,使他的mex值最大,mex值定义:一个区间[li,ri]内,最小的不重复的非负整数。

&题解:

仔细观察发现,最大的就是区间长度最小的长度值,之后根据这个区间,向两边扩展,按照0~dis-1的顺序,一直扩展就好了。

&代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define rez(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
#define PI(A) cout<<(A)<<endl;
const int MAXN = 100000 + 5 ;
int n, m, x, y, dis;
int a[MAXN];
int main() {
while (cin >> n >> m) {
dis = INF;
rez(i, 1, m) {
int u, v;
cin >> u >> v;
if (dis > v - u + 1) {
dis = v - u + 1;
x = u, y = v;
}
}
int p = 0;
PI(dis)
rez(i, x, y) {
a[i] = p++;
}
p = 0;
rez(i, y + 1, n) {
a[i] = p % dis;
p++;
}
p = (int)2e9 - (int)2e9 % dis - 1;
red(i, x - 1, 0) {
a[i] = p % dis;
p--;
}
rez(i, 1, n)
printf("%d ", a[i]);
}
return 0;
}

Codeforces Round #381 (Div. 2)C. Alyona and mex(思维)的更多相关文章

  1. Codeforces Round #381 (Div. 1) A. Alyona and mex 构造

    A. Alyona and mex 题目连接: http://codeforces.com/contest/739/problem/A Description Alyona's mother want ...

  2. Codeforces Round #381 (Div. 2)C Alyona and mex

    Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be ...

  3. Codeforces Round #381 (Div. 2) C. Alyona and mex(无语)

    题目链接 http://codeforces.com/contest/740/problem/C 题意:有一串数字,给你m个区间求每一个区间内不含有的最小的数,输出全部中最小的那个尽量使得这个最小值最 ...

  4. Codeforces Round #358 (Div. 2)B. Alyona and Mex

    B. Alyona and Mex time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

  6. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  7. Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)

    D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...

  8. Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)

    B. Alyona and flowers Problem Description: Let's define a subarray as a segment of consecutive flowe ...

  9. Codeforces Round #381 (Div. 2)A. Alyona and copybooks(dfs)

    A. Alyona and copybooks Problem Description: Little girl Alyona is in a shop to buy some copybooks f ...

随机推荐

  1. wordpress模板学习之导航目录

    wordpress的导航目录分为三个部分,一开启,二配置:三使用 开启在functions.php,这个注册会保存在全局变量中,接下来在菜单配置中会看到 register_nav_menu( 'pri ...

  2. 页码条--字符串拼接--重写HtmlHelper

    public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, ...

  3. VS 打开工程后 自动关闭

    今天在打开一个VS2008的工程的时候,会提示vs2008 已停止工作的异常信息,具体的解决办法如下: 打开vs2008命令提示窗口: 打开窗口后:键入:devenv.exe /resetuserda ...

  4. C#编程语言与面向对象——继承

    现实生活中的事物都归属于一定的类别,比如,狮子是一种(IS_A)动物,为了在计算机中模拟这种关系,面向对象的语言引入了继承(inherit)特性. 构成继承关系的两个类中,Animal称为父类(par ...

  5. ABAP程序运行锁定

    转自http://www.cnblogs.com/aBaoRong/archive/2012/06/15/2550458.html ABAP 程序运行锁 1. create a Table ZRUNN ...

  6. SpringMVC中使用Cron表达式的定时器

    SpringMVC中使用Cron表达式的定时器 cron(定时策略)简要说明 顺序: 秒 分 时 日 月 星期 年份 (7个参数,空格隔开各个参数,年份非必须参数) 通配符: , 如果分钟位置为* 1 ...

  7. Linux指令小结

    1.apt指令集:是ubuntu中最强大的命令行软件管理工具,用于获取.安装.编译.卸载和查询软件包.还可以检查软件包的依赖关系.在ubunt中下载是根据/etc/apt/sources.list这个 ...

  8. Django后台post请求中的csrf token

    使用Requests库操作自己的Django站点,post登陆admin页面返回403,serverlog显示csrf token not set. csrf token是get登陆页面时服务器放在c ...

  9. 【引】objective-c,5:Associated Objects 的原理

    参考博客: http://blog.leichunfeng.com/blog/2015/06/26/objective-c-associated-objects-implementation-prin ...

  10. 【我是老中医】Win10系统下MATLAB无法正常打开的解决方案

    转眼大四了,要开始做毕设了,导师给的题目要用到他之前做的东西,都是MATLAB做的,所以不太熟悉MATLAB的我也得用这玩意儿了,想想自己目前也就大二的DSS实验和大三的AI实验用过MATLAB,当时 ...