結果

問題 No.694 square1001 and Permutation 3
ユーザー konsin_tokagekonsin_tokage
提出日時 2018-06-25 20:32:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 910 ms / 3,000 ms
コード長 894 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 60,756 KB
実行使用メモリ 9,304 KB
最終ジャッジ日時 2023-09-13 13:55:28
合計ジャッジ時間 8,324 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,408 KB
testcase_01 AC 2 ms
5,488 KB
testcase_02 AC 2 ms
5,552 KB
testcase_03 AC 3 ms
5,424 KB
testcase_04 AC 3 ms
5,476 KB
testcase_05 AC 3 ms
5,424 KB
testcase_06 AC 3 ms
5,552 KB
testcase_07 AC 731 ms
9,232 KB
testcase_08 AC 889 ms
9,240 KB
testcase_09 AC 895 ms
9,232 KB
testcase_10 AC 734 ms
9,236 KB
testcase_11 AC 910 ms
9,304 KB
testcase_12 AC 909 ms
9,284 KB
testcase_13 AC 2 ms
5,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;
int a[500000], b[500000], t[500000];
long long r;
void merge_sort(int s, int e){
    if(e-s == 1) return;
    int m = s+(e-s)/2;
    merge_sort(s, m);
    merge_sort(m, e);
    int i = s, j = m;
    for(int k=s; k<e; ++k){
        if(j==e || i<m&&b[i]<=b[j]){
            t[k] = b[i];
            ++i;
        }
        else{
            t[k] = b[j];
            ++j;
            r += m-i;
        }
    }
    for(int i=s; i<e; ++i) b[i] = t[i];
}
int main(){
    cin.tie(0), ios_base::sync_with_stdio(false); 
    int n;
    cin >> n;
    for(int i=0; i<n; ++i) cin >> a[i];
    for(int i=0; i<n; ++i) b[i] = a[i];
    merge_sort(0, n);
    for(int i=0; i<n; ++i){
        cout << r << endl;
        int l = lower_bound(b, b+n, a[i]) - b;
        int u = upper_bound(b, b+n, a[i]) - b;
        r += (n-u) - l;
    }
    return 0;
}
0