結果

問題 No.448 ゆきこーだーの雨と雪 (3)
ユーザー tnakao0123
提出日時 2016-11-24 16:39:58
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,603 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 83,904 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-06-11 19:38:31
合計ジャッジ時間 4,289 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:74:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   74 |   scanf("%d%d", &n, &k);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:78:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   78 |     scanf("%d%d", ts + i, ds + i);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 448-1.cc: No.448 ゆきこーだーの雨と雪 (3) - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_T = 1000000000;
const int MAX_D = 1000000000;

const int TINF = MAX_T * 2 + 10;
typedef long long ll;
const ll LINF = 1LL << 62;

/* typedef */

/* global variables */

int ts[MAX_N + 1], ds[MAX_N + 1];
ll dsums[MAX_N + 1], dp[MAX_N + 1];

/* subroutines */

ll check(int n, int k, int maxd) {
  fill(dp, dp + n + 1, LINF);
  dp[0] = 0;

  for (int i = 0, j = 0, pi = -1; i < n; i++) {
    while (ts[j] < ts[i] + k) {
      if (ds[j] > maxd) pi = j;
      j++;
    }

    if (ds[i] <= maxd) {
      ll d0 = dp[i] + ds[i];
      if (dp[i + 1] > d0) dp[i + 1] = d0;
    }

    if (pi <= i) {
      ll d1 = dp[i] + dsums[j] - dsums[i + 1];
      if (dp[j] > d1) dp[j] = d1;
    }
  }

  return dp[n];
}

/* main */

int main() {
  int n, k;
  scanf("%d%d", &n, &k);

  dsums[0] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", ts + i, ds + i);
    dsums[i + 1] = dsums[i] + ds[i];
  }
  ts[n] = TINF, ds[n] = 0;

  int dl = -1, dr = MAX_D;
  while (dl + 1 < dr) {
    int d = (dl + dr) / 2;
    if (check(n, k, d) >= LINF) dl = d;
    else dr = d;
  }

  printf("%d\n%lld\n", dr, check(n, k, dr));
  return 0;
}
0