BZOJ 3314 [Usaco2013 Nov]Crowded Cows

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314

题意:略。

思路:单调队列或用堆维护当前最大值,左右两边扫一下,如果满足条件就打个标记,最后看是否标记数为2就好了。

单调队列:

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
inline int add(int a,int b){return (a+=b)>=P?a-P:a;}
inline int sub(int a,int b){return (a-=b)<0?a+P:a;}
inline int mul(int a,int b){return 1LL*a*b%P;}
struct Node{
    int pos,h;
    bool operator<(const Node&a)const{
        return h<a.h;
    }
}a[maxn],q[maxn];
int n,d,c[maxn];
//priority_queue<Node>Q;
inline bool cmp(const Node&a,const Node&b){return a.pos<b.pos;}
int main(){
    read(n),read(d);
    for (int i=1;i<=n;i++) read(a[i].pos),read(a[i].h);
    sort(a+1,a+1+n,cmp);
    int l=1,r=0;
    for (int i=1;i<=n;i++){
        while (l<=r&&q[r].h<a[i].h) r--;
        q[++r]=a[i];
        while (l<=r&&q[l].pos+d<a[i].pos) l++;
        if (q[l].h>=(a[i].h<<1)) c[i]++;
    }
    l=1,r=0;
    for (int i=n;i>=1;i--){
        while (l<=r&&q[r].h<a[i].h) r--;
        q[++r]=a[i];
        while (l<=r&&q[l].pos-d>a[i].pos) l++;
        if (q[l].h>=(a[i].h<<1)) c[i]++;
    }
    int ans=0;
    for (int i=1;i<=n;i++) if (c[i]==2) ans++;
    printf("%d\n",ans);
    return 0;
}

堆:

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
inline int add(int a,int b){return (a+=b)>=P?a-P:a;}
inline int sub(int a,int b){return (a-=b)<0?a+P:a;}
inline int mul(int a,int b){return 1LL*a*b%P;}
struct Node{
    int pos,h;
    bool operator<(const Node&a)const{
        return h<a.h;
    }
}a[maxn],q[maxn];
int n,d,c[maxn];
priority_queue<Node>Q;
inline bool cmp(const Node&a,const Node&b){return a.pos<b.pos;}
int main(){
    read(n),read(d);
    for (int i=1;i<=n;i++) read(a[i].pos),read(a[i].h);
    sort(a+1,a+1+n,cmp);
    for (int i=1;i<=n;i++){
        while (!Q.empty()&&Q.top().pos+d<a[i].pos) Q.pop();
        if (!Q.empty()&&Q.top().h>=(a[i].h<<1)) c[i]++;
        Q.push(a[i]);
    }
    while (!Q.empty())Q.pop();
    for (int i=n;i>=1;i--){
        while (!Q.empty()&&Q.top().pos-d>a[i].pos) Q.pop();
        if(!Q.empty()&&Q.top().h>=(a[i].h<<1)) c[i]++;
        Q.push(a[i]);
    }
    int ans=0;
    for (int i=1;i<=n;i++) if (c[i]==2) ans++;
    printf("%d\n",ans);
    return 0;
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注