結果

問題 No.694 square1001 and Permutation 3
ユーザー bal4ubal4u
提出日時 2019-07-20 20:22:02
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 162 ms / 3,000 ms
コード長 1,462 bytes
コンパイル時間 1,017 ms
コンパイル使用メモリ 30,256 KB
実行使用メモリ 11,380 KB
最終ジャッジ日時 2023-08-28 20:51:03
合計ジャッジ時間 5,854 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,504 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 103 ms
11,288 KB
testcase_08 AC 147 ms
11,380 KB
testcase_09 AC 162 ms
11,352 KB
testcase_10 AC 72 ms
9,632 KB
testcase_11 AC 162 ms
11,344 KB
testcase_12 AC 161 ms
11,348 KB
testcase_13 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:10:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:18:24: 備考: in expansion of macro ‘gc’
   18 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:11:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   11 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:27:17: 備考: in expansion of macro ‘pc’
   27 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.694 square1001 and Permutation 3
// 2019.7.20 bal4u

#include <stdio.h>
#include <stdlib.h>

typedef long long ll;

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

void out(ll n) { // 非負整数の表示(出力)
	int i;
	char b[50];

	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

#define MAX 500005

// Bit木 library
int bit[MAX]; int nbit;
int sum(int i) {
	int s = 0;
	while (i > 0) s += bit[i], i -= i & -i;
	return s;
}

void add(int i) {
	while (i <= nbit) bit[i]++, i += i & -i;
}

typedef struct { int a, id; } T;
T t[MAX];
int a[MAX]; int N;

int cmp(const void *a, const void *b) { return ((T *)a)->a - ((T *)b)->a; }

void compact(int n, int *a) {
	int i, v;
	for (i = 0; i < n; i++) t[i].a = a[i], t[i].id = i;
	qsort(t, n, sizeof(T), cmp);
	v = 0; a[t[0].id] = v;
	for (i = 1; i < n; i++) {
		if (t[i].a != t[i-1].a) v++;
		a[t[i].id] = v;
	}
}

int main()
{
	int i;
	ll ans;
	
	N = in();
	for (i = 0; i < N; i++) a[i] = in();
	compact(N, a);

	ans = 0, nbit = N+1;
	for (i = 0; i < N; i++) {
		ans += i - sum(a[i]+1);
		add(a[i]+1);
	}

	for (i = 0; i < N; i++) {
		out(ans);
		ans -= sum(a[i]);
		ans += N - sum(a[i]+1);
	}
	return 0;
}
0