結果

問題 No.1715 Dinner 2
ユーザー 00 Sakuda
提出日時 2024-04-03 20:18:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,183 bytes
コンパイル時間 2,153 ms
コンパイル使用メモリ 197,800 KB
最終ジャッジ日時 2025-02-20 19:56:49
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace atcoder;
using namespace std;
using mint = modint;
using ll = long long;
ll INF = (1LL << 60);
int main() {
	int N, D;cin >> N >> D;
	vector<ll> P(N + 1, 0), Q(N + 1, 0);
	for (int i = 1;i <= N;i++) cin >> P[i] >> Q[i];
	ll ok = -(1e9 + 100);
	ll ng = 1e9 + 100;
	while (ng - ok > 1) {
		ll X = (ok + ng) / 2;
		vector<ll> dp(N + 2, -INF);
		for (int i = 1;i <= N;i++) if (-P[i] >= X) dp[i] = Q[i] - P[i];
		vector<ll> rmx(N + 1, -INF), lmx(N + 2, -INF);
		for (int i = 1;i <= N;i++) rmx[i] = max(rmx[i - 1], dp[i]);
		for (int i = N;i >= 1;i--) lmx[i] = max(lmx[i + 1], dp[i]);
		for (int i = 2;i <= D;i++) {
			vector<ll> ndp(N + 2, -INF);
			for (int j = 1;j <= N;j++) {
				ll mx = max(rmx[j - 1], lmx[j + 1]);
				if (mx == -INF) continue;
				if (mx - P[j] >= X) {
					ndp[j] = mx + Q[j] - P[j];
				}
			}
			swap(dp, ndp);
			for (int i = 1;i <= N;i++) rmx[i] = max(rmx[i - 1], dp[i]);
			for (int i = N;i >= 1;i--) lmx[i] = max(lmx[i + 1], dp[i]);
		}
		bool ret = false;
		for (int i = 1;i <= N;i++) if (dp[i] >= X) ret = true;
		if (ret) {
			ok = X;
		} else  {
			ng = X;
		}
	}
	cout << ok << endl;
}
0