結果

問題 No.694 square1001 and Permutation 3
ユーザー tnakao0123tnakao0123
提出日時 2018-06-18 15:19:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 321 ms / 3,000 ms
コード長 1,568 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 83,120 KB
実行使用メモリ 9,444 KB
最終ジャッジ日時 2023-09-13 06:47:13
合計ジャッジ時間 4,652 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,572 KB
testcase_01 AC 2 ms
5,584 KB
testcase_02 AC 2 ms
5,604 KB
testcase_03 AC 2 ms
5,764 KB
testcase_04 AC 2 ms
5,548 KB
testcase_05 AC 3 ms
5,584 KB
testcase_06 AC 2 ms
5,476 KB
testcase_07 AC 149 ms
9,344 KB
testcase_08 AC 307 ms
9,292 KB
testcase_09 AC 321 ms
9,380 KB
testcase_10 AC 121 ms
9,444 KB
testcase_11 AC 321 ms
9,292 KB
testcase_12 AC 318 ms
9,320 KB
testcase_13 AC 2 ms
5,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 694.cc: No.694 square1001 and Permutation 3 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 500000;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N], bs[MAX_N], tmp[MAX_N];

/* subroutines */

ll merge_sort(int as[], int tmp[], int l, int r) { // [l, r)
  ll ic = 0;

  if (l + 1 < r) {
    int m = (l + r) / 2;
  
    ic = merge_sort(as, tmp, l, m) + merge_sort(as, tmp, m, r);

    int i = l, j = m, k = l;

    while (i < m && j < r) {
      if (as[i] <= as[j]) tmp[k++] = as[i++];
      else tmp[k++] = as[j++], ic += m - i;
    }

    while (i < m) tmp[k++] = as[i++];
    while (j < r) tmp[k++] = as[j++];

    for (int i = l; i < r; i++) as[i] = tmp[i];
  }  

  return ic;
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    scanf("%d", &as[i]), bs[i] = as[i];

  ll ic = merge_sort(as, tmp, 0, n);

  //printf("ic=%d\n", ic);
  //for (int i = 0; i < n * 2; i++) printf("%d ", bs[i]); putchar('\n');

  for (int i = 0; i < n; i++) {
    printf("%lld\n", ic);

    int &bi = bs[i];
    int lb = lower_bound(as, as + n, bi) - as;
    int ub = upper_bound(as, as + n, bi) - as;
    ic -= lb;
    ic += (n - ub);
  }
  return 0;
}
0