結果

問題 No.1948 足し算するだけのパズルゲーム(1)
ユーザー RVindicatio
提出日時 2022-05-20 22:19:22
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,822 bytes
コンパイル時間 11,534 ms
コンパイル使用メモリ 285,860 KB
最終ジャッジ日時 2025-01-29 10:52:25
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;

using ll = long long;
const int INF = 1e9;
const ll inf = 1LL<<60;

template<typename T>
bool chmin(T& x, const T& y){
  if(x <= y) return false;
  x = y;
  return true;
}
template<typename T>
bool chmax(T& x, const T& y){
  if(x >= y) return false;
  x = y;
  return true;
}

void solve() {
  int h, w; cin >> h >> w;
  vector<vector<ll>> a(h, vector<ll>(w));
  for (int i=0; i<h; i++) for (int j=0; j<w; j++) cin >> a[i][j];
  vector<vector<vector<ll>>> dp(h, vector<vector<ll>>(w, vector<ll>(2, -inf)));
  dp[0][0][0] = a[0][0];
  for (int i=0; i<h; i++) for (int j=0; j<w; j++) {
    if (i-1 >= 0) {
      if (dp[i-1][j][0] > a[i][j]) {
        chmax(dp[i][j][0], dp[i-1][j][0] + a[i][j]);
      } else {
        chmax(dp[i][j][1], dp[i-1][j][0]);
      }
      if (dp[i-1][j][1] > a[i][j]) {
        chmax(dp[i][j][1], dp[i-1][j][1] + a[i][j]);
      }
    }
    if (j-1 >= 0) {
      if (dp[i][j-1][0] > a[i][j]) {
        chmax(dp[i][j][0], dp[i][j-1][0] + a[i][j]);
      } else {
        chmax(dp[i][j][1], dp[i][j-1][0]);
      }
      if (dp[i][j-1][1] > a[i][j]) {
        chmax(dp[i][j][1], dp[i][j-1][1] + a[i][j]);
      }
    }
  }
  // for (int i=0; i<h; i++) {
  //   for (int j=0; j<w; j++) {
  //     cout << dp[i][j][0] << " ";
  //   }
  //   cout << endl;
  // }
  // for (int i=0; i<h; i++) {
  //   for (int j=0; j<w; j++) {
  //     cout << dp[i][j][1] << " ";
  //   }
  //   cout << endl;
  // }
  ll ma = max({dp[h-2][w-1][0], dp[h-2][w-1][1], dp[h-1][w-2][0], dp[h-1][w-2][1]});
  cout << (ma > a[h-1][w-1] ? "Yes" : "No") << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  // int t; cin >> t;
  /*while (t--)*/ solve();
}
0