結果

問題 No.694 square1001 and Permutation 3
ユーザー tnakao0123tnakao0123
提出日時 2018-06-18 14:59:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,546 bytes
コンパイル時間 756 ms
コンパイル使用メモリ 83,264 KB
実行使用メモリ 9,448 KB
最終ジャッジ日時 2023-09-13 06:47:02
合計ジャッジ時間 4,060 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,640 KB
testcase_01 AC 2 ms
5,516 KB
testcase_02 AC 2 ms
5,724 KB
testcase_03 AC 2 ms
5,536 KB
testcase_04 AC 2 ms
5,588 KB
testcase_05 AC 2 ms
5,728 KB
testcase_06 AC 2 ms
5,592 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
5,576 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 */

/* global variables */

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

/* subroutines */

int merge_sort(int as[], int tmp[], int l, int r) { // [l, r)
  int 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];

  int 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("%d\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