添加 T637677 问题的完整解决方案,包括输入处理、动态规划实现和输出逻辑

This commit is contained in:
Zengtudor 2025-07-23 11:32:21 +08:00
parent 391e21c91d
commit 767c57290d

115
src/7/23/T637677d.cpp Normal file
View File

@ -0,0 +1,115 @@
/*
solution of https://www.luogu.com.cn/problem/T637677
*/
#include <cstdint>
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
#include <algorithm>
using namespace std;
using ll = int64_t;
const ll f = 50000;
const ll msize = 100001;
const ll inf = 1000000;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, m;
cin >> n >> m;
vector<ll> d(n);
long long T0 = 0;
for (ll i = 0; i < n; i++) {
ll a, b;
cin >> a >> b;
d[i] = a - b;
T0 += d[i];
}
vector<ll> dp0(msize, inf);
vector<ll> dp1(msize, inf);
if (0 + f >= 0 && 0 + f < msize) {
dp0[0 + f] = 0;
}
ll nx0 = d[0] + f;
if (nx0 >= 0 && nx0 < msize) {
dp1[nx0] = 1;
}
for (ll i = 1; i < n; i++) {
vector<ll> ndp0(msize, inf);
vector<ll> ndp1(msize, inf);
for (ll x = 0; x < msize; x++) {
ndp0[x] = min(dp0[x], dp1[x]);
}
for (ll x = 0; x < msize; x++) {
if (dp0[x] != inf) {
ll nx = x + d[i];
if (nx >= 0 && nx < msize) {
if (dp0[x] + 1 < ndp1[nx]) {
ndp1[nx] = dp0[x] + 1;
}
}
}
}
for (ll x = 0; x < msize; x++) {
if (dp1[x] != inf) {
ll nx = x + d[i];
if (nx >= 0 && nx < msize) {
if (dp1[x] < ndp1[nx]) {
ndp1[nx] = dp1[x];
}
}
}
}
dp0 = move(ndp0);
dp1 = move(ndp1);
}
ll ans = inf;
for (ll x = 0; x < msize; x++) {
if (dp0[x] == inf && dp1[x] == inf) continue;
long long xv = (long long)(x - f);
if (abs(T0 - 2 * xv) <= m) {
if (dp0[x] < ans) ans = dp0[x];
if (dp1[x] < ans) ans = dp1[x];
}
}
if (ans == inf) {
ans = n;
}
cout << ans << endl;
return 0;
}
/*
n和目标值md_i和初始总差T0
DP数组dp0和dp1分别存储不在区间中和在区间中的状态100001X的偏移范围
X=0X=d[0]
dp0dp0或dp1不翻转当前骨牌转移而来
dp1dp0翻转当前骨牌+1dp1翻转当前骨牌
X值|T0 - 2X| m
n
O(n × range_size)range_size为100001
*/