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 ...
随机推荐
- 彻底弄懂Activity四大启动模式
最近有几位朋友给我留言,让我谈一下对Activity启动模式的理解.我觉得对某个知识点的理解必须要动手操作才能印象深刻,所以今天写一篇博文,结合案例理解Activity启动模式.由于之前看过" ...
- Android Handler 最佳的理解资料
- EasyUI 开发笔记(一)
由于某些原因,在公司做的后台需要改成类似于Ext.js 形式的后台,主要看好其中的 框架布局,以及tab开页面和弹出式内部窗体. 后来看看,改成EasyUI,较Ext.js 库小很多,也便于公司的初级 ...
- check_pkg函数解析
函数位置,conf/core,具体代码如下: check_pkg() { # Usage: check_pkg <command> <package> # It means: ...
- SDK截图(四):压缩位图实例
这个问题研究了两天.<windows程序设计>中没有给出实例,MSDN也没有给出具体的例子.在知道,CSDN提问后,也没有得到答案.所以决定重新对DIB做一次彻底的研究,这大概会花去我一个 ...
- 3G数据请求
// 该类负责发送2G/3G Http请求的数据 #import <Foundation/Foundation.h> #import "ASIHTTPRequest.h&quo ...
- cocos2d3.x在android下屏蔽多点触控
ios上很简单的在AppController.mm里 [eaglView setMultipleTouchEnabled:YES] 设置为NO,就是单点触控了,无需更改cocos底层代码; andro ...
- Azure CDN Smart Routing 功能正式上线
经过Azure中国CDN产品团队的不懈努力,融合CDN服务的一个重要功能 Smart Routing目前已经正式上线.在接下来的一段时间该功能将逐步开放给所有客户.本文主要对Smart Routing ...
- 【转】 TechED2010与我(三) —— 初识云计算
作者用到的比喻很好. 网址:TechED2010与我(三) -- 初识云计算 初识云计算最近"云计算"被炒的很热,但是由于工作重点主要是做WinForm的控件开发,对云计算的接触比 ...
- i5 6300HQ 和 i7 6700HQ
i7 6700HQ: 四核八线程 14nm 45W 2.6-3.5GHz 支持DDR4 2133内存 三级缓存6M HD Graphics 530集显 i5 6300HQ: 四核四线程 14nm 45 ...