3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 111 Solved: 79
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
6 4 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
2 OUTPUT DETAILS: The cows at positions x=5 and x=6 are both crowded.
HINT
Source
题解:一个萌萌哒RMQ问题(HansBug:吐槽下翻译——输入里面明明说了the location is distinct,也就是说每只牛坐标唯一,这样子虽然没有实质性变化,但是简化了不少问题,毕竟题目说是左边和右边的牛,没说此位置上的= =),将坐标排序,然后就是区间查询最大值啦,然后没了
1 /**************************************************************
2 Problem: 3314
3 User: HansBug
4 Language: Pascal
5 Result: Accepted
6 Time:608 ms
7 Memory:6008 kb
8 ****************************************************************/
9
10 var
11 i,j,k,l,m,n:longint;
12 a,b:array[0..100000,1..2] of longint;
13 c:array[0..17,0..60000] of longint;
14 function max(x,y:longint):longint;
15 begin
16 if x>y then max:=x else max:=y;
17 end;
18 procedure swap(var x,y:longint);
19 var z:longint;
20 begin
21 z:=x;x:=y;y:=z;
22 end;
23 procedure sort(l,r:longint);
24 var i,j,x:longint;
25 begin
26 i:=l;j:=r;x:=a[(l+r) div 2,1];
27 repeat
28 while a[i,1]<x do inc(i);
29 while a[j,1]>x do dec(j);
30 if i<=j then
31 begin
32 swap(a[i,1],a[j,1]);
33 swap(a[i,2],a[j,2]);
34 inc(i);dec(j);
35 end;
36 until i>j;
37 if i<r then sort(i,r);
38 if l<j then sort(l,j);
39 end;
40 function getmax(x,y:longint):longint;
41 var i:longint;
42 begin
43 if y<x then exit(-1);
44 i:=trunc(ln(y-x+1)/ln(2));
45 exit(max(c[i,x],c[i,y-trunc(exp(ln(2)*i))+1]));
46 end;
47 begin
48 readln(n,m);
49 for i:=1 to n do readln(a[i,1],a[i,2]);
50 sort(1,n);
51 for i:=1 to n do c[0,i]:=a[i,2];
52 for i:=1 to trunc(ln(n)/ln(2)+1) do
53 for j:=1 to n-trunc(exp(ln(2)*i))+1 do
54 c[i,j]:=max(c[i-1,j],c[i-1,j+trunc(exp(ln(2)*(i-1)))]);
55 k:=1;fillchar(b[1],sizeof(b[1]),0);
56 for i:=2 to n do
57 begin
58 while (a[k,1]+m)<a[i,1] do inc(k);
59 if getmax(k,i-1)>=(a[i,2]*2) then b[i,1]:=1;
60 end;
61 k:=n;fillchar(b[1],sizeof(b[1]),0);
62 for i:=n-1 downto 1 do
63 begin
64 while (a[k,1]-m)>a[i,1] do dec(k);
65 if getmax(i+1,k)>=(a[i,2]*2) then b[i,2]:=1;
66 end;
67 l:=0;for i:=1 to n do inc(l,(b[i,1]+b[i,2]) div 2);
68 writeln(l);
69 readln;
70
71 end.
- 今天,你有微信小游戏提交审核吗?
- 云计算,迷你版线程同步
- Kubernetes 年度关键进展回顾
- 比较一下以“反射”和“表达式”执行方法的性能差异
- 人工智能芯片是什么?有什么用?
- 柯洁食言“复出”再战AI:明年4月见分晓
- 深入探讨ASP.NET MVC的筛选器
- pytorch自然语言处理之Pooling层的句子分类
- su命令cannot set groups: Operation not permitted的解决方法
- 利用腾讯云 COS 云对象存储定时远程备份网站
- 腾讯云技术公开课:零基础入门高可用云端架构设计
- 包学会之浅入浅出Vue.js:开学篇
- 包学会之浅入浅出Vue.js:升学篇
- 一个只有99行代码的JS流程框架 (一)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 一个有意思的方案:不借助后台和 JS ,只用 CSS 让一个列表编号倒序,你会怎么做?
- 关于MySQL LOAD DATA特性的利用与思考
- NHibernate 缓存
- 渗透测试 | 突破前端JS加密限制
- 使用 Task 简化异步编程
- Task 编程中的异常处理
- AngularJS 中的 factory、 service 和 provider
- 设计模式之适配器模式
- 设计模式之组合模式
- MvvmCross 框架中的数据绑定语法
- Vivado IP核锁定的解除方法
- Flutter Dojo设计之道——骚气的闪屏动画是如何实现的
- 数据处理 | pandas入门专题——离散化与one-hot
- 聊聊dubbo-go的failfastCluster
- 一分钟学Python|Python的循环语句