QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#408506#1945. Finding PollyohiostatescarletTL 0ms0kbC++174.2kb2024-05-10 15:35:342024-05-10 15:35:36

Judging History

This is the latest submission verdict.

  • [2024-05-10 15:35:36]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 0kb
  • [2024-05-10 15:35:34]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
#define L long long

struct Li {
    L x1, x2, y1, y2;
    pair<L,L> vec() {
        return {x2-x1,y2-y1};
    }
};

struct F {
    L n, d;
    F() {}
    F(L n, L d) : n(n), d(d) {
        L g = gcd(d, n);
        this->d = d / g;
        this->n = n / g;
        if (this->d < 0) this->n *= -1, this->d *= -1;
    }
    bool operator==(F o) {
        return n == o.n && d == o.d;
    }
    F operator-(F o) {
        return {n * o.d - d * o.n, o.d * d};
    }
    F operator*(F o) {
        return {n * o.n, o.d * d};
    }
};

struct Co {
    bool valid;
    F x, y;
    Co() {}
    Co(F x, F y) : x(x), y(y) {}
    Co(Li a, Li b) {
        auto [vax, vay] = a.vec();
        auto [vbx, vby] = b.vec();
        if (vax*vby-vay*vbx == 0) {
            valid = false;
        } else {
            valid = true;
            L x1 = a.x1, x2 = a.x2, x3 = b.x1, x4 = b.x2;
            L y1 = a.y1, y2 = a.y2, y3 = b.y1, y4 = b.y2;
            for (int i = 0; i < 2; i++) {
                y = {(y2-y1)*(y4*x3-x4*y3)-(y4-y3)*(y2*x1-x2*y1), (x3-x4)*(y2-y1)-(x1-x2)*(y4-y3)};
                swap(x, y); swap(x1, y1); swap(x2, y2); swap(x3, y3); swap(x4, y4);
            }
        }
    }
    bool operator==(Co&o) {
        return x == o.x && y == o.y;
    }
    Co operator-(Co&o) {
        return {x-o.x, y-o.y};
    }
};

F cross(Co d1, Co d2) {
    return d1.x * d2.y - d1.y * d2.x;
}

bool collides(Co p1, Co p2, Co q1, Co q2) {
    for (int i = 0; i < 4; i++) {
        Co dp = p2 - p1;
        F v = (cross(dp, q1 - p1) * cross(dp, q2 - p1));
        if (v.n > 0) {
            return false;
        }
        swap(p1, q2);
        swap(p2, q1);
        swap(q1, q2);
    }
    return true;
};

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n;
    cin >> n;
    vector<Li> Z(n);
    for (Li&l:Z) cin >> l.x1 >> l.y1 >> l.x2 >> l.y2;
    vector<vector<Co>> collisions(n, vector<Co>(n));
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) collisions[i][j] = Co(Z[i], Z[j]);
    vector<int> path;
    queue<int> perm;
    for (int i = 1; i < n; i++) perm.push(i);
    path.push_back(0);
    function<L()> solve = [&]() {
        if (!perm.size()) {
            Co&colA = collisions[path[0]][path[n-1]];
            if (!colA.valid) return 0ll;
            Co&colB = collisions[path[0]][path[1]];
            Co&colC = collisions[path[n-2]][path[n-1]];
            if (colA == colB || colA == collisions[path[n-1]][path[n-2]]) return 0ll;
            for (int i = 0; i < n-2; i++) {
                if (i > 0 && collides(colA, colB, collisions[path[i]][path[i+1]], collisions[path[i+1]][path[i+2]])) return 0ll;
                if (i < n-3 && collides(colA, colC, collisions[path[i]][path[i+1]], collisions[path[i+1]][path[i+2]])) return 0ll;
            }
            return 1ll;
        } else {
            L res = 0;
            for (int i = 0; i < perm.size(); i++) {
                int u = perm.front(); perm.pop();
                bool succ = true;
                Co&colA = collisions[path[path.size()-1]][u];
                if (!colA.valid) {
                    succ = false;
                } else if (path.size() >= 2) {
                    Co&colB = collisions[path[path.size()-2]][path[path.size()-1]];
                    if (colA == colB) {
                        succ = false;
                    } else if (path.size() >= 3) {
                        for (int i = 0; i < path.size()-3; i++) {
                            if (collides(colA, colB, collisions[path[i]][path[i+1]], collisions[path[i+1]][path[i+2]])) {
                                succ = false;
                                break;
                            }
                        }
                    }
                }
                if (succ) {
                    path.push_back(u);
                    res += solve();
                    path.pop_back();
                }
                perm.push(u);
            }
            return res;
        }
    };
    cout << solve() / 2 << "\n";
}

/*

4
0 0 0 1
0 0 1 0
0 2 1 0
2 0 0 1

4
0 0 0 1
0 1 1 1
1 1 1 0
1 0 0 0

*/

詳細信息

Test #1:

score: 0
Time Limit Exceeded

input:

12
1754 -1894 1131 163
-1361 350 1392 -1473
1672 -1863 -634 1706
-1869 498 -432 700
355 1737 -1052 -771
590 1273 -158 778
-1599 1914 -1543 -1839
191 995 -1589 -1387
-1126 -30 854 -1504
-1127 -794 -234 523
-653 -1719 903 -1828
-325 -1165 1958 -1015

output:


result: