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 ...
随机推荐
- SVN的部署及分支等方法
1.本地Repository的创建 repository的创建很简单,假设我要在D:\TortoiseSVN\TestRepository目录中创建repository,只需 右键TestReposi ...
- 第三个Sprint冲刺事后诸葛亮报告
用户反馈:还好吧. 用户数量:4 团队改进建议:思维局限太大,技术需要革新. 1.每个成员第一个sprint阶段有何需要改进? 成员 需要改进 邵家文 需要提高自己的工作效率,与创新能力,解决问题的能 ...
- C++学习笔记26:泛型编程概念
一.什么是泛型编程? 泛型就是通用的型式 编写不依赖数据对象型式的代码就是泛型编程 二.为什么需要泛型编程? 函数重载,相似类定义与型式兼容性 例如:设计函数,求两个数据对象的较小值 //未明确规定参 ...
- WPF学习笔记——TextBox的一些问题
1.如何判断TextBox是否为空? if(textbox.Text != string.empty) //或者 if(textbox.Text.Trim()!="") { } 2 ...
- 用bugzilla统计BUG
bugzilla—— reports—— Tabular reports—— 选择版本号啥的跟search一样 上面的行标Vertical Axis选Resolution:列标Horizontal A ...
- sql指定插入自增长id的数据
SET IDENTITY_Insert [Tag.V2].[dbo].[Members_EnAccount] ON; insert into [Tag.V2].[dbo].[Members_EnAcc ...
- web view调h5的方法死活调不到
(WebViewJavascriptBridge注册方法都能调用,只有callhandlename时无论如何也没响应)这个问题真是纠缠我好久了 webview评论区刷新问题终于找到原因了 ,我试着把咨 ...
- visual studio 2013 已停止运行 解决办法
情况是这样,WINDOWS 8.1,双显卡笔记本,打开VS.NET 没几秒就提示 已停止运行,然后就关闭程序了,找了无数资料都没有解决 -------------------------------- ...
- C#生成随机字符串(数字,字母,特殊符号)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...