Codeforces Round #381 (Div. 2)C(构造,思维)
2016-11-25 21:16
393 查看
C. Alyona and mex
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
题意:给你一个长度为n的非负数区间和m个子区间。定义mex为子区间未出现的最小正整数,问 最大的最小正整数是谁和构造出任意满足题意区间。
题解:
这个最大的最小正整数一定是最小区间长度+1,如何构造这个区间才是问题。想一下剩下子区间的长度都大于等于ans。我们只要将这些子区间从0—ans-1赋值即可。
代码:
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
题意:给你一个长度为n的非负数区间和m个子区间。定义mex为子区间未出现的最小正整数,问 最大的最小正整数是谁和构造出任意满足题意区间。
题解:
这个最大的最小正整数一定是最小区间长度+1,如何构造这个区间才是问题。想一下剩下子区间的长度都大于等于ans。我们只要将这些子区间从0—ans-1赋值即可。
代码:
#include <bits/stdc++.h> #define ll long long using namespace std; ll a[1000000]; int main() { int n,m,x,y,l,r; cin>>n>>m; int ans=1e120; memset(a,0,sizeof(a)); while(m--) { cin>>x>>y; if(y-x<ans) { ans=y-x; l=x; r=y; } } ans++; int tmp=0; printf("%d\n",ans); for(int i=1;i<=n;i++) { a[i]=tmp++; if(tmp==ans) tmp=0; } for(int i=1;i<=n;i++) { printf("%I64d ",a[i]); } }
相关文章推荐
- Codeforces Round #394 (Div. 2)E. Dasha and Puzzle【思维+Dfs构造】
- Codeforces Round #381 (Div. 2)C. Alyona and mex(构造题,好题)
- Codeforces Round 340 (Div 2)D Polyline 【枚举】【思维】
- Codeforces Round #324 (Div. 2) (584A,584B(排列组合),584C(构造),584D(哥德巴赫猜想),584E(置换群))
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
- Codeforces Round #253 (Div. 2) C Borya and Hanabi (思维+bitmask)
- Codeforces Round #257 (Div. 2)E(数论+构造)
- Codeforces Round #277 (Div. 2)(B)构造
- Codeforces Round #218 (Div. 2) E. Subway Innovation(思维 前缀和)
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D. The Door Problem【2-sat Tarjan+思维建图】
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
- Codeforces Round #345 (Div. 2) B 思维题
- Codeforces Round #394(Div. 2)B. Dasha and friends【思维+暴力】
- Codeforces Round #397(Div. 1 + Div. 2 combined)D. Artsem and Saunders【思维+乱搞】
- Codeforces Round #277.5(Div. 2) F. Special Matrices【思维+Dp】好题~好题~
- Codeforces Round #381 (Div. 2) A Alyona and copybooks
- Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
- Codeforces Round #426 (Div. 2) C. The Meaningless Game 思维 D. The Bakery dp
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(想法题,构造题)
- **Codeforces Round #282 (Div. 2) C. Treasure ACM解题报告(构造难题)