POJ 2653 计算几何

链接:

http://poj.org/userstatus?user_id=Flowersea

题意:

给你n条线段,问你有多少条线段不被后面的线段压着

题解:

数据比较水,直接n²

代码:

  1 #include <map>
  2 #include <set>
  3 #include <cmath>
  4 #include <queue>
  5 #include <stack>
  6 #include <cstdio>
  7 #include <string>
  8 #include <vector>
  9 #include <cstdlib>
 10 #include <cstring>
 11 #include <sstream>
 12 #include <iostream>
 13 #include <algorithm>
 14 #include <functional>
 15 using namespace std;
 16 #define rep(i,a,n) for (int i=a;i<n;i++)
 17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 18 #define all(x) (x).begin(),(x).end()
 19 #define pb push_back
 20 #define mp make_pair
 21 #define lson l,m,rt<<1  
 22 #define rson m+1,r,rt<<1|1 
 23 typedef long long ll;
 24 typedef vector<int> VI;
 25 typedef pair<int, int> PII;
 26 const ll MOD = 1e9 + 7;
 27 const int INF = 0x3f3f3f3f;
 28 const int MAXN = 1e5 + 7;
 29 // head
 30 
 31 const double eps = 1e-8;
 32 int cmp(double x) {
 33     if (fabs(x) < eps) return 0;
 34     if (x > 0) return 1;
 35     return -1;
 36 }
 37 
 38 const double pi = acos(-1);
 39 inline double sqr(double x) {
 40     return x*x;
 41 }
 42 struct point {
 43     double x, y;
 44     point() {}
 45     point(double a, double b) :x(a), y(b) {}
 46     void input() {
 47         scanf("%lf%lf", &x, &y);
 48     }
 49     friend point operator+(const point &a, const point &b) {
 50         return point(a.x + b.x, a.y + b.y);
 51     }
 52     friend point operator-(const point &a, const point &b) {
 53         return point(a.x - b.x, a.y - b.y);
 54     }
 55     friend point operator*(const double &a, const point &b) {
 56         return point(a*b.x, a*b.y);
 57     }
 58     friend point operator/(const point &a, const double &b) {
 59         return point(a.x / b, a.y / b);
 60     }
 61     double norm() {
 62         return sqrt(sqr(x) + sqr(y));
 63     }
 64 };
 65 double det(point a, point b) {
 66     return a.x*b.y - a.y*b.x;
 67 }
 68 double dot(point a, point b) {
 69     return a.x*b.x + a.y*b.y;
 70 }
 71 
 72 struct line {
 73     point a, b;
 74     line() {}
 75     line(point x, point y) :a(x), b(y) {}
 76 }p[MAXN];
 77 bool point_on_segment(point p, point s, point t) {
 78     return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t)) <= 0;
 79 }
 80 bool parallel(line a, line b) {
 81     return !cmp(det(a.a - a.b, b.a - b.b));
 82 }
 83 bool line_make_point(line a, line b,point &res) {
 84     if (parallel(a, b)) return false;
 85     double s1 = det(a.a - b.a, b.b - b.a);
 86     double s2 = det(a.b - b.a, b.b - b.a);
 87     res = (s1*a.b - s2*a.a) / (s1 - s2);
 88     return true;
 89 }
 90 
 91 int main() {
 92     int n;
 93     while (cin >> n, n) {
 94         rep(i, 1, n + 1) {
 95             point a, b;
 96             a.input(), b.input();
 97             p[i] = line(a, b);
 98         }
 99         VI ans;
100         rep(i, 1, n) {
101             int fg = 1;
102             rep(j, i + 1, n + 1) {
103                 point res;
104                 if (line_make_point(p[i], p[j], res))
105                     if (point_on_segment(res, p[i].a, p[i].b) && 
106                         point_on_segment(res, p[j].a, p[j].b)) {
107                         fg = 0;
108                         break;
109                     }
110             }
111             if (fg) ans.pb(i);
112         }
113         cout << "Top sticks: ";
114         rep(i, 0, ans.size()) cout << ans[i] << ", ";
115         cout << n << '.' << endl;
116     }
117     return 0;
118 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值