結果

問題 No.3017 交互浴
ユーザー tnakao0123
提出日時 2025-01-28 16:26:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 913 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 56,872 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-01-28 16:26:24
合計ジャッジ時間 13,692 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 55
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:35:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   35 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:36:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |   for (int i = 0; i < n; i++) scanf("%d", hs + i);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3017.cc:  No.3017 莠、莠呈オエ - yukicoder
 */

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

using namespace std;

/* constant */

const int MAX_N = 200000;
const int INF = 1 << 30;

enum { G, B };

/* typedef */

using pii = pair<int,int>;
using stpii = stack<pii>;

/* global variables */

int hs[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", hs + i);

  int sum = 0;
  stpii q;
  q.push({INF, G});

  for (int i = 0; i < n; i++) {
    while (q.top().first <= hs[i]) {
      auto [h, c] = q.top(); q.pop();
      if (c == G) sum += h;
      else sum -= h;
    }

    int ci = ((i & 1) == 0) ? B : G;
    if (q.top().second != ci) {
      if (ci == G) sum -= hs[i];
      else sum += hs[i];
      q.push({hs[i], ci});
    }

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