题目大意是给定一些有限制的区间,求每个区间和其他区间相交的次数,依次输出区间相交的个数
思路:
暴力,数学
借鉴了大神的代码
对于任意一个起点 都可以有 x = t + b / x = -t + b
则 b 可以求出是 si ± ti 则 判断线段相交 先看斜率
斜率相同的情况下判断区间是否包含或者相交
斜率不同的情况下判断交点是否满足同时在两个区间内
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int n;
#define bug printf("Bug\n")
struct Point{
int t, s, f;
Point(int t = 0, int s = 0, int f = 0) : t(t), s(s), f(f) {}
}p[maxn];
int cnt[maxn];
bool judge(int s1, int f1, int s2, int f2){
if(s1 >= s2 && f1 <= f2) return true;
if(s2 >= s1 && f2 <= f1) return true;
if(s1 <= f2 && f2 <= f1) return true;
if(f1 <= f2 && f1 >= s2) return true;
return false;
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; ++i){
int t, s, f;
scanf("%d %d %d", &t, &s, &f);
p[i] = Point(t, s, f);
}
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
int s1 = p[i].s, f1 = p[i].f, s2 = p[j].s, f2 = p[j].f, t1 = p[i].t, t2 = p[j].t;
if(s1 <= f1 && s2 <= f2){
if(s1 - t1 != s2 - t2) continue;
if(judge(t1, f1 - s1 + t1, t2, f2 - s2 + t2)){
cnt[i]++; cnt[j]++;
}
}
else if(s1 > f1 && s2 <= f2){
double x = (s1 + s2 + t1 - t2) * 1.0 / 2;
if(x >= s2 && x <= f2 && x >= f1 && x <= s1){
cnt[i]++; cnt[j]++;
}
}
else if(s1 <= f1 && s2 > f2){
double x = (s1 + s2 + t2 - t1) * 1.0 / 2;
if(x >= f2 && x <= s2 && x >= s1 && x <= f1){
cnt[i]++; cnt[j]++;
}
}
else{
if(s1 + t1 != s2 + t2) continue;
if(judge(t1, s1 - f1 + t1, t2, s2 - f2 + t2)) {
cnt[i]++; cnt[j]++;
}
}
}
}
for(int i = 0; i < n; ++i){
if(i) printf(" ");
printf("%d", cnt[i]);
}
return 0;
}