列表

详情


NC24160. [USACO 2015 Jan S]Stampede

描述

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.

输入描述

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.

输出描述

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

示例1

输入:

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

输出:

2

说明:

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 87ms, 内存消耗: 9336K, 提交时间: 2020-09-19 12:46:48

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
const int M=50010;
int n;
struct cow
{
	int L,R,h;
}a[M];
int f[M*2],s;
int ans;
map<int,int> m;
bool cmp(cow a,cow b)
{
	return a.h<b.h;
}
struct node
{
	int L,R;
	bool vis;
}t[M*8+5];
void build(int pos,int L,int R)
{
	t[pos].L=L;
	t[pos].R=R;
	t[pos].vis=0;
	if(L==R)
	return;
	int mid=(L+R)>>1;
	build(pos<<1,L,mid);
	build(pos<<1|1,mid+1,R);
}
int query(int pos,int ll,int rr)
{
	if(t[pos].vis)
	return 0;
	int L=t[pos].L;
	int R=t[pos].R;
	if(ll<=L&&rr>=R)
	{
		t[pos].vis=1;
		return 1;
	}
	if(L==R)
	return 0;
	int mid=(L+R)>>1,res=0;
	if(rr<=mid)
	res=query(pos<<1,ll,rr);
	else if(ll>mid)
	res=query(pos<<1|1,ll,rr);
	else
	res=query(pos<<1,ll,rr)|query(pos<<1|1,ll,rr);
	t[pos].vis=t[pos].vis|(t[pos<<1].vis&t[pos<<1|1].vis);
	return res;
}
int main()
{
	f[0]=-1;
	scanf("%d",&n);
	int x,y,c,cnt=0;
	for(int i=1;i<=n;++i)
	{
		scanf("%d%d%d",&x,&y,&c);
		a[i].h=y;
		a[i].L=(-x-1)*c;
		a[i].R=(-x)*c;
		f[++cnt]=a[i].L;
		f[++cnt]=a[i].R;
	}
	sort(a+1,a+1+n,cmp);
	sort(f+1,f+1+cnt);
	for(int i=1;i<=cnt;++i)
	if(f[i]!=f[i-1])
	m[f[i]]=++s;
	for(int i=1;i<=n;++i)
	{
		a[i].L=m[a[i].L];
		a[i].R=m[a[i].R]-1;
	}
	build(1,1,s);
	for(int i=1;i<=n;++i)
	ans+=query(1,a[i].L,a[i].R);
	cout<<ans;
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 53ms, 内存消耗: 9196K, 提交时间: 2020-09-23 21:22:51

#include<cstdio>
#include<algorithm>
using namespace std;
int sep[200005],tot,n,i,x,y,z,m,k,ans,flag,f[200005][20];
struct Node{
	int h,l,r;
}a[200005];
bool cmp(Node a,Node b)
{return a.h<b.h;}
void dfs(int l,int r,int l1,int r1,int k)
{
	if (l==l1&&r==r1)
	{	if (f[l][k]==0) flag=1,f[l][k]=1;
		return;}
	int mid=(l+r)>>1;
	if (f[l][k]) f[l][k-1]=1,f[mid+1][k-1]=1; 
	if (r1<=mid) dfs(l,mid,l1,r1,k-1);
	if (l1>mid)  dfs(mid+1,r,l1,r1,k-1);
	if (l1<=mid&&r1>mid) dfs(l,mid,l1,mid,k-1),dfs(mid+1,r,mid+1,r1,k-1);
	f[l][k]=f[l][k-1]&&f[mid+1][k-1];
}
int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		a[i].h=y;a[i].l=-(x+1)*z;a[i].r=-x*z;
		sep[++tot]=a[i].l;sep[++tot]=a[i].r;
	}
	sort(sep+1,sep+tot+1);
	tot=unique(sep+1,sep+tot+1)-sep-1;
	for(i=1;i<=n;i++)
	{	a[i].l=lower_bound(sep+1,sep+tot+1,a[i].l)-sep;
		a[i].r=lower_bound(sep+1,sep+tot+1,a[i].r)-sep;}
	sort(a+1,a+n+1,cmp);
	ans=0;m=1;k=0;
	while(m<tot) m*=2,k++;
	for(i=1;i<=n;i++)
	{	flag=0;
		dfs(1,m,a[i].l,a[i].r-1,k);
		ans+=flag;}
	printf("%d\n",ans);
}

上一题