zoukankan      html  css  js  c++  java
  • [USACO12MAR]花盆Flowerpot

    传送门:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define re register
    #define fi first
    #define se second
    const int N=1e5+10;
    inline void read(int &a)
    {
        a=0;
        int d=1;
        char ch;
        while(ch=getchar(),ch>'9'||ch<'0')
            if(ch=='-')
                d=-1;
        a=ch^48;
        while(ch=getchar(),ch>='0'&&ch<='9')
            a=(a<<3)+(a<<1)+(ch^48);
        a*=d;
    }
    pair <int,int> q[N];
    int maxq[N],minq[N];
    int ans=0x3fffffff;
    int main()
    {
        int n,m;
        read(n);
        read(m);
        int h1,h2,t1,t2;
        h1=h2=1;
        t1=t2=1;
        for(re int i=1;i<=n;i++)
        {
            read(q[i].fi);
            read(q[i].se);
        }
        sort(q+1,q+1+n);
        for(int l=1,r=0;l<=n;l++)
        {
            while(h1<t1&&maxq[h1]<l) h1++;
            while(h2<t2&&minq[h2]<l) h2++;
            while(r<n&&q[maxq[h1]].se-q[minq[h2]].se<m)
            {
                r++;
                while(h1<t1&&q[maxq[t1-1]].se<=q[r].se)
                    t1--;
                maxq[t1++]=r;
                while(h2<t2&&q[minq[t2-1]].se>=q[r].se)
                    t2--;
                minq[t2++]=r;
            }
            if(q[maxq[h1]].se-q[minq[h2]].se>=m)
                ans=min(ans,q[r].fi-q[l].fi);
        }
        printf("%d",ans>=0x3fffffff?-1:ans);
        return 0;
    }
  • 相关阅读:
    Java vs Python
    Compiled Language vs Scripting Language
    445. Add Two Numbers II
    213. House Robber II
    198. House Robber
    276. Paint Fence
    77. Combinations
    54. Spiral Matrix
    82. Remove Duplicates from Sorted List II
    80. Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/acm1ruoji/p/11003195.html
Copyright © 2011-2022 走看看