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.
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].
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.
5 3 1 3 2 5 4 5
2 1 0 2 1 0
4 2 1 4 2 4
3 5 2 0 1
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.
题目链接:http://codeforces.com/contest/740/problem/C
啊啊啊!!!好不容易才读懂的题意,发现应该可以过,然后输出最大的最小mex出来了,那个队列怎么也出不来了,然后看了下别人的代码。
取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!取余!!!
果然我还是太渣了!!!
我的wa代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[20000];
int vis[20000];
struct node{
int x,y;
}xin[20000];
bool cmp(struct node a,struct node b){
return a.y-a.x<b.y-b.x;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++){
scanf("%d%d",&xin[i].x,&xin[i].y);
}
sort(xin,xin+m,cmp);
printf("%d\n",xin[0].y-xin[0].x+1);
int k=xin[0].y-xin[0].x+1;
for(int i=0;i<=n;i++){
a[i]=k;
}
for(int i=0;i<m;i++){//亏我想了半天!!!各种姿势,想哭Q^Q
int top=0;
for(int j=xin[i].x;j<=xin[i].y;j++){
if(!vis[j]){
if(top==k)
break;
vis[j]=1;
a[j]=top++;
}
}
}
for(int i=1;i<=n;i++){
printf(i==1?"%d":" %d",a[i]);
}
cout<<endl;
return 0;
}
渣渣的正确代码(未交,赛后看题解打的)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[20000];
int vis[20000];
struct node{
int x,y;
}xin[20000];
bool cmp(struct node a,struct node b){
return a.y-a.x<b.y-b.x;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++){
scanf("%d%d",&xin[i].x,&xin[i].y);
}
sort(xin,xin+m,cmp);
printf("%d\n",xin[0].y-xin[0].x+1);
int k=xin[0].y-xin[0].x+1;
for(int i=0;i<=n;i++){
a[i]=k;
}
for(int i=1;i<=n;i++){//只需要一个简单的取余!!!Q^Q
printf(i==1?"%d":" %d",i%k);
}
cout<<endl;
return 0;
}