BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314
题意:
N头牛在一个坐标轴上,每头牛有个高度。现给出一个距离值D。
如果某头牛在它的左边,在距离D的范围内,如果找到某个牛的高度至少是它的两倍,且在右边也能找到这样的牛的话。则此牛会感觉到不舒服。
问有多少头会感到不舒服。
题解:
从左到右、从右到左两遍单调队列。
单调性:
(1)坐标x递增。
(2)高度h递减。
维护单调性:
(1)从队首开始,所有与当前牛i的距离超过d的,以后都不会再用到。
(2)从队尾开始,所有高度 <= 当前高度h[i]的,都不会再用到,因为当前牛i一定比前面的更优(又高又近)。
(3)最后再将i压入队尾。
每次判断一下之前最高的牛(队首)是不是h[i]的两倍,如果是则cnt[i]++。
最后统计一下cnt[i] == 2的个数就好。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAX_N 50005 using namespace std; struct Data
{
int x;
int h;
Data(int _x,int _h)
{
x=_x;
h=_h;
}
Data(){}
friend bool operator < (const Data &a,const Data &b)
{
return a.x<b.x;
}
}; int n,d;
int head;
int tail;
int ans=;
int q[MAX_N];
int cnt[MAX_N];
Data dat[MAX_N]; void read()
{
cin>>n>>d;
for(int i=;i<n;i++)
{
cin>>dat[i].x>>dat[i].h;
}
} void solve()
{
sort(dat,dat+n);
memset(cnt,,sizeof(cnt));
head=;
tail=;
for(int i=;i<n;i++)
{
while(head<tail && dat[i].x-dat[q[head]].x>d) head++;
while(head<tail && dat[q[tail-]].h<=dat[i].h) tail--;
if(head<tail && dat[q[head]].h>=dat[i].h*) cnt[i]++;
q[tail++]=i;
}
head=;
tail=;
for(int i=n-;i>=;i--)
{
while(head<tail && dat[q[head]].x-dat[i].x>d) head++;
while(head<tail && dat[q[tail-]].h<=dat[i].h) tail--;
if(head<tail && dat[q[head]].h>=dat[i].h*) cnt[i]++;
q[tail++]=i;
}
for(int i=;i<n;i++)
{
if(cnt[i]==) ans++;
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列的更多相关文章
- BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )
从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- 3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 111 Solved: 79[Sub ...
- 【BZOJ】3314: [Usaco2013 Nov]Crowded Cows(单调队列)
http://www.lydsy.com/JudgeOnline/problem.php?id=3314 一眼就是维护一个距离为d的单调递减队列... 第一次写.....看了下别人的代码... 这一题 ...
- BZOJ3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 86 Solved: 61[Subm ...
- BZOJ : [Usaco2013 Nov]Crowded 单调队列
正反两遍个来一次单调队列 DP 即可. Code: #include<cstdio> #include<deque> #include<algorithm> usi ...
- 【bzoj 1414】对称的正方形 单调队列+manacher
Description Orez很喜欢搜集一些神秘的数据,并经常把它们排成一个矩阵进行研究.最近,Orez又得到了一些数据,并已经把它们排成了一个n行m列的矩阵.通过观察,Orez发现这些数据蕴涵了一 ...
- bzoj 1047 : [HAOI2007]理想的正方形 单调队列dp
题目链接 1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2369 Solved: 1266[Submi ...
- BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )
树形dp..水 ------------------------------------------------------------------------ #include<cstdio& ...
随机推荐
- 防止vue组件渲染不更新
1.key <el-dialog title="" :visible.sync="dialogVisible" @close="dialogCl ...
- EasyUI这个框架用了好久了,总结一下遇到的问题和解决方法
1. jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法: 我们之所以在页面中,只要书写相应easyui的class,Easyui就能成功渲染页面,这是因为解析器在 ...
- HDU 3461 Code Lock(并查集的应用+高速幂)
* 65536kb,仅仅能开到1.76*10^7大小的数组. 而题目的N取到了10^7.我開始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 事 ...
- Git分支中的远程操作实践
Git分支中的远程操作实践 前几篇博客陆陆续续的讲了好多关于Git操作的内容, 其中在上篇博客聊了<Git中的merge.rebase.cherry-pick以及交互式rebase>,本篇 ...
- zoj 2068 - Chopsticks
题目:非常多人在一起吃饭.有两组单支的筷子,定义badness为一对筷子长度差的平方,求最小的badness和. 分析:dp,最大公共子序列类似物. 这里利用数学关系找到一个结论: a < b ...
- ABAP 弹出框 函数
POPUP_GET_VALUES_USER_HELP 是一个和用户交互信息的函数,用户能够填写信息,并且我们还能够依据实际的需求对弹出框进行F1 F4 以及用户的需求进行增强.具体的实现能够參考系统标 ...
- TensorFlowSharp
https://github.com/migueldeicaza/TensorFlowSharp
- mongo数据库中一条记录中某个属性是数组情形时 数据结构的定义
package entity; import java.util.Date; import com.mongodb.BasicDBList;import com.mongodb.DBObject; p ...
- wepy/packages/wepy-web/src/helper/device.js
wepy/packages/wepy-web/src/helper/device.js https://github.com/Tencent/wepy/blob/bd0003dca2bfb958113 ...
- multiple-value uuid.NewV4() in single-value context
[root@t ~]# go get github.com/aliyun/aliyun-sts-go-sdk/sts# github.com/aliyun/aliyun-sts-go-sdk/sts/ ...