CodeForces 589D

  题目大意是给定一些有限制的区间,求每个区间和其他区间相交的次数,依次输出区间相交的个数

  思路:

    暴力,数学

  借鉴了大神的代码

  对于任意一个起点 都可以有 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值