码迷,mamicode.com
首页 > 其他好文 > 详细

花盆Flowerpot[USACO12MAR]

时间:2018-03-14 20:42:52      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:str   blank   open   std   inf   分享   flow   复杂度   pen   

  题意:

  平面上n个点,给出d,求最小的k使得长度为k在某个长度为k横坐标区间内的纵坐标的最大值与最小值之差超过d

 

  题解:

  如果做过poj2823这道题的话,这道题应该是挺好想的。(没做过的同学可以点这里

  我们可以二分长度k,然后就和上面这道题的操作一样了。复杂度是O(n*logn),1e5的数据完全没问题。

  但其实我们可以不用二分答案。

  我们假设区间[L1,R1]满足max-min>=d,且对于左端点来说,R1是满足条件的最左点。那么对于L2>L1,满足max-min>=d的区间[L2,R2]一定满足R2>=R1。(应该挺好想的吧qaq) 不过我们还是证明一下好了。

  我们假设R2<R1,那么既然[L2,R2]都满足条件了,所以[L1,R2]也一定是满足条件的,这与题设不符。

  所以我们从左到右枚举左端点,用单调队列维护区间纵坐标即可。

 

上代码:

技术分享图片
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> 
#include<queue>
#define LL long long
#define RI register int
using namespace std;
const int INF = 0x7ffffff ;
const int N = 1e5 + 10 ;

inline int read() {
    int k = 0 , f = 1 ; char c = getchar() ;
    for( ; !isdigit(c) ; c = getchar())
      if(c == -) f = -1 ;
    for( ; isdigit(c) ; c = getchar())
      k = k*10 + c-0 ;
    return k*f ;
}
struct point {
    int x, y ;
}p[N] ;
int n, d ;
deque<int>q1 ; // 单调递减序列维护最大值 
deque<int>q2 ; // 单调递增序列维护最小值
deque<int>q11 ;
deque<int>q22 ; 

inline bool cmp1(point s,point t) { return s.x < t.x ; }
int main() {
    n = read(), d = read() ;
    for(int i=1;i<=n;i++) {
        p[i].x = read(), p[i].y = read() ;
    }
    sort(p+1,p+n+1,cmp1) ;
    int res = 0, ans = INF ;
    int i = 0 ; // 右端点 
    for(int j=1;j<=n;j++) { // j维护左端点
        while(q1.size() && q11.front() < j) q1.pop_front(), q11.pop_front() ;
        while(q2.size() && q22.front() < j) q2.pop_front(), q22.pop_front() ;
        if(q1.size() && q1.size()) res = q1.front()-q2.front() ;
        else res = 0 ;
        while(res < d && i < n) {
            i++ ;
            while(q1.size() && q1.back() <= p[i].y) q1.pop_back(), q11.pop_back() ;
            while(q2.size() && q2.back() >= p[i].y) q2.pop_back(), q22.pop_back() ;
            q1.push_back(p[i].y), q11.push_back(i) ; q2.push_back(p[i].y), q22.push_back(i) ;
            res = q1.front()-q2.front() ;
        }
        if(res >= d) {
            ans = min(ans,p[i].x-p[j].x) ;
        }
    }
    if(ans == INF) printf("-1") ; 
    else printf("%d",ans) ;
    return 0 ;
}
View Code

 

——end ;

花盆Flowerpot[USACO12MAR]

标签:str   blank   open   std   inf   分享   flow   复杂度   pen   

原文地址:https://www.cnblogs.com/zub23333/p/8569713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!