結果

問題 No.2157 崖
ユーザー 👑 chro_96chro_96
提出日時 2022-12-09 22:19:36
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,352 bytes
コンパイル時間 249 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 37,152 KB
最終ジャッジ日時 2024-04-22 22:27:26
合計ジャッジ時間 6,043 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
36,968 KB
testcase_01 AC 9 ms
36,984 KB
testcase_02 AC 9 ms
36,892 KB
testcase_03 AC 10 ms
36,864 KB
testcase_04 AC 10 ms
36,916 KB
testcase_05 AC 10 ms
36,916 KB
testcase_06 AC 10 ms
36,880 KB
testcase_07 AC 8 ms
36,752 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 10 ms
36,900 KB
testcase_11 WA -
testcase_12 AC 10 ms
36,936 KB
testcase_13 WA -
testcase_14 AC 392 ms
37,028 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 343 ms
36,816 KB
testcase_18 AC 323 ms
36,732 KB
testcase_19 WA -
testcase_20 AC 423 ms
36,840 KB
testcase_21 AC 429 ms
36,860 KB
testcase_22 AC 316 ms
36,784 KB
testcase_23 AC 10 ms
36,976 KB
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

#define INF 1000000000000000LL

int cmp_ll (const void *ap, const void *bp) {
  long long a = *(long long *)ap;
  long long b = *(long long *)bp;
  
  if (a < b) {
    return -1;
  }
  
  if (a > b) {
    return 1;
  }
  
  return 0;
}

int main () {
  int n = 0;
  int m = 0;
  long long d[1500][1500] = {};
  
  int res = 0;
  
  long long dp[1500][1500] = {};
  long long ans = INF;
  
  res = scanf("%d", &n);
  res = scanf("%d", &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      res = scanf("%lld", d[i]+j);
    }
    qsort(d[i], m, sizeof(long long), cmp_ll);
  }
  
  for (int i = 1; i < n; i++) {
    int idx = -1;
    for (int j = 0; j < m; j++) {
      if (dp[i-1][j] < INF) {
        dp[i-1][j] -= d[i-1][j];
      }
    }
    for (int j = 1; j < m; j++) {
      if (dp[i-1][j] > dp[i-1][j-1]) {
        dp[i-1][j] = dp[i-1][j-1];
      }
    }
    for (int j = 0; j < m; j++) {
      while (idx < m-1 && d[i][j] >= d[i-1][idx+1]) {
        idx++;
      }
      if (idx < 0) {
        dp[i][j] = INF;
      } else {
        dp[i][j] = dp[i-1][idx]+d[i][j];
      }
    }
  }
  
  for (int i = 0; i < m; i++) {
    if (ans > dp[n-1][i]) {
      ans = dp[n-1][i];
    }
  }
  
  if (ans >= INF) {
    printf("-1\n");
  } else {
    printf("%lld\n", ans);
  }
  
  return 0;
}
0