b278中等

98 台中區資訊學科能力競賽(2009)|二度空間上的排名計算

計算每點兩軸皆嚴格更小的點數。

解題思路

計算每點兩軸皆嚴格更小的點數。

用精確有理數讀入有限十進位座標,將y排序去重後壓縮成索引。按x排序,掃描同x的一整組。

對每點查詢y索引之前的Fenwick前綴和,得到之前x嚴格較小且y嚴格較小的點數。

整組都查完才把這組點加入樹,故同x互不影響;依原編號輸出名稱與排名。

時間 O(N log N)次精確比較與整數樹操作,另計座標大整數位數成本;空間 O(N)。

Step-by-step animation

A(1,1)、B(1,2)、C(2,2)的嚴格比較示例

回報動畫錯誤
步驟 1 / 3

步驟 1

示例A(1,1)、B(1,2)、C(2,2)。

同x組:A,B
步驟
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;
}
int main() {
    int n;
    cin >> n;
    struct P {
        string name;
        Q x, y;
        int id;
    };
    vector<P> a(n);
    vector<Q> ys;
    for (int i = 0; i < n; i++) {
        string x, y;
        cin >> a[i].name >> x >> y;
        a[i].x = dec(x);
        a[i].y = dec(y);
        a[i].id = i;
        ys.push_back(a[i].y);
    }
    sort(ys.begin(), ys.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    sort(a.begin(), a.end(), [](P u, P v) { return u.x < v.x; });
    vector<int> bit(n + 1), ans(n);
    vector<string> names(n);
    for (int l = 0; l < n;) {
        int r = l;
        while (r < n && a[r].x == a[l].x)
            r++;
        for (int j = l; j < r; j++) {
            int p = lower_bound(ys.begin(), ys.end(), a[j].y) - ys.begin(), s = 0;
            for (int k = p; k; k -= k & -k)
                s += bit[k];
            ans[a[j].id] = s;
            names[a[j].id] = a[j].name;
        }
        for (int j = l; j < r; j++) {
            int p = lower_bound(ys.begin(), ys.end(), a[j].y) - ys.begin() + 1;
            for (int k = p; k <= n; k += k & -k)
                bit[k]++;
        }
        l = r;
    }
    for (int i = 0; i < n; i++)
        cout << names[i] << ' ' << ans[i] << '\n';
}

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

更多