Submission #645642

#TimeUsernameProblemLanguageResultExecution timeMemory
645642ghostwriterKnapsack (NOI18_knapsack)C++14
12 / 100
279 ms262144 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 N = 1e5 + 1;
const int S = 2e3 + 1;
int s, n, a[S][S];
vi d[S][S];
vpi d1[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;
    	d1[w].pb({v, k});
    }
    FOR(i, 1, s) {
    	int pos = 0, cnt = 0;
    	sort(all(d1[i]), greater<pi>());
    	EACH(j, d1[i]) {
    		int v = j.st, k = j.nd;
    		k = min(k, s / i - cnt);
    		FOR(z, 1, k) a[i][++pos] = v;
    		cnt += k;
    	}
    	cerr << i << " : ";
    	FOR(j, 1, pos) cerr << a[i][j] << ' ';
    	cerr << '\n';
    }
    FOR(i, 1, s)
    FOR(j, 0, s)
    	d[i][j].resize(s / i + 1, 0);
    FOR(i, 1, s)
    FOR(j, 0, s)
    FOS(z, s / i, 1) {
    	int &rs = d[i][j][z];
    	if (z < s / i) {
    		rs = max(rs, d[i][j][z + 1]);
    		if (j >= i) rs = max(rs, d[i][j - i][z + 1] + a[i][z]);
    	}
    	else if (i > 1) {
    		rs = max(rs, d[i - 1][j][1]);
    		if (j >= i) rs = max(rs, d[i - 1][j - i][1] + a[i][z]);
    	}
    	else if (j >= i) rs = max(rs, a[i][z]);
    }
    cout << d[s][s][1];
    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:59:5: note: in expansion of macro 'FOR'
   59 |     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:64:5: note: in expansion of macro 'FOR'
   64 |     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:67:6: note: in expansion of macro 'EACH'
   67 |      EACH(j, d1[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:70:7: note: in expansion of macro 'FOR'
   70 |       FOR(z, 1, k) a[i][++pos] = v;
      |       ^~~
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:74:6: note: in expansion of macro 'FOR'
   74 |      FOR(j, 1, pos) cerr << a[i][j] << ' ';
      |      ^~~
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:77:5: note: in expansion of macro 'FOR'
   77 |     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:78:5: note: in expansion of macro 'FOR'
   78 |     FOR(j, 0, s)
      |     ^~~
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:80:5: note: in expansion of macro 'FOR'
   80 |     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:81:5: note: in expansion of macro 'FOR'
   81 |     FOR(j, 0, s)
      |     ^~~
knapsack.cpp:31:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   31 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
knapsack.cpp:82:5: note: in expansion of macro 'FOS'
   82 |     FOS(z, s / i, 1) {
      |     ^~~
#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...