Submission #477500

# Submission time Handle Problem Language Result Execution time Memory
477500 2021-10-02T10:32:34 Z kaxzert Knapsack (NOI18_knapsack) C++17
0 / 100
1 ms 332 KB
/**
      00  00      11      00  00  111111  00000  111111  000000
      00 00      1111      0000      11   00     11  11  000000
      0000      11  11      00      11    00000  111111    00
      00 00    11111111    0000    11     00     11 11     00
      00  00  11      11  00  00  111111  00000  11  11    00
**/

#include<bits/stdc++.h>

using namespace std;

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

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

#define fto(i, a, b) for(int i = a; i <= b; ++i)
#define fdto(i, a, b) for(int i = a; i >= b; --i)
#define bugarr(a, i, j) cout << #a << "{" << i << "..." << j << "}:"; fto(k, i, j-1) cout << a[k] << ", "; cout << a[j] << endl;
#define ll long long
#define db double
#define ldb long double
#define ii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vt vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(i, a) for(auto &i : a)
#define sz(a) (int)a.size()
#define fast ios::sync_with_stdio(false); cin.tie(0)

template<typename T, typename V>
bool ckmin(T &a, V b) {return (b < a)? a = b, true : false;}
template<typename T, typename V>
bool ckmax(T &a, V b) {return (b > a)? a = b, true : false;}

int main() {

    fast;
    int s, n;
    cin >> s >> n;
    vt<vt<pair<ll, ll> > > m(s+1);
    vt<vt<ll> > temp(s+1);
    fto(i, 1, n) {
        int v, w, k;
        cin >> v >> w >> k;
        m[w].eb(mp(v, k));
    }

    vt<int> sth;
    fto(i, 1, s) {
        if (m[i].empty()) continue;
        sth.pb(i);
        sort(all(m[i]), [](pair<ll, ll> a, pair<ll, ll> b)->bool {
            return a.ff < b.ff;
        });
        int need = s/i;
        while(!m[i].empty() && need > 0) {
            temp[i].pb(m[i][sz(m[i])-1].ff);
            if (sz(temp[i]) > 1) temp[i][sz(temp[i])-1] += temp[i][sz(temp[i])-2];
            if ((m[i][sz(m[i])-1].ss -= 1) == 0) {
                m[i].pop_back();
            }
            --need;
        } 
    }

    vt<vt<ll> > f(2, vt<ll>(s+5, 0));

    fto(i, 0, sz(sth)-1) {
        int li = i&1;
        int w = sth[i];
        fto(j, 1, s) f[li][j] = f[li^1][j];
        fto(j, 0, sz(temp[w])-1) {
            fto(k, w*(j+1), s) {
                f[li][k] = max(f[li][k], f[li^1][k-w*(j+1)] + temp[w][j]);
            }
        }
    }

    ll ans = 0;
    fto(i, 1, s) {
        ans = max(ans, f[n&1][i]);
    }

    cout << ans << '\n';

    return 0;
}

Compilation message

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