线段树 BZOJ3888 [Usaco2015 Jan]Stampede

解析 USACO 2015 Jan 赛事中的 Stampede 问题,介绍如何通过线段树来确定 Farmer John 在奶牛赛跑过程中能够看到的奶牛数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3888: [Usaco2015 Jan]Stampede

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 253  Solved: 81
[Submit][Status][Discuss]

Description

Farmer John's N cows (1 <= N <= 50,000) appear to be stampeding along the road at the front of FJ's farm, but they are actually just running in a foot race to see which cow is the fastest. Viewed from above, each cow is represented by a unit-length horizontal line segment, specified by the coordinates of its left corner point at time t=0. For example, (-3,6) would specify a cow who at time t=0 is represented by the segment from (-3,6) to (-2,6). Each cow is moving to the right (in the +x direction) at a certain rate, specified by the integer amount of time it takes her to move 1 unit to the right. FJ is not particularly thrilled that his cows are outside running instead of in the barn producing milk. He plans to admonish them with a stern lecture after the race ends. In order to determine which of his cows are participating in the race, FJ situates himself at (0,0) and looks along a ray extending in the +y direction. As the race unfolds, FJ sees a cow if she is ever the first cow visible along this ray. That is, a cow might not be visible if another cow is "in front" of her during the entire time she crosses FJ's line of sight. Please compute the number of cows FJ can see during the entire race.
PoPoQQQ站在原点上向y轴正半轴看,然后有一群羊驼从他眼前飞过。这些羊驼初始都在第二象限,尾巴在(Xi,Yi),头在(Xi+1,Yi),每Ci秒向右走一个单位。 PoPoQQQ能看见一匹羊驼当且仅当它身体任意某部位x坐标为0时,没有其它y坐标小于此羊驼的羊驼身体某部位x坐标为0。 问PoPoQQQ能看见几匹羊驼?

Input

The first line of the input contains N. Each of the following N lines describes a cow with three integers x y r, corresponding to a cow whose left endpoint is at (x,y) at time t=0, moving to the right at a continuous speed of 1 unit of distance every r units of time. The value of x is in the range -1000..-1, the value of y is in the range 1..1,000,000 (and distinct for every cow, to prevent any possible collisions), and the value of r is in the range 1..1,000,000.

Output

A single integer, specifying the number of cows FJ can see during the entire race (from t=0 onward).

Sample Input

3
-2 1 3
-3 2 3
-5 100 1

Sample Output

2

SOLUTION NOTES:

FJ can see cows 1 and 2 but not cow 3.

HINT

谨记:

  粗心引起的错误,浪费掉数小时人生。——sdfzyny

 

这个题和poj一个叫做star的题好像,把题意看穿,紧握影响答案的因素……

说的什么屁话,路过大神不要嘲讽

某一时刻仅能看到1只羊驼,那么只要查询这个时刻有没有羊驼就好啦

首先自然想到按y坐标排序

然后可以先自己举几个样例,发现此题羊驼的y坐标影响并不是多效的……又说了句屁话QAQ

神奇的感觉到每个羊驼占(zhi)着(pei)观察者的眼睛的时间段似乎好、可以用线段树实现

根据题意把时间离散一下做,时间是个坏孩子……

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<map>
 6 using namespace std;
 7 int n,size,cnt,ans;
 8 struct dt{
 9     int l,r,h;
10 }yt[60010];
11 int f[200010]; 
12 map<int,int>mp;
13 bool cmp(const dt&aa,const dt&bb){
14     return aa.h<bb.h;
15 }
16 int segtree[400010];
17 int ask(int pl,int pr,int pos,int ll,int rr){
18     if(pl>rr||pr<ll) return 0;
19     if(segtree[pos]) return 0;
20     //if(pl==pr) return 0;手残!!! 
21     if(pl<=ll&&pr>=rr){
22         segtree[pos]=1;
23         return 1;
24     }
25     int mid=(ll+rr)>>1;
26     int p1=0,p2=0;
27     if(pl<=mid) p1=ask(pl,pr,pos<<1,ll,mid);
28     if(pr>mid) p2=ask(pl,pr,pos<<1|1,mid+1,rr);
29     segtree[pos]=segtree[pos]|(segtree[pos<<1]&segtree[pos<<1|1]);
30     return p1|p2;
31 }
32 int main(){
33     scanf("%d",&n);
34     f[0]=-1;
35     int x,t;
36     for(int i=1;i<=n;i++){
37         scanf("%d%d%d",&x,&yt[i].h,&t);
38         yt[i].l=(-x-1)*t;
39         yt[i].r=-x*t;
40         f[++cnt]=yt[i].l;
41         f[++cnt]=yt[i].r;
42     }
43     sort(yt+1,yt+n+1,cmp);
44     sort(f+1,f+cnt+1);
45     for(int i=1;i<=cnt;i++)
46         if(f[i]!=f[i-1]) mp[f[i]]=++size;
47     for(int i=1;i<=n;i++){
48         yt[i].l=mp[yt[i].l];
49         yt[i].r=mp[yt[i].r]-1;
50     }
51     for(int i=1;i<=n;i++) ans+=ask(yt[i].l,yt[i].r,1,1,size);
52     printf("%d",ans);
53     return 0; 
54 }

 

转载于:https://www.cnblogs.com/sdfzxh/p/7048937.html

好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值