Submission #645655

#TimeUsernameProblemLanguageResultExecution timeMemory
645655ghostwriterKnapsack (NOI18_knapsack)C++14
100 / 100
129 ms42316 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int S = 2e3 + 5;
int s, n, a[S][S];
int d[S][S], d1[S][S];
vpi d2[S];
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> s >> n;
    FOR(i, 1, n) {
    	int v, w, k;
    	cin >> v >> w >> k;
    	d2[w].pb({v, k});
    }
    FOR(i, 1, s) {
    	int pos = 0, cnt = 0;
    	sort(all(d2[i]), greater<pi>());
    	EACH(j, d2[i]) {
    		int v = j.st, k = j.nd;
    		k = min(k, s / i - cnt);
    		FOR(z, 1, k) a[i][++pos] = v;
    		cnt += k;
    	}
    }

    FOR(i, 1, s) {
	    FOR(j, 1, s / i)
	    FOR(z, 0, s) {
	    	int &rs = d[j][z];
	    	rs = 0;
	    	if (j > 1) {
	    		rs = max(rs, d[j - 1][z]);
	    		if (z >= i) rs = max(rs, d[j - 1][z - i] + a[i][j]);
	    	}
	    	else {
	    		rs = max(rs, d1[i > 1? s / (i - 1) : 0][z]);
	    		if (z >= i) rs = max(rs, d1[i > 1? s / (i - 1) : 0][z - i] + a[i][j]);
	    	}
	    }
	    FOR(j, 1, s / i)
	    FOR(z, 0, s)
	    	d1[j][z] = d[j][z];
	}
    cout << d[1][s];
    return 0;
}
/*
15 5
4 12 1
2 1 1
10 4 1
1 1 1
2 2 1
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
knapsack.cpp:58:5: note: in expansion of macro 'FOR'
   58 |     FOR(i, 1, n) {
      |     ^~~
knapsack.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
knapsack.cpp:63:5: note: in expansion of macro 'FOR'
   63 |     FOR(i, 1, s) {
      |     ^~~
knapsack.cpp:34:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   34 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
knapsack.cpp:66:6: note: in expansion of macro 'EACH'
   66 |      EACH(j, d2[i]) {
      |      ^~~~
knapsack.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
knapsack.cpp:69:7: note: in expansion of macro 'FOR'
   69 |       FOR(z, 1, k) a[i][++pos] = v;
      |       ^~~
knapsack.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
knapsack.cpp:74:5: note: in expansion of macro 'FOR'
   74 |     FOR(i, 1, s) {
      |     ^~~
knapsack.cpp:30:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
knapsack.cpp:75:6: note: in expansion of macro 'FOR'
   75 |      FOR(j, 1, s / i)
      |      ^~~
knapsack.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
knapsack.cpp:76:6: note: in expansion of macro 'FOR'
   76 |      FOR(z, 0, s) {
      |      ^~~
knapsack.cpp:30:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
knapsack.cpp:88:6: note: in expansion of macro 'FOR'
   88 |      FOR(j, 1, s / i)
      |      ^~~
knapsack.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
knapsack.cpp:89:6: note: in expansion of macro 'FOR'
   89 |      FOR(z, 0, s)
      |      ^~~
#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...