zoukankan      html  css  js  c++  java
  • Alyona and mex

    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.

    分析:贪心,分析可知答案为最小区间长度,然后数组循环放就行;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define intxt freopen("in.txt","r",stdin)
    const int maxn=1e5+10;
    using namespace std;
    int gcd(int p,int q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,ans;
    int main()
    {
        int i,j;
        ans=1e9;
        scanf("%d%d",&n,&m);
        rep(i,1,m)scanf("%d%d",&j,&k),ans=min(ans,k-j+1);
        printf("%d
    ",ans);
        rep(i,1,n)
        {
            printf("%d ",(i-1)%ans);
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    HTML5新标签与特性---多媒体
    HTML5新标签与特性---新表单+新属性----综合案例1
    字体图标引入到HTML---复制用代码
    字体图标网站---常用汇总
    滑动门出现的背景---实例微信导航栏(a盒子里面包span盒子,文字写在span里)
    【Web前端开发】---前端培训roadmap
    清除浮动的4种方法
    进度更新---Responsive Web Design Certification (300 hours)
    Python实现一个桌面版的翻译工具【新手必学】
    Python爬虫老是被封的解决方法【面试必问】
  • 原文地址:https://www.cnblogs.com/dyzll/p/6102914.html
Copyright © 2011-2022 走看看