Submission #3721787


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;
int bit[100001], N;
int sum(int i) {
	int s = 0;
	while (i > 0) {
		s += bit[i];
		i -= i & -i;
	}
	return s;
}
void add(int i, int x) {
	while (i <= N) {
		bit[i] += x;
		i += i & -i;
	}
}
int pos[100001];
int main() {
	cin >> N;
	for (int i = 1; i <= N; i++) {
		int x;
		cin >> x;
		pos[x] = i;
		add(i, 1);
	}
	ll ans = 0;
	for (int i = 1; i <= N; i++) {
		int l = 0, r = 0;
		if (pos[i] > 1) l = sum(pos[i] - 1);
		if (pos[i] + 1 <= N) r = sum(N) - sum(pos[i]);
		if (l <= r) ans += l;
		else ans += r;
		add(pos[i], -1);
	}
	cout << ans << endl;
}

Submission Info

Submission Time
Task C - 積み木
User Div9851
Language C++14 (GCC 5.4.1)
Score 100
Code Size 751 Byte
Status AC
Exec Time 43 ms
Memory 1024 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 70 / 70
Status
AC × 3
AC × 33
AC × 68
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
Subtask1 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 10_small_00.txt, 10_small_01.txt, 10_small_02.txt, 10_small_03.txt, 10_small_04.txt, 10_small_05.txt, 10_small_06.txt, 10_small_07.txt, 10_small_08.txt, 10_small_09.txt, 10_small_10.txt, 10_small_11.txt, 10_small_12.txt, 10_small_13.txt, 10_small_14.txt, 10_small_15.txt, 10_small_16.txt, 10_small_17.txt, 10_small_18.txt, 10_small_19.txt, 10_small_20.txt, 10_small_21.txt, 10_small_22.txt, 10_small_23.txt, 10_small_24.txt, 10_small_25.txt, 10_small_26.txt, 10_small_27.txt, 10_small_28.txt, 10_small_29.txt
Subtask2 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 10_small_00.txt, 10_small_01.txt, 10_small_02.txt, 10_small_03.txt, 10_small_04.txt, 10_small_05.txt, 10_small_06.txt, 10_small_07.txt, 10_small_08.txt, 10_small_09.txt, 10_small_10.txt, 10_small_11.txt, 10_small_12.txt, 10_small_13.txt, 10_small_14.txt, 10_small_15.txt, 10_small_16.txt, 10_small_17.txt, 10_small_18.txt, 10_small_19.txt, 10_small_20.txt, 10_small_21.txt, 10_small_22.txt, 10_small_23.txt, 10_small_24.txt, 10_small_25.txt, 10_small_26.txt, 10_small_27.txt, 10_small_28.txt, 10_small_29.txt, 20_rand_00.txt, 20_rand_01.txt, 20_rand_02.txt, 20_rand_03.txt, 20_rand_04.txt, 20_rand_05.txt, 20_rand_06.txt, 20_rand_07.txt, 20_rand_08.txt, 20_rand_09.txt, 20_rand_10.txt, 20_rand_11.txt, 20_rand_12.txt, 20_rand_13.txt, 20_rand_14.txt, 20_rand_15.txt, 20_rand_16.txt, 20_rand_17.txt, 20_rand_18.txt, 20_rand_19.txt, 20_rand_20.txt, 20_rand_21.txt, 20_rand_22.txt, 20_rand_23.txt, 20_rand_24.txt, 20_rand_25.txt, 20_rand_26.txt, 20_rand_27.txt, 20_rand_28.txt, 20_rand_29.txt, 30_max_0.txt, 30_max_1.txt, 30_max_2.txt, 30_max_3.txt, 30_max_4.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 256 KB
00_sample_01.txt AC 1 ms 256 KB
00_sample_02.txt AC 1 ms 256 KB
10_small_00.txt AC 1 ms 256 KB
10_small_01.txt AC 1 ms 256 KB
10_small_02.txt AC 1 ms 256 KB
10_small_03.txt AC 1 ms 256 KB
10_small_04.txt AC 1 ms 256 KB
10_small_05.txt AC 1 ms 256 KB
10_small_06.txt AC 1 ms 256 KB
10_small_07.txt AC 1 ms 256 KB
10_small_08.txt AC 1 ms 256 KB
10_small_09.txt AC 1 ms 256 KB
10_small_10.txt AC 1 ms 256 KB
10_small_11.txt AC 1 ms 256 KB
10_small_12.txt AC 1 ms 256 KB
10_small_13.txt AC 1 ms 256 KB
10_small_14.txt AC 1 ms 256 KB
10_small_15.txt AC 1 ms 256 KB
10_small_16.txt AC 1 ms 256 KB
10_small_17.txt AC 1 ms 256 KB
10_small_18.txt AC 1 ms 256 KB
10_small_19.txt AC 1 ms 256 KB
10_small_20.txt AC 1 ms 256 KB
10_small_21.txt AC 1 ms 256 KB
10_small_22.txt AC 1 ms 256 KB
10_small_23.txt AC 1 ms 256 KB
10_small_24.txt AC 1 ms 256 KB
10_small_25.txt AC 1 ms 256 KB
10_small_26.txt AC 1 ms 256 KB
10_small_27.txt AC 1 ms 256 KB
10_small_28.txt AC 1 ms 256 KB
10_small_29.txt AC 1 ms 256 KB
20_rand_00.txt AC 28 ms 768 KB
20_rand_01.txt AC 4 ms 256 KB
20_rand_02.txt AC 8 ms 384 KB
20_rand_03.txt AC 13 ms 512 KB
20_rand_04.txt AC 28 ms 768 KB
20_rand_05.txt AC 39 ms 896 KB
20_rand_06.txt AC 41 ms 1024 KB
20_rand_07.txt AC 29 ms 768 KB
20_rand_08.txt AC 8 ms 384 KB
20_rand_09.txt AC 6 ms 384 KB
20_rand_10.txt AC 27 ms 768 KB
20_rand_11.txt AC 13 ms 512 KB
20_rand_12.txt AC 13 ms 512 KB
20_rand_13.txt AC 17 ms 512 KB
20_rand_14.txt AC 11 ms 384 KB
20_rand_15.txt AC 3 ms 256 KB
20_rand_16.txt AC 18 ms 640 KB
20_rand_17.txt AC 2 ms 256 KB
20_rand_18.txt AC 43 ms 1024 KB
20_rand_19.txt AC 8 ms 384 KB
20_rand_20.txt AC 6 ms 384 KB
20_rand_21.txt AC 27 ms 768 KB
20_rand_22.txt AC 28 ms 768 KB
20_rand_23.txt AC 41 ms 1024 KB
20_rand_24.txt AC 18 ms 512 KB
20_rand_25.txt AC 21 ms 640 KB
20_rand_26.txt AC 15 ms 512 KB
20_rand_27.txt AC 16 ms 512 KB
20_rand_28.txt AC 34 ms 896 KB
20_rand_29.txt AC 6 ms 384 KB
30_max_0.txt AC 42 ms 1024 KB
30_max_1.txt AC 42 ms 1024 KB
30_max_2.txt AC 42 ms 1024 KB
30_max_3.txt AC 42 ms 1024 KB
30_max_4.txt AC 42 ms 1024 KB