結果

問題 No.2873 Kendall's Tau
ユーザー tnakao0123
提出日時 2024-09-09 10:39:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 157 ms / 4,500 ms
コード長 1,951 bytes
コンパイル時間 1,163 ms
コンパイル使用メモリ 77,780 KB
最終ジャッジ日時 2025-02-24 06:17:05
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:78:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   78 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:79:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   79 |   for (int i = 0; i < n; i++) scanf("%d%d", xs + i, ys + i);
      |                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2873.cc:  No.2873 Kendall&#39;s Tau - yukicoder
 */

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

using ll = long long;
using pii = pair<int,int>;

template <typename T>
struct BIT {
  int n;
  vector<T> bits;
  
  BIT() {}
  BIT(int _n) { init(_n); }

  void init(int _n) {
    n = _n;
    bits.assign(n + 1, 0);
  }

  T sum(int x) {
    x = min(x, n);
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    if (x <= 0) return;
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

/* global variables */

int xs[MAX_N], ys[MAX_N], uys[MAX_N];
pii xys[MAX_N];
BIT<int> bit;

/* subroutines */

ll calcr(int n, int xs[]) {
  sort(xs, xs + n);
  ll r = 0;
  for (int i = 0; i < n;) {
    int j = i;
    while (i < n && xs[j] == xs[i]) i++;
    r += (ll)(n - (i - j)) * (i - j);
  }
  return r / 2;
}

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d%d", xs + i, ys + i);
  
  copy(ys, ys + n, uys);
  sort(uys, uys + n);
  int m = unique(uys, uys + n) - uys;
  for (int i = 0; i < n; i++)
    ys[i] = lower_bound(uys, uys + m, ys[i]) - uys;

  for (int i = 0; i < n; i++) xys[i] = {xs[i], ys[i]};
  sort(xys, xys + n);

  bit.init(m);
  ll p = 0, q = 0;
  for (int i = 0; i < n;) {
    int j = i;
    while (i < n && xys[j].first == xys[i].first) {
      auto [xi, yi] = xys[i++];
      p += bit.sum(yi);
      q += j - bit.sum(yi + 1);
    }

    while (j < i) {
      auto [xj, yj] = xys[j++];
      bit.add(yj + 1, 1);
    }
  }
  //printf(" p=%lld, q=%lld\n", p, q);

  ll r = calcr(n, xs);
  ll s = calcr(n, ys);
  //printf(" r=%lld, s=%lld\n", r, s);

  double tau = (double)(p - q) / (sqrt(r) * sqrt(s));

  printf("%.10lf\n", tau);
  
  return 0;
}
0