b325中等

98 臺北市資訊學科能力競賽(2009)|剪多邊形

計算凸包內原輪廓外的塗色區域所需色塊數。

解題思路

計算凸包內原輪廓外的塗色區域所需色塊數。

先由原頂點建立凸包,使用鞋帶公式分別計算原輪廓與凸包的兩倍面積。凸包包含原輪廓,所以差為所有外加區域面積。

用精確有理數讀座標與色塊面積a,所需數為ceil((凸包面積−原面積)/a)。

若差為0則輸出0;否則即使剩下不足一塊也需再一塊,對精確分數分子分母做整數向上除法。

時間 O(n log n)次有理數幾何操作,空間 O(n);數字位數成本另計,n≤20。

Step-by-step animation

凸包與原多邊形的面積差逐步追蹤

回報動畫錯誤
步驟 1 / 3

步驟 1

範例原輪廓(0,0),(5,5),(10,0),(5,10)。

原面積25
步驟
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::multiprecision::cpp_int;
using ll = long long;
#include <boost/rational.hpp>
using Q = boost::rational<cpp_int>;
Q rat(cpp_int a, cpp_int b) {
    if (b < 0) {
        a = -a;
        b = -b;
    }
    return Q(a, b);
}
cpp_int abig(cpp_int a) {
    return a < 0 ? -a : a;
}
cpp_int ten(int n) {
    cpp_int a = 1;
    while (n--)
        a *= 10;
    return a;
}
Q dec(string s) {
    auto slash = s.find('/');
    if (slash != string::npos)
        return Q(cpp_int(s.substr(0, slash)), cpp_int(s.substr(slash + 1)));
    bool neg = !s.empty() && s[0] == '-';
    if (s[0] == '-' || s[0] == '+')
        s.erase(s.begin());
    auto p = s.find('.');
    int k = p == string::npos ? 0 : s.size() - p - 1;
    if (p != string::npos)
        s.erase(p, 1);
    size_t z = s.find_first_not_of('0');
    cpp_int a = z == string::npos ? cpp_int(0) : cpp_int(s.substr(z));
    return Q(neg ? -a : a, ten(k));
}
string fixedq(Q q, int digits) {
    cpp_int a = q.numerator(), b = q.denominator(), scale = ten(digits);
    bool neg = a < 0;
    a = abig(a);
    cpp_int k = (2 * a * scale + b) / (2 * b);
    string s = k.convert_to<string>();
    if (digits) {
        if (s.size() <= digits)
            s = string(digits + 1 - s.size(), '0') + s;
        s.insert(s.size() - digits, ".");
    }
    return (neg && k != 0 ? "-" : "") + s;
}
string finiteq(Q q) {
    cpp_int a = q.numerator(), b = q.denominator();
    bool neg = a < 0;
    a = abig(a);
    cpp_int whole = a / b, r = a % b;
    string s = whole.convert_to<string>();
    if (r != 0) {
        s += '.';
        while (r != 0) {
            r *= 10;
            cpp_int z = r / b;
            s += char('0' + z.convert_to<int>());
            r %= b;
        }
    }
    return (neg ? "-" : "") + s;
}
struct Pt {
    Q x, y;
    bool operator<(const Pt &b) const {
        return x != b.x ? x < b.x : y < b.y;
    }
    bool operator==(const Pt &b) const {
        return x == b.x && y == b.y;
    }
};
Q cross(Pt a, Pt b, Pt c) {
    return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
vector<Pt> hull(vector<Pt> a) {
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    if (a.size() < 3)
        return a;
    vector<Pt> h;
    for (Pt p : a) {
        while (h.size() > 1 && cross(h[h.size() - 2], h.back(), p) <= 0)
            h.pop_back();
        h.push_back(p);
    }
    size_t l = h.size();
    for (int i = (int)a.size() - 2; i >= 0; i--) {
        Pt p = a[i];
        while (h.size() > l && cross(h[h.size() - 2], h.back(), p) <= 0)
            h.pop_back();
        h.push_back(p);
    }
    h.pop_back();
    return h;
}
int main() {
    int n;
    string s;
    cin >> n >> s;
    Q area = dec(s);
    vector<Pt> a(n);
    for (auto &p : a) {
        string x, y;
        cin >> x >> y;
        p.x = dec(x);
        p.y = dec(y);
    }
    auto area2 = [](vector<Pt> p) {
        Q s(0);
        for (int i = 0; i < (int)p.size(); i++)
            s += p[i].x * p[(i + 1) % p.size()].y - p[i].y * p[(i + 1) % p.size()].x;
        return s < 0 ? -s : s;
    };
    Q q = (area2(hull(a)) - area2(a)) / (2 * area);
    cout << (q.numerator() + q.denominator() - 1) / q.denominator() << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多