結果
問題 | No.3091 The Little Match Boy |
ユーザー |
![]() |
提出日時 | 2025-04-06 15:52:32 |
言語 | C (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 38 ms / 2,000 ms |
コード長 | 1,582 bytes |
コンパイル時間 | 392 ms |
コンパイル使用メモリ | 27,136 KB |
実行使用メモリ | 6,272 KB |
最終ジャッジ日時 | 2025-04-06 15:52:36 |
合計ジャッジ時間 | 3,170 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 62 |
コンパイルメッセージ
main.c: In function ‘main’: main.c:69:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 69 | scanf("%d %d", &n, &m); | ^~~~~~~~~~~~~~~~~~~~~~ main.c:72:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 72 | scanf("%d", &s[i]); | ^~~~~~~~~~~~~~~~~~
ソースコード
#define __USE_MINGW_ANSI_STDIO 0 #include<stdio.h> int u[100005], v[100005]; int h[100005], l; int comp_h(int a, int b) { if (u[h[a]] > u[h[b]]) return 1; else if (u[h[a]] < u[h[b]]) return -1; else if (v[h[a]] > v[h[b]]) return 1; else return -1; } void swap_h(int a, int b) { int f = h[a]; h[a] = h[b]; h[b] = f; return; } void push(int ne) { h[l] = ne; int p = l++; for (; p > 0; p = (p - 1) / 2) if (comp_h((p - 1) / 2, p) > 0) swap_h((p - 1) / 2, p); return; } int pop() { swap_h(0, --l); int p = 0; for (;;) { if (2 * p + 2 < l) { if (comp_h(2 * p + 1, 2 * p + 2) > 0) { if (comp_h(p, 2 * p + 2) > 0) swap_h(p, 2 * p + 2); p = 2 * p + 2; } else { if (comp_h(p, 2 * p + 1) > 0) swap_h(p, 2 * p + 1); p = 2 * p + 1; } } else if (2 * p + 1 < l) { if (comp_h(p, 2 * p + 1) > 0) swap_h(p, 2 * p + 1); p = 2 * p + 1; } else break; } return h[l]; } int s[100005]; int p[100005]; int main() { int n, m; scanf("%d %d", &n, &m); int i; for (i = 0; i < m; i++) scanf("%d", &s[i]); for (i = 0; i < n; i++) p[i] = i; for (i = 0; i < m; i++) { s[i]--; u[i] = p[s[i]]; v[i] = p[s[i] + 1]; if (u[i] > v[i]) { u[i] ^= v[i]; v[i] ^= u[i]; u[i] ^= v[i]; } p[s[i]] ^= p[s[i] + 1]; p[s[i] + 1] ^= p[s[i]]; p[s[i]] ^= p[s[i] + 1]; } l = 0; for (i = 0; i < m; i++) push(i); for (i = 0; i < m; i++) p[i] = pop(); int ans = 1; for (i = 1; i < m; i++) if (u[p[i]] != u[p[i - 1]] || v[p[i]] != v[p[i - 1]]) ans++; printf("%d\n", ans); return 0; }