洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解
P3088 [USACO13NOV]挤奶牛Crowded Cows
题目描述
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.
FJ有N(1 <= N <= 50,000)头奶牛沿着一维的栅栏吃草,第i头奶牛在目标点x(i) ,它的身高是 h(i) (1 <=x(i),h(i) <= 1,000,000,000)。
当一头奶牛左边D距离内而且右边D距离内有身高至少是它的两倍的奶牛,t (1 <= D <= 1,000,000,000),它就会觉得拥挤。
请计算觉得拥挤的奶牛的数量。
输入格式
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.
输入输出样例
输入 #1复制
6 4
10 3
6 2
5 3
9 7
3 6
11 2
输出 #1复制
2
说明/提示
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.
The cows at positions x=5 and x=6 are both crowded.
【思路】
单调队列
【题目分析】
每一头牛都有两边的d长度的的区间
只要两边的区间内都有大于等于这头牛两倍身高的牛
那他就会觉得拥挤
求觉得拥挤的牛的数量
【前缀思想】
有身高至少是它的两倍的奶牛?
这完全可以之比较最高的那头奶牛啊?
只要最高的比他的两倍高
那就至少是有的了
如果最高的不如他的两倍高
那一定没有比他两倍高的
所以比较最高的就可
区间最大值?
当然是单调队列
但是两个区间?
那就两个单调队列!
【最终思路】
顺着扫一遍处理出每一个点左边区间内
最高的那头牛
倒着扫一遍处理出每一个点右边区间内
最高的那头牛
(这里区间指的都是合法区间即长度小于等于d的区间)
然后在扫一遍每一个点
看看是不是两倍都有比自己的身高高两倍的牛
如果有那就计数
如果没有那就继续
【完整代码】
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<deque>
using namespace std;
const int Max = 50004;
struct node
{
int x;
int h;
}a[Max];
bool cmp(const node x,const node y)
{
return x.x < y.x;
}
deque<node>q1,q2;
int l[Max],r[Max];
int main()
{
// freopen("cow.in","r",stdin);
int n,d;
cin >> n >> d;
for(register int i = 1;i <= n;++ i)
cin >> a[i].x >> a[i].h;
sort(a + 1,a + 1 + n,cmp);
for(register int i = 1;i <= n;++ i)
{
while(!q1.empty() && a[i].x - q1.front().x > d)
q1.pop_front();
if(!q1.empty())
l[i] = q1.front().h;
else
l[i] = 0;
while(!q1.empty() && a[i].h > q1.back().h)
q1.pop_back();
q1.push_back(a[i]);
}
for(register int i = n;i >= 1;i --)
{
while(!q2.empty() && q2.front().x - a[i].x > d)
q2.pop_front();
if(!q2.empty())
r[i] = q2.front().h;
else
r[i] = 0;
while(!q2.empty() && a[i].h > q2.back().h)
q2.pop_back();
q2.push_back(a[i]);
}
int js = 0;
for(register int i = 1;i <= n;++ i)
if(l[i] >= a[i].h * 2 && r[i] >= a[i].h * 2)
js ++;
cout << js << endl;
return 0;
}
洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解的更多相关文章
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...
- 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)
题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...
- 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...
- 洛谷P2996 [USACO10NOV]拜访奶牛Visiting Cows
题目 树形dp 设f[i][j]表示走到第i号节点的最大权值 j为0/1表示这个点选或者不选 如果这个点不选 就从他的子树里的选或者不选选最大 如果这个点选 就加上他子树的不选 f[x][0] += ...
- 洛谷 P2996 [USACO10NOV]拜访奶牛Visiting Cows
P2996 传送门 题意: 给你一棵树,每一条边上最多选一个点,问你选的点数. 我的思想: 一开始我是想用黑白点染色的思想来做,就是每一条边都选择一个点. 可以跑两边一遍在意的时候染成黑,第二遍染成白 ...
随机推荐
- 记CentOS 发布.NET Core 2.0
centos 7.x sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c 'echo -e & ...
- Kafka重启出错:Corrupt index found
日志记录 FATAL Fatal error during KafkaServerStable startup. Prepare to shutdown (kafka.server.KafkaServ ...
- java之aop
1.AOP:aspect orientied programming 面向切面编程.就是横向编程. 2.面向切面编程是在不改变原有代码的情况下增加新的功能. 3.在spring中面向切面编程有两种应用 ...
- 在ASP.NET MVC中加载部分视图的方法及差别
在视图里有多种方法可以加载部分视图,包括Partial() .Action().RenderPartial().RenderAction().RenderPage()方法.下面说明一下这些方法的差别. ...
- npm全局模块卸载及默认安装目录修改方法
卸载全局安装模块 npm uninstall -g <package> 卸载后,你可以到 /node_modules/ 目录下查看包是否还存在,或者使用以下命令查看:npm ls npm ...
- 【MFC】在CHtmlView中在同一窗口显示新打开页面
使用MFC的单文档,用IE核心做的简单浏览器.当打开一个新的链接时,IE核心会使用IE来打开一个新窗口显示打开的新页面.为了让新页面在本程序中显示,我试了如下方法,其中的问题一并列出: 方法1.重载C ...
- 使用TP5验证器遇到的坑
项目中需要对字段进行验证,本人使用的是控制器验证方式.话不多说,直接上报错信息: SQLSTATE[42S02]: Base table or view not found: 1146 Table ' ...
- restframework框架写api中的个人理解以及碰到的问题
1.明确处理对象,在restframework的处理过程当中,如果是针对model写视图的话,queryset是要待展示的对象集,serializer_class是对每一个对象的所要使用的处理方式. ...
- sphinx中文版Coreseek中文检索引擎安装和使用方法(Linux)
sphinx中文版Coreseek中文检索引擎安装和使用方法(Linux) 众所周知,在MYSQL数据库中,如果你在百万级别数据库中使用 like 的话那你一定在那骂娘,coreseek是一个 ...
- 自动化运维-Ansible-playbook
Ansible Playbook https://ansible-tran.readthedocs.io/en/latest/docs/playbooks_intro.html Ansible中文网址 ...