CodeForces 589D
来源:互联网 发布:手机比价软件哪个好 编辑:程序博客网 时间:2024/05/26 15:55
题目大意是给定一些有限制的区间,求每个区间和其他区间相交的次数,依次输出区间相交的个数
思路:
暴力,数学
借鉴了大神的代码
对于任意一个起点 都可以有 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
- CodeForces 589D Boulevard
- CodeForces 589D
- CodeForces #589 D Boulevard 模拟
- CodeForces 589D(思路)
- Codeforces 589D Boulevard 线段相交
- codeforces 589d(相遇问题)
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- codeforces 257D. Sum
- Codeforces 292D
- Codeforces 275D
- Codeforces 126D
- poj2513Network Saboteur(dfs+简单减枝)
- 操作符重载
- HDU1171--01背包
- 使用VIM支持鼠标操作
- 平衡二叉树
- CodeForces 589D
- Android之自定义实现BaseAdapter(普通实现)
- 150. Evaluate Reverse Polish Notation
- Android 6.0 运行时权限如何处理
- cocos编译安卓程序:Unsupported major.minor version 52.0
- 可调节灵敏度的ViewPager
- 5访客统计
- red hat linux 5中文显示乱码,无法输入中文
- 创建一个弧