結果
| 問題 |
No.2873 Kendall's Tau
|
| コンテスト | |
| ユーザー |
n0ma_ru
|
| 提出日時 | 2024-09-06 23:08:28 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 303 ms / 4,500 ms |
| コード長 | 3,026 bytes |
| コンパイル時間 | 2,212 ms |
| コンパイル使用メモリ | 212,548 KB |
| 最終ジャッジ日時 | 2025-02-24 04:54:02 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << (x) << endl;
template<class F, class S>
ostream& operator<<(ostream& os, pair<F,S>& p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template<class Iter>
void print(Iter beg, Iter end) {
for (Iter itr = beg; itr != end; ++itr) {
cerr << *itr << ' ';
}
cerr << '\n';
}
class BIT {
public:
BIT(int n)
: n(n), d(n+1,0) {
}
// index番目にvalを加える
void add(int index, ll val) {
assert(0 <= index && index < n);
++index;
for (int x = index; x <= n; x += x & -x) {
d[x] += val;
}
}
// [0, index)の総和
ll sum(int index) {
ll ret = 0;
for (int x = index; x > 0; x -= x & -x) {
ret += d[x];
}
return ret;
}
// [l, r)の総和
ll sum(int l, int r) {
l = max(0, l);
r = min(r, n);
return sum(r) - sum(l);
}
private:
int n;
vector<ll> d;
};
int n;
vector<ll> x, y;
int main() {
cin >> n;
x.resize(n);
y.resize(n);
for (int i = 0; i < n; ++i) cin >> x[i] >> y[i];
vector<int> ord(n);
iota(ALL(ord), 0);
ll p = 0, q=0, r=0, s=0;
{
sort(ALL(ord), [](int a, int b) {
return y[a] < y[b];
});
ll now = -1e18;
int cnt = 0;
for (int i : ord) {
if (y[i] == now) {
++cnt;
} else {
q += ll(n - cnt) * cnt;
now = y[i];
cnt = 1;
}
}
q += ll(n - cnt) * cnt;
assert(q % 2 == 0);
q /= 2;
dbg(q);
}
{
sort(ALL(ord), [](int a, int b) {
return x[a] < x[b];
});
ll now = -1e18;
int cnt = 0;
for (int i : ord) {
if (x[i] == now) {
++cnt;
} else {
p += cnt * ll(n - cnt);
now = x[i];
cnt = 1;
}
}
p += cnt * ll(n - cnt);
assert(p % 2 == 0);
p /= 2;
dbg(p);
}
{
auto tmp = y;
sort(ALL(tmp));
tmp.erase(unique(ALL(tmp)), tmp.end());
for (int i = 0; i < n; ++i) y[i] = lower_bound(ALL(tmp), y[i]) - tmp.begin();
BIT bit(tmp.size());
vector<int> add;
ll now = 1e18;
for (int i : ord) {
if (x[i] == now) {
add.push_back(y[i]);
} else {
for (int j : add) {
bit.add(j, 1);
}
add.clear();
now = x[i];
add.push_back(y[i]);
}
r += bit.sum(0, y[i]);
s += bit.sum(y[i]+1, tmp.size());
}
dbg(r)
dbg(s)
}
double ans = (r - s) / (sqrt(p) * sqrt(q));
cout << fixed << setprecision(20) << ans << '\n';
}
n0ma_ru