結果

問題 No.694 square1001 and Permutation 3
ユーザー ats5515ats5515
提出日時 2018-06-08 22:28:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 965 ms / 3,000 ms
コード長 1,175 bytes
コンパイル時間 673 ms
コンパイル使用メモリ 83,204 KB
実行使用メモリ 18,860 KB
最終ジャッジ日時 2023-09-12 23:56:33
合計ジャッジ時間 8,303 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 753 ms
18,800 KB
testcase_08 AC 921 ms
18,732 KB
testcase_09 AC 965 ms
18,764 KB
testcase_10 AC 705 ms
18,824 KB
testcase_11 AC 943 ms
18,860 KB
testcase_12 AC 923 ms
18,732 KB
testcase_13 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
int mergecount(vector<int> &a) {
	int count = 0;
	int n = a.size();
	if (n > 1) {
		vector<int> b(a.begin(), a.begin() + n / 2);
		vector<int> c(a.begin() + n / 2, a.end());
		count += mergecount(b);
		count += mergecount(c);
		for (int i = 0, j = 0, k = 0; i < n; ++i)
			if (k == c.size())       a[i] = b[j++];
			else if (j == b.size())  a[i] = c[k++];
			else if (b[j] <= c[k])   a[i] = b[j++];
			else { a[i] = c[k++]; count += n / 2 - j; }
	}
	return count;
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<int> A(N);
	int res = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	vector<int> B = A;
	res = mergecount(A);
	sort(A.begin(), A.end());
	cout << res << endl;
	for (int i = 0; i < N - 1; i++) {
		int k = lower_bound(A.begin(), A.end(), B[i]) - A.begin();
		int k2 = upper_bound(A.begin(), A.end(), B[i]) - A.begin();
		res -= k;
		res += N - k2;
		cout << res << endl;
	}
}
0