【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define N 50010 6 using namespace std; 7 struct data 8 { 9 int x,h; 10 }a[N],q[N]; 11 int n,m,l,r; 12 int ok1[N],ok2[N]; 13 inline int read() 14 { 15 int f=1,ans=0; 16 char c; 17 while (!isdigit(c=getchar())) if (c=='-') f=-1; 18 ans=c-'0'; 19 while (isdigit(c=getchar())) ans=ans*10+c-'0'; 20 return ans*f; 21 } 22 bool cmp(data a,data b) {return a.x<b.x;} 23 int main() 24 { 25 n=read(); m=read(); 26 for (int i=1;i<=n;i++) a[i].x=read(),a[i].h=read(); 27 sort(a+1,a+n+1,cmp); 28 l=1; r=0; 29 for (int i=1;i<=n;i++) 30 { 31 while (l<=r && q[r].h<a[i].h) r--; 32 q[++r]=a[i]; 33 while (l<=r && q[l].x<a[i].x-m) l++; 34 if (q[l].h>=2*a[i].h) ok1[i]=1; 35 } 36 l=1; r=0; 37 for (int i=n;i>=1;i--) 38 { 39 while (l<=r && q[r].h<a[i].h) r--; 40 q[++r]=a[i]; 41 while (l<=r && q[l].x>a[i].x+m) l++; 42 if (q[l].h>=2*a[i].h) ok2[i]=1; 43 } 44 int ans=0; 45 for (int i=1;i<=n;i++) 46 if (ok1[i]&&ok2[i]) ans++; 47 printf("%d\n",ans); 48 return 0; 49 }
View Code
Description
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.
N头牛在一个坐标轴上,每头牛有个高度。现给出一个距离值D。
如果某头牛在它的左边,在距离D的范围内,如果找到某个牛的高度至少是它的两倍,且在右边也能找到这样的牛的话。则此牛会感觉到不舒服。
问有多少头会感到不舒服。
Input
* 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.
Output
* Line 1: The number of crowded cows.
Sample Input
10 3
6 2
5 3
9 7
3 6
11 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.
Sample Output
OUTPUT DETAILS: The cows at positions x=5 and x=6 are both crowded.
HINT
Source
Silver
转载于:https://www.cnblogs.com/DMoon/p/5422650.html
【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列相关推荐
- 单调队列(P3088 Crowded Cows S)
题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i ...
- [DP/单调队列]BZOJ 2059 [Usaco2010 Nov]Buying Feed 购买饲料
首先我想吐槽的是题目并没有表明数据范围... 这个题目 DP方程并不难表示. dp[i][j]表示前i个地点携带了j个货物的最小花费 dp[i][j] = dp[i-1][k] + (j-k) * c ...
- Buying Feed, 2010 Nov (单调队列优化DP)
约翰开车回家,又准备顺路买点饲料了(咦?为啥要说"又"字?)回家的路程一共有 E 公里, 这一路上会经过 K 家商店,第 i 家店里有 Fi 吨饲料,售价为每吨 Ci 元.约翰打算 ...
- Crowded Cows(洛谷P3088)
[USACO13NOV]CrowdedCowsS[USACO13NOV]Crowded Cows S[USACO13NOV]CrowdedCowsS 传送门 思路 先看数据, 对于 202020 %的 ...
- P3088 [USACO13NOV]CROWDED COWS S
https://www.luogu.com.cn/problem/P3088 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazin ...
- [USACO13NOV]挤奶牛Crowded Cows(洛谷 P3088)
题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i ...
- 【POJ - 2373】Dividing the Path(单调队列优化dp)
题干: Farmer John's cows have discovered that the clover growing along the ridge of the hill in his fi ...
- POJ 3250 Bad Hair Day【单调队列】
Description Some of Farmer John'sNcows (1 ≤N≤ 80,000) are having a bad hair day! Since each cow is s ...
- HDU 4122 Alice's mooncake shop 单调队列优化dp
Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
最新文章
- 像素颜色JavaFX示例--简易图片处理工具
- 预测 “疯狂三月” 冠军的办法,我只告诉你!
- setTimeout 定时器的使用
- BZOJ.3277.串(广义后缀自动机)
- 前台html调用函数 格式化输出
- hdu 4533(树状数组区间更新+单点查询)
- std::bind绑定成员函数,为什么第二个参数必须绑定对象地址
- linux开发板上程序如何调试,linux开发板调试典型方法
- 全国职业院校技能大赛软件测试题目,我院荣获2017年全国职业院校技能大赛软件测试赛项一等奖...
- 计算机应用技术环境评估,计算机应用教程(第7版)(Windows 7与Office 2007环境)习题解答与上机练习...
- php多进程并发,php多进程模拟并发事务
- Visual Studio控制台程序输出窗口一闪而过的解决方法
- Openstack Swift 原理、架构与 API 介绍
- java获取汉字的拼音首字母_java获取汉字的拼音首字母
- 收藏!示波器探头的选择与使用
- 说说3D打印培训课程在我国的开展的现状——3D打印培训宁波清车
- C# 客户端使用Excel批量导入数据
- 16 服务器无响应什么意思,dns电脑服务器未响应是什么意思如何处理
- 【§炫彩苹果win7主题§】
- 如何将这个递归调用停下来呢?
热门文章
- python爬虫教程下载-Python爬虫入门教程!手把手教会你爬取网页数据
- 记事本写python怎么运行-Python开发简单记事本
- 自学python要看哪些书籍-学习Python编程的最好的7本书
- 用python画漂亮图片-python结合G2绘制精美图形
- python比c语言好学吗-C 和 Python语言先学哪个好?
- python百度网盘-bypy-百度网盘Python客户端 linux
- 小学生python入门-如何看待小学生开始学Python?
- python装饰器实例-Python装饰器原理与简单用法实例分析
- python英语单词大全-使用Python进行英文单词分割
- python3入门到精通pdf-Python3入门到精通实战特训