結果
| 問題 |
No.694 square1001 and Permutation 3
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-07-20 20:22:02 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 173 ms / 3,000 ms |
| コード長 | 1,462 bytes |
| コンパイル時間 | 528 ms |
| コンパイル使用メモリ | 31,488 KB |
| 実行使用メモリ | 11,392 KB |
| 最終ジャッジ日時 | 2024-12-30 08:43:04 |
| 合計ジャッジ時間 | 5,085 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 13 |
コンパイルメッセージ
main.c: In function 'in':
main.c:10:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
10 | #define gc() getchar_unlocked()
| ^~~~~~~~~~~~~~~~
main.c:18:24: note: in expansion of macro 'gc'
18 | int n = 0, c = gc();
| ^~
main.c: In function 'out':
main.c:11:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
11 | #define pc(c) putchar_unlocked(c)
| ^~~~~~~~~~~~~~~~
main.c:27:17: note: in expansion of macro 'pc'
27 | if (!n) pc('0');
| ^~
ソースコード
// 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