結果

問題 No.3091 The Little Match Boy
ユーザー tnakao0123
提出日時 2025-04-08 17:22:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 801 bytes
コンパイル時間 517 ms
コンパイル使用メモリ 46,080 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-04-08 17:22:55
合計ジャッジ時間 3,306 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 62
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:32:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   32 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:33:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   33 |   for (int i = 0; i < m; i++) scanf("%d", ss + i), ss[i]--;
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- 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