제출 #1106331

#제출 시각아이디문제언어결과실행 시간메모리
1106331GuessWhoHas2CatsKnapsack (NOI18_knapsack)C++14
17 / 100
1 ms592 KiB
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int W, n; 
    cin >> W >> n;
    
    vi vals(n), wes(n), copies(n);
    for (int i = 0; i < n; i++) {
        cin >> vals[i] >> wes[i] >> copies[i];
    }
    
    // 使用一维数组进行滚动优化
    vi dp(W + 1, 0);
    
    for (int i = 0; i < n; i++) {
        int val = vals[i], we = wes[i], cop = copies[i];
        
        // 使用二进制拆分法来处理副本
        for (int k = 1; k <= cop; k *= 2) {
            int count = min(cop, k);
            dp[W] = max(dp[W], dp[W - count * we] + count * val);
            for (int j = W; j >= count * we; j--) {
                dp[j] = max(dp[j], dp[j - count * we] + count * val);
            }
            cop -= count;
        }
        
        // 处理剩余的副本
        if (cop > 0) {
            for (int j = W; j >= cop * we; j--) {
                dp[j] = max(dp[j], dp[j - cop * we] + cop * val);
            }
        }
    }
    
    cout << dp[W];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...