2191: 【USACO】Crowded Cows

15 篇文章 0 订阅

2191: 【USACO】Crowded Cows

时间限制: 1.000 Sec  内存限制: 64 MB
提交: 26  解决: 19
[命题人:][下载数据: 90]

提交状态报告

题目描述

Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(i) and has height h(i) (1 <= x(i),h(i) <= 1,000,000,000). A cow feels "crowded" if there is another cow at least twice her height within distance D on her left, and also another cow at least twice her height within distance D on her right (1 <= D <= 1,000,000,000). Since crowded cows produce less milk, Farmer John would like to count the number of such cows. Please help him.

输入

* Line 1: Two integers, N and D. * Lines 2..1+N: Line i+1 contains the integers x(i) and h(i). The locations of all N cows are distinct.

输出

* Line 1: The number of crowded cows.

样例

输入  复制

6 4 10 3 6 2 5 3 9 7 3 6 11 2

输出  复制

2

提示

INPUT DETAILS: There are 6 cows, with a distance threshold of 4 for feeling crowded. Cow #1 lives at position x=10 and has height h=3, and so on. OUTPUT DETAILS: The cows at positions x=5 and x=6 are both crowded.

来源/分类

USACO 2013 November Silver 

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

struct Cow {
    long long x , hi;
    bool operator < (const Cow &a) const {
        return hi > a.hi;
    }
};
Cow tag[50005];
long long n;
long long d , ans = 0;

long long myabs(long long x) {
    return x>0?x:(-x);
}

int main() {
    scanf("%d%lld",&n,&d);
    for(long long i=1; i<=n; i++)
        scanf("%lld%lld",&tag[i].x,&tag[i].hi);
    sort(tag+1 , tag+n+1);
    for(long long i=1; i<=n; i++) {
        long long flag1 = 0 , flag2 = 0;
        for(long long j=1; j<i; j++) {
            if(tag[j].hi < tag[i].hi*2)
                break;
            if(myabs(tag[j].x-tag[i].x) <= d) {
                if(tag[j].x > tag[i].x)
                    flag1 = 1;
                if(tag[j].x < tag[i].x)
                    flag2 = 1;
                if(flag1 && flag2) {
                    ans++;
                    break;
                }
            }
        }   
    }
    printf("%lld",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值