解题思路:直接暴力判断两两直接是否相遇。任意选两个人p1,p2.如果p1的时间大于p2交换一下,令p1和p2时间相同,即p1走到和p2时间相同。p1和p2如果相遇有两种情况,第一种,同向时,p1和p2的位置相同。第二种,p1和p2反向,求出相遇点,判断p1、p2是否走到终点,即是否在两个区间交集里。
#include<cstdio>
#include<algorithm>
using namespace std;
struct tt{
int t;
int s;
int f;
}a[1010];
int num[1010];
bool judge(tt p1, tt p2){
if(p1.t > p2.t){
swap(p1.t, p2.t);
swap(p1.s, p2.s);
swap(p1.f, p2.f);
}
int flag1, flag2;
if(p1.s > p1.f) flag1 = -1;
else flag1 = 1;
if(p2.s > p2.f) flag2 = -1;
else flag2 = 1;
int x = p1.s + (p2.t - p1.t) * flag1;
if(flag1 == 1 && flag2 == 1){
if(x == p2.s && x <= p1.f) return true;
}
if(flag1 == -1 && flag2 == 1){
int t1 = x - p1.f;
int t2 = p2.f - p2.s;
int tt = (x - p2.s);
double t = tt / 2.0;
double ans = x - t;
if(p1.s < p2.s || p1.f > p2.f) return false;
if(ans >= p1.f && ans <= p2.f && ans >= p2.s && ans <= x) return true;
}
else if(flag1 == 1 && flag2 == -1){
int t1 = p1.f - x;
int t2 = p2.s - p2.f;
int tt = p2.s - x;
double t = tt / 2.0;
double ans = x + t;
if(p1.f < p2.f || p1.s > p2.s) return false;
if(ans >= p2.f && ans <= p1.f && ans >= x && ans <= p2.s) return true;
}
else if(flag1 == -1 && flag2 == -1){
if(x == p2.s && x >= p1.f) return true;
}
return false;
}
int main(){
int n;
scanf("%d", &n);
for(int i = 0;i < n; i++){
scanf("%d%d%d", &a[i].t, &a[i].s, &a[i].f);
}
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++)
if(judge(a[i], a[j])) {
num[i]++;
num[j]++;
}
}
for(int i = 0; i < n; i++){
if(i) printf(" ");
printf("%d", num[i]);
}
printf("\n");
}