結果

問題 No.3091 The Little Match Boy
コンテスト
ユーザー tnakao0123
提出日時 2025-04-08 17:22:50
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 801 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 270 ms
コンパイル使用メモリ 58,540 KB
実行使用メモリ 5,760 KB
最終ジャッジ日時 2026-07-08 13:44:30
合計ジャッジ時間 2,947 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 62
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3091.cc:  No.3091 The Little Match Boy - yukicoder
 */

#include<cstdio>
#include<algorithm>
#include<utility>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 100000;

/* typedef */

using pii = pair<int,int>;

/* global variables */

int ss[MAX_M], ps[MAX_N];
pii es[MAX_M];

/* subroutines */

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) scanf("%d", ss + i), ss[i]--;

  for (int i = 0; i < n; i++) ps[i] = i;
  
  for (int i = 0; i < m; i++) {
    int u = ss[i], v = ss[i] + 1;
    int p0 = ps[u], p1 = ps[v];
    if (p0 > p1) swap(p0, p1);
    es[i] = {p0, p1};
    swap(ps[u], ps[v]);
  }
  
  sort(es, es + m);
  m = unique(es, es + m) - es;

  printf("%d\n", m);
  
  return 0;
}
0