Submission #1083901

#TimeUsernameProblemLanguageResultExecution timeMemory
1083901tkvinhtruongquangKnapsack (NOI18_knapsack)C++14
100 / 100
46 ms3868 KiB
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define pf push_front
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define clz __builtin_clz
#define popcount __builtin_popcount
#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)
#define loop(i, j, n, k) for(int i = j; i <= n; i += k)
#define fast ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)

template <typename T>
inline void print(T x) { cerr << x << endl; }
template <typename T>
inline void print(vector <T> x) { cerr << '['; loop(i, 0, sz(x) - 2, 1) cerr << x[i] << ", "; cerr << x.back() << ']' << endl; }
template <typename T>
inline bool maximize(T &a, const T &b) { return a < b ? (a = b) : 0; }
template <typename T>
inline bool minimize(T &a, const T &b) { return a > b ? (a = b) : 0; }

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using pss = pair<string, string>;
using vll = vector<ll>;
using pll = pair<ll, ll>;

#define debug(x) cerr << #x << " = "; print(x)

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
    freopen("error.txt", "w", stderr);
}

int main() {
    // setIO("task");

    fast;
    int cap, n; cin >> cap >> n;
    map<int, vector<pii>> by_weight;
    
    loop(i, 0, n - 1, 1) {
        int v, w, a;
        cin >> v >> w >> a;
        by_weight[w].pb({v, a});
    }

    vll dp(cap + 1, 0); dp[0] = 0;

    for(auto &[weight, items] : by_weight) {
        sort(items.rbegin(), items.rend());
        
        int copies = 1;
        int item_indx = 0;

        vll prefix(sz(items));
        prefix[0] = items[0].se;
        loop(i, 1, sz(items) - 1, 1) 
            prefix[i] = items[i].se += prefix[i - 1];

        while(copies * weight <= cap && item_indx < sz(items)) {
            for(int i = cap; i >= copies * weight; -- i) {
                dp[i] = max(dp[i], dp[i - weight] + items[item_indx].fi);
            }

            copies ++;
            if(copies > prefix[item_indx]) ++ item_indx;
        }
    }       

    cout << dp[cap] << '\n';
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:56:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |     for(auto &[weight, items] : by_weight) {
      |               ^
knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     freopen("error.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...