zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #6 (Div. 2 Only) E. Exposition multiset

    E. Exposition

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/problemset/problem/6/E

    Description

    There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than k millimeters.

    The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task.

    Input

    The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters.

    Output

    In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters.

    In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work.

    Sample Input

    3 3
    14 12 10

    Sample Output

    2 2
    1 2
    2 3

    HINT

    题意

    给你一堆数,让你找到最长的区间,使得这个区间里面的最大值减去最小值不超过K,然后让你输出每一个区间的起始位置和结束为止

    题解:

    类似于双端队列的写法,我们用multiset来处理这个问题,如果当前区间不合法,那么我们不断删去起始端的数就好了

    然后不断跑,O(n) 

    代码

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 1000010
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    inline ll read()
    {
        int x=0,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;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int a[maxn];
    vector< pair<int,int> > q;
    multiset<int> s;
    int main()
    {
        int n=read(),k=read();
        for(int i=0;i<n;i++)
            a[i]=read();
        int j=0;
        int aa=-1;
        for(int i=0;i<n;i++)
        {
            s.insert(a[i]);
            while(*s.rbegin()-*s.begin()>k)
                s.erase(s.find(a[j++]));
            if(i-j+1>aa)
            {
                aa=i-j+1;
                q.clear();
            }
            if(i-j+1==aa)
                q.push_back(make_pair(j+1,i+1));
        }
        printf("%d %d
    ",aa,q.size());
        for(int i=0;i<q.size();i++)
            printf("%d %d
    ",q[i].first,q[i].second);
    }
  • 相关阅读:
    VSTO不能创建OFFICE 文档项目的原因
    vs2016 创建 vsto excel 文件项目的一个问题
    一个开发原则:永远不要返回NULL
    客户为什么习惯变更需求
    从实际项目中的一个改进细节谈程序的易用性优化
    第三方系统打开EAFC的实现
    功能间(两个form)数据交互的编程方法
    关于行军模式大批量数据的审批的实现
    程序的升级发布管理
    转:从如何判断浮点数是否等于0说起——浮点数的机器级表示 献给依然 if ( double i ==0.00)的菜鸟们
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4595525.html
Copyright © 2011-2022 走看看