Codeforces Round #381 (Div. 2)C. Alyona and mex(思维)
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(思维)的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #381 (Div. 2) C. Alyona and mex(无语)
题目链接 http://codeforces.com/contest/740/problem/C 题意:有一串数字,给你m个区间求每一个区间内不含有的最小的数,输出全部中最小的那个尽量使得这个最小值最 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Lua查找表元素过程(元表、__index方法是如何工作的)
近日开始研究Lua,在元表的使用上照猫画虎地搞了两下,实现了“面向对象”,但究其本质却略有不解,后咨询牛哥得解,特此记录. Lua的表本质其实是个类似HashMap的东西,其元素是很多的Key-Val ...
- jquery.easyui使用详解,和遇到的问题,提供大家在使用的时候少走弯路(二)
上次解释了几个易犯错的地方,当然对于大神们那都是小菜一碟了,今天来说说后台请求数据,分页,返回json数据 废话不多说献上代码 private string QueryList(ArrayList a ...
- ListView到顶部不可再拉
ListView到顶部不可再拉,主要针对魅族.索爱等手机: 如果你的程序针对的是2.3及以上版本,可以直接调用AbsListView的setOverScrollMode方法来解决这个问题. 如果还要兼 ...
- 第三章Git使用入门--读书笔记
“管理”一词,几乎在生活的方方面面都存在,而在Linux驱动开发中会涉及很多的源代码,对于数量繁多的源码,我们也应该有一个管理软件.Android和Linux内核及驱动开发的源代码基本都是由Git 来 ...
- jQuery取得select 选中值和文本 来自园友“大气象”
本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").text();就是取得的文本. 这是不正确 ...
- 如何用github快速搭建个人博客
当当当当-来看下新鲜出炉的Github博客 http://wli12.github.io/ 喜欢写markdown,但cnblogs对md文件的渲染简直丑爆了... 好奇怎么用github+Jekyl ...
- javascript学习笔记全记录
js的初步了解 1.就是用来修改样式的,修改的是行内样式.任何样式都能够修改. 2.css里面怎么写js就怎么写. 3.任何元素都能加事件:事件都要小写 js的三大 ...
- 浏览器功能记住账号和密码解决方法(HTML解决方式)
1.在input标签里应用html5的新特性autocomplete="off" 注:对chrome不管用.其他浏览器没试. 2.如果是一个输入框那就在当前input标签后面(一 ...
- JavaScript,DOM经典基础面试题
JavaScript的数据类型 JavaScript的数据类型可以分为原始类型和对象类型 原始类型包括string,number和Boolean三种,其中字符串是使用一对单引号或者一堆双引号括起来的任 ...
- Scrollview嵌套listview
//建立Scrollview类 public class MyScrollView extends ScrollView { public MyScrollView(Context context, ...