• 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 }
  • 相关阅读:
    C# 泛型
    css样式优先级
    c# 可空类型
    JS 两个数组合并
    c# 参数传递问题(形参与实参)
    c# JsonReader读取json字符串
    C# 获取当前ip
    swprintf引发的血案
    Struts2解析FreeMarker模板中变量的顺序
    structs2在action中输出
  • 原文地址:https://www.cnblogs.com/baocong/p/6730435.html
Copyright © 2020-2023  润新知