Submission #471012

# Submission time Handle Problem Language Result Execution time Memory
471012 2021-09-06T16:03:51 Z Shogun3103 Knapsack (NOI18_knapsack) C++17
0 / 100
1 ms 332 KB
#include <bits/stdc++.h>
 
using namespace std;

void setIO(string fileName = ""){
    if(fileName.length()){
        freopen((fileName+".in").c_str(), "r", stdin);
        freopen((fileName+".out").c_str(), "w", stdout);
    }
}

#define ll long long
#define ld long double
#define FOR(i,a,b) for(__typeof(b) i=a; i<=b; i++)
#define FOD(i,a,b) for(__typeof(b) i=b; i>=a; i--)
#define EACH(u, v) for(auto& u : v)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define ii pair<int,int>
#define iii pair<int,ii>
#define ld2 pair<ld, ld>
#define ll2 pair<ll, ll>
#define X first
#define Y second
#define ed "\n"
#define sp " "
#define vt vector
const int oo1 = 1e9 + 7;
const int oo2 = 1e9 + 9;
const int N = 1e5 + 1;
const int M = 5e6 + 1;

int s, n;

void solve()
{
    map<int, vector<pair<int, int>>> cnt;
    cin >> s >> n;
    FOR(i, 1, n) {
        int v, w, k;
        cin >> v >> w >> k;
        if(w <= s && k)
            cnt[w].pb({v, k});
    }

    vector<vector<ll>> best(sz(cnt) + 1, vector<ll>(s + 1, oo1));
    best[0][0] = 0;
    int a = 1;
    for(auto& [w, item] : cnt) {
        sort(item.begin(), item.end(), greater<pair<int, int>>());
        FOR(i, 0, s) {
            best[a][i] = best[a-1][i];
            int c = 0, type = 0, curused = 0;
            ll res = 0;
            while((c + 1) * w <= i && type < sz(item)) {
                c++;
                res += item[type].X;
                if(best[a-1][i-c*w] != oo1) {
                    best[a][i] = max(best[a][i], best[a-1][i-c*w] + res);
                }
                curused++;
                if(curused == item[type].Y) {
                    curused = 0;
                    type++;
                }
            }
        }
        a++;
    }
    cout << *max_element(all(best.back()));
}          

int main()
{ 
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);        
    setIO("");
    int t; 
    t = 1;
    //cin >> t;
    FOR(i, 1, t) {
        solve();
    }
    return 0;
}

Compilation message

knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:7:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 |         freopen((fileName+".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:8:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 |         freopen((fileName+".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -