結果

問題 No.2139 K Consecutive Sushi
ユーザー 👑 chro_96chro_96
提出日時 2022-12-03 00:30:01
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,351 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 30,208 KB
実行使用メモリ 8,140 KB
最終ジャッジ日時 2024-05-05 19:21:49
合計ジャッジ時間 2,659 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,056 KB
testcase_01 AC 4 ms
8,064 KB
testcase_02 AC 3 ms
8,104 KB
testcase_03 AC 66 ms
7,936 KB
testcase_04 AC 78 ms
8,040 KB
testcase_05 AC 74 ms
8,064 KB
testcase_06 AC 64 ms
8,064 KB
testcase_07 AC 69 ms
8,064 KB
testcase_08 AC 71 ms
7,916 KB
testcase_09 AC 75 ms
7,936 KB
testcase_10 AC 75 ms
8,044 KB
testcase_11 AC 73 ms
7,916 KB
testcase_12 AC 71 ms
8,064 KB
testcase_13 AC 4 ms
8,064 KB
testcase_14 AC 4 ms
7,936 KB
testcase_15 AC 3 ms
8,064 KB
testcase_16 AC 4 ms
8,064 KB
testcase_17 AC 3 ms
8,064 KB
testcase_18 AC 4 ms
8,140 KB
testcase_19 AC 5 ms
8,064 KB
testcase_20 AC 4 ms
8,064 KB
testcase_21 AC 4 ms
8,064 KB
testcase_22 AC 4 ms
8,064 KB
testcase_23 AC 10 ms
8,064 KB
testcase_24 AC 26 ms
8,064 KB
testcase_25 AC 52 ms
8,100 KB
testcase_26 AC 10 ms
8,020 KB
testcase_27 AC 22 ms
8,064 KB
testcase_28 AC 21 ms
8,064 KB
testcase_29 AC 12 ms
8,064 KB
testcase_30 AC 27 ms
8,064 KB
testcase_31 AC 69 ms
8,032 KB
testcase_32 AC 13 ms
8,064 KB
testcase_33 AC 68 ms
8,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

void set_segt (long long *t, int idx, long long val, int size) {
  idx = idx+size-1;
  t[idx] = val;
  
  while (idx > 0) {
    idx = (idx-1)/2;
    if (t[2*idx+1] < t[2*idx+2]) {
      t[idx] = t[2*idx+1];
    } else {
      t[idx] = t[2*idx+2];
    }
  }
  
  return;
}

long long min_segt_rec (long long *t, int a, int b, int k, int l, int r) {
  long long ans = 0LL;
  long long tmp = 0LL;
  
  if (r <= a || b <= l) {
    return 1000000000000000LL;
  }
  
  if (a <= l && r <= b) {
    return t[k];
  }
  
  ans = min_segt_rec(t, a, b, 2*k+1, l, (l+r)/2);
  tmp = min_segt_rec(t, a, b, 2*k+2, (l+r)/2, r);
  
  if (tmp < ans) {
    ans = tmp;
  }
  
  return ans;
}

long long min_segt (long long *t, int a, int b, int size) {
  return min_segt_rec(t, a, b, 0, 0, size);
}

int main () {
  int n = 0;
  int k = 0;
  long long a[200000] = {};
  
  int res = 0;
  
  long long sum = 0LL;
  long long segt[600000] = {};
  
  int size = 1;
  
  res = scanf("%d", &n);
  res = scanf("%d", &k);
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", a+i);
    sum += a[i];
  }
  
  while (size < n+1) {
    size *= 2;
  }
  
  for (int i = 0; i < n; i++) {
    long long val = min_segt(segt, i-k+1, i+1, size);
    set_segt(segt, i+1, val+a[i], size);
  }
  
  printf("%lld\n", sum-min_segt(segt, n-k+1, n+1, size));
  return 0;
}
0