USACO 2023 February Contest, Silver-2 Cow-libi

2 Cow-libi

Note: The time limit for this problem is 4s, two times the default.
Somebody has been grazing in Farmer John’s (1≤G≤105) private gardens! Using his expert forensic knowledge, FJ has been able to determine the precise time each garden was grazed. He has also determined that there was a single cow that was responsible for every grazing incident.
In response to these crimes each of FJ’s N (1≤N≤105) cows have provided an alibi that proves the cow was in a specific location at a specific time. Help FJ test whether each of these alibis demonstrates the cow’s innocence.
A cow can be determined to be innocent if it is impossible for her to have travelled between all of the grazings and her alibi. Cows travel at a rate of 1 unit distance per unit time.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input will contain G and N separated by a space.
The next G lines contain the integers x, y, and t (−109≤x,y≤109;0≤t≤109) separated by a space describing the location and time of the grazing. It will always be possible for a single cow to travel between all grazings.
The next N lines contain x, y, and t (−109≤x,y≤109;0≤t≤109) separated by a space describing the location and time of each cow’s alibi.
OUTPUT FORMAT (print output to the terminal / stdout):
Output a single integer: the number of cows with alibis that prove their innocence.
SAMPLE INPUT:
2 4
0 0 100
50 0 200
0 50 50
1000 1000 0
50 0 200
10 0 170
SAMPLE OUTPUT:
2
There were two grazings; the first at (0,0) at time 100 and the second at (50,0) at time 200.
The first cow’s alibi does not prove her innocence. She has just enough time to arrive at the first grazing.
The second cow’s alibi does prove her innocence. She is nowhere near any of the grazings.
Unfortunately for the third cow, being at the scene of the crime does not prove innocence.
Finally, the fourth cow is innocent because it’s impossible to make it from her alibi to the final grazing in time.
SCORING:
Inputs 2-4: 1≤G,N≤103. Also, for both the fields and alibis, −106≤x,y≤106 and 0≤t≤106.
Inputs 5-11: No additional constraints.
Problem credits: Mark Gordon

对每一个案件按照案发时间进行排序以便于二分搜索,对于每一头牛最能够证明它是不是犯罪了的的案件就是它之前最晚与之后最早的案件。因此,按时间对案件排序,寻找它之后最早的案件pos,那么它之前最晚的案件就是pos-1。然后利用勾股定理算出该牛能不能在规定时间内完成犯罪。

#include <bits/stdc++.h>
#define int long long
using namespace std;

int n,m,cnt;

struct nod{
	int x,y,t;
}nn[100010],mm[100010];

//两分查找,返回右端点 
int two_search(int t){
	int head=0,tail=n+1;
	while(head + 1 < tail){
		int mid=(head+tail)/2;
		if(nn[mid].t>t)tail=mid;
		else head = mid;
	}
	return tail;
}

//返回true表示不能作案 
bool ispos(int pos,int i){
	if((nn[pos].t-mm[i].t)*(nn[pos].t-mm[i].t)<(nn[pos].x-mm[i].x)*(nn[pos].x-mm[i].x)+(nn[pos].y-mm[i].y)*(nn[pos].y-mm[i].y))return true;
	else return false;
}
//返回值为true是能作案 
bool check(int pos,int i){
	//检测pos, pos - 1
	int pos1=pos-1,pos2=pos;
	if (pos2 != n + 1 && ispos(pos2, i)) return false;
	if (pos1 != 0 && ispos(pos1, i)) return false;
	return true;
}

bool cmp(nod q,nod p){
	return q.t<p.t;
}

signed main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%lld%lld%lld",&nn[i].x,&nn[i].y,&nn[i].t);
	for(int i=1;i<=m;++i)
		scanf("%lld%lld%lld",&mm[i].x,&mm[i].y,&mm[i].t);
	sort(nn+1,nn+1+n,cmp);
	for(int i=1;i<=m;++i){
		int pos=two_search(mm[i].t);
		if(!check(pos,i))++cnt;
	}
	cout<<cnt<<endl;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GaoGuohao2022

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值