結果
| 問題 | No.694 square1001 and Permutation 3 |
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-07-20 20:22:02 |
| 言語 | C(gnu17) (gcc 15.2.0) |
| 結果 |
AC
|
| 実行時間 | 166 ms / 3,000 ms |
| コード長 | 1,462 bytes |
| 記録 | |
| コンパイル時間 | 316 ms |
| コンパイル使用メモリ | 40,392 KB |
| 最終ジャッジ日時 | 2026-02-22 03:58:13 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 13 |
ソースコード
// 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;
}
bal4u