結果

問題 No.561 東京と京都
コンテスト
ユーザー yuppe19 😺
提出日時 2017-08-25 22:39:57
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 787 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,425 ms
コンパイル使用メモリ 170,768 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-03-08 16:13:58
合計ジャッジ時間 1,819 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:8:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    8 |   int n; i64 d; scanf("%d%lld", &n, &d);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:11:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |     scanf("%lld%lld", &T[i], &K[i]);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
using namespace std;
using i64 = long long;

int main(void) {
  constexpr i64 INIT = i64(powl(10, 15));
  int n; i64 d; scanf("%d%lld", &n, &d);
  vector<i64> T(n), K(n);
  for(int i=0; i<n; ++i) {
    scanf("%lld%lld", &T[i], &K[i]);
  }
  vector<vector<i64>> dp(n+1, vector<i64>(2, 0)); // dp[n+1][2]
  dp[0][0] = INIT;

  for(int i=0; i<n; ++i) {
    // 東京 -> 東京
    dp[i+1][0] = max(dp[i+1][0], dp[i][0] + T[i]);
    // 東京 -> 京都
    dp[i+1][1] = max(dp[i+1][1], dp[i][0] - d + K[i]);
    // 京都 -> 東京
    dp[i+1][0] = max(dp[i+1][0], dp[i][1] - d + T[i]);
    // 京都 -> 京都
    dp[i+1][1] = max(dp[i+1][1], dp[i][1] + K[i]);
  }
  i64 res = max(dp[n][0], dp[n][1]) - INIT;
  printf("%lld\n", res);

  return 0;
}
0