結果
| 問題 |
No.2873 Kendall's Tau
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-20 00:37:07 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 6,160 bytes |
| コンパイル時間 | 3,783 ms |
| コンパイル使用メモリ | 199,228 KB |
| 最終ジャッジ日時 | 2025-02-24 09:38:55 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 WA * 17 |
ソースコード
#include<iostream>
#include<sstream>
#include<algorithm>
#include<deque>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<utility>
#include<string.h>
#include<string>
#include<math.h>
#include<float.h>
#include<stdio.h>
#include<vector>
#include<iomanip>
#include<bitset>
#include<random>
#include<complex>
#include <cassert>
#include <utility>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmin(T &a, T b) { return ((a>b) ? (a = b, true) : (false));}
#define rep(i,s,n) for(long long i=s;i<(long long)(n);i++)
#define rrep(i,s,n) for(long long i=n-1;i>=s;i--)
const long long inf = 1LL<<60;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//pairのsecondでソートsort(p.begin(),p.end(),cmp)
#define cmp [](pair<ll,ll> a, pair<ll,ll> b){return a.second<b.second;}
//cmpArg := 偏角ソート, atan2l(y, x); atan2l(y, x) = [-pi, pi]
#define cmpArg [](const auto &p1, const auto &p2) { return atan2l(p1.second, p1.first) < atan2l(p2.second, p2.first); }
typedef pair<long long, long long> P;
typedef pair<ll, pair<ll,ll> > PP;
#define rll ll,vector<ll>,greater<ll>
#define rP P,vector<P>,greater<P>
const long double pi = 3.14159265358979;
typedef unsigned long long ull;
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vmint vector<mint>
#define vvmint vector<vector<mint>>
#define vvch vector<vector<char>>
#define vch vector<char>
#define vstring vector<string>
#define rPP PP,vector<PP>,greater<PP>
#define vP vector<P>
#define vvP vector<vector<P>>
#define vPP vector<PP>
#define all(x) x.begin(), x.end()
//UNIQUE(x) xをソートして値の被りがないようにする
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
int pc(ll x) { return __builtin_popcount(x); } //ビット列にどれだけ1がたっているかを求める pop count
//逆順のlower_bound(単調減少関数で自分以下を二分探索)するときは`ll index = lower_bound(all(inv), -a[i], greater<ll>()) - inv.begin();`, 逆からみたlis的なやつが作れる
//オバフロしない計算はa > inf - b および a > inf / bでとってね
//半分全列挙は前の方を(siz+1)/2ででかくする
//using mint = atcoder::modint, main関数でmint::set_mod(M)とすると任意modのmintにできる
//mapでも auto it = mp.lower_bound(key)としてlower_boundが使用できる while(it != mp.end())でループすることもできる
//解いてる時に詰まったらできるだけ数式や図に変換してみる!!
using mint = modint998244353;
class BIT {
public:
//データの長さ
ll n;
//データの格納先
vector<ll> a;
//コンストラクタ
BIT(ll n):n(n),a(n+1,0){}
//a[i]にxを加算する
void add(ll i,ll x){
i++;
if(i==0) return;
for(ll k=i;k<=n;k+=(k & -k)){
a[k]+=x;
}
}
//a[i]+a[i+1]+…+a[j]を求める
ll sum(ll i,ll j){
return sum_sub(j)-sum_sub(i-1);
}
//a[0]+a[1]+…+a[i]を求める
ll sum_sub(ll i){
i++;
ll s=0;
if(i==0) return s;
for(ll k=i;k>0;k-=(k & -k)){
s+=a[k];
}
return s;
}
//a[0]+a[1]+…+a[i]>=xとなる最小のiを求める(任意のkでa[k]>=0が必要)
ll lower_bound(ll x){
if(x<=0){
//xが0以下の場合は該当するものなし→0を返す
return 0;
}else{
ll i=0;ll r=1;
//最大としてありうる区間の長さを取得する
//n以下の最小の二乗のべき(BITで管理する数列の区間で最大のもの)を求める
while(r<n) r=r<<1;
//区間の長さは調べるごとに半分になる
for(int len=r;len>0;len=len>>1) {
//その区間を採用する場合
if(i+len<n && a[i+len]<x){
x-=a[i+len];
i+=len;
}
}
return i;
}
}
};
// Coodinate Compression
// https://youtu.be/fR3W5IcBGLQ?t=8550
template<typename T=ll>
struct CC {
bool initialized;
vector<T> xs;
CC(): initialized(false) {}
void add(T x) { xs.push_back(x);} //要素の追加
void init() {
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(),xs.end()),xs.end());
initialized = true;
}
int operator()(T x) { //圧縮前 -> 圧縮後
if (!initialized) init();
return upper_bound(xs.begin(), xs.end(), x) - xs.begin() - 1;
}
T operator[](int i) { //圧縮後 -> 圧縮前
if (!initialized) init();
return xs[i];
}
int size() {
if (!initialized) init();
return xs.size();
}
};
int main()
{
ll n; cin >> n;
CC cc;
vector<int> x(n) , y(n);
rep(i,0,n) cin >> x[i] >> y[i];
rep(i,0,n) cc.add(x[i]), cc.add(y[i]);
rep(i,0,n) x[i] = cc(x[i]), y[i] = cc(y[i]);
ll P=0,Q=0,R,S;
R=S=n*(n-1)/2;
//R,Sを求める
auto calcRS = [&](const vector<int>& array) -> ll {
vector<ll> cnt(cc.size()+10,0);
rep(i,0,n) cnt[array[i]]++;
ll res = 0;
rep(i,0,cc.size()+10) {
res -= cnt[i] * (cnt[i] - 1) / 2;
}
return res;
};
R += calcRS(x);
S += calcRS(y);
//P,Qを求める
vector<pair<int,int>> xy(n);
rep(i,0,n) xy[i] = {x[i], y[i]};
sort(xy.begin(), xy.end(), [](pair<int,int> a, pair<int,int> b) {
if(a.first != b.first) return a.first < b.first;
return a.second > b.second;
});
BIT bit(cc.size()+10);
rep(i,0,n) {
P += bit.sum(0,xy[i].second-1);
bit.add(xy[i].second, 1);
}
//Qの計算
sort(all(xy));
BIT bit2(cc.size()+10);
rep(i,0,n) {
Q += bit2.sum(xy[i].second+1, cc.size()+9);
bit2.add(xy[i].second, 1);
}
double ans = double(P - Q) / double(sqrt(R*S));
cout << setprecision(20) << ans << endl;
}