关于此题中的动态规划赋初值
查看原帖
关于此题中的动态规划赋初值
956071
Xiongzx楼主2023/3/6 19:23
#include <bits/stdc++.h>
    
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define PIL pair<int, long long>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sf scanf
#define prf printf
#define el putchar('\n')
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define mmc(arr1, arr2) memcpy(arr1, arr2, sizeof(arr2))
const int inf = 0x3f3f3f3f;
//const int mod = ;

template <typename T> inline void rd(T &x){
    x = 0; bool f = true; char ch = getchar();
    while(ch < '0' || ch > '9'){ f = ((ch == '-') ? false : true); ch = getchar();}
    while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar();}
    if(!f) x = -x;
}
template <typename T, typename ...Args> inline void rd(T &x, Args &...args){ rd(x); rd(args...);}

using namespace std;

const int N = 300, M = 1010;
int n, maxw;
int w[N], t[N];
double f[M], eps = 1e-5;

bool check(double x){
    for(int i = 1; i < M; i++) f[i] = -1e9; f[0] = 0; // 如何保证代码正确,为什么,赋值为-1e9有没有可能让f[maxw]变为好小的数
    for(int i = 1; i <= n; i++){
        for(int j = maxw; j >= 0; j--){
            int k = min(maxw, j + w[i]);
            f[k] = max(f[k], f[j] + (t[i] - w[i] * x));
        }
    }
    return f[maxw] >= 0;
}

int main(){
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    ios::sync_with_stdio(false);
    cin >> n >> maxw;
    for(int i = 1; i <= n; i++) cin >> w[i] >> t[i];
    double l = 0, r = 1000;
    for(int i = 1; i <= 30; i++){
        double mid = (l + r) / 2.0;
        if(check(mid)) l = mid;
        else r = mid;
    }
    prf("%d\n", (int)(1000 * r));
    return 0;
}
2023/3/6 19:23
加载中...