Submission #914176

#TimeUsernameProblemLanguageResultExecution timeMemory
914176May27_thKnapsack (NOI18_knapsack)C++17
12 / 100
1 ms348 KiB
#include <bits/stdc++.h>
#define int64_t long long
#define double long double
using namespace std;
using type = int64_t;
const long long mod = 1000000007, inf = 1e18;
const int base = 33;
const int N = 2e5 + 10;
const int LG = 20;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void Minimize(int64_t &a, int64_t b) {if(b < a) a = b;}
void Maximize(int64_t &a, int64_t b) {if(b > a) a = b;}
void Add(int64_t& a, int64_t b) {a = a + b; a %= mod;}
void Dec(int64_t& a, int64_t b) {a = a - b + mod; a %= mod;}

int n, S;
struct Item{
    int64_t v, w, k;
};
pair<int64_t, int64_t> f[3000], g[3000];
void Solve(void)
{
    /**
        n <= 1e5, k <= 1e9; S <= 2000
        f[i][j] la max value khi den thg i va dung j space
        f[i][j] = f[i - 1][j - w * t] + v * t (t <= k)
        f[i][j] = f[i - 1][j];
        g[j] = f[j]
        g[j] = f[j - w * t] + v * t (t <= min(k, 2000))
    **/
    cin >> S >> n;
    vector<Item>items;
    for(int i = 1; i <= n; i ++){
        int64_t v, w, k; cin >> v >> w >> k;
        items.push_back({v, w, k});
    }
    f[0] = make_pair(0, 0);
    for(int i = 1; i <= n; i ++){
        int64_t w = items[i - 1].w, v = items[i - 1].v, k = items[i - 1].k;
        for(int j = 0; j <= S; j ++){
            g[j] = make_pair(f[j].first, 0);
            if(j >= w){
                //Maximize(g[j], g[j - w] + v);
                if(g[j].first < g[j - w].first + v && g[j - w].second + 1 <= k){
                    g[j] = make_pair(g[j - w].first + v, g[j - w].second + 1);
                }
                else if(g[j].first == g[j - w].first + v){
                    if(g[j].second > g[j - w].second + 1){
                        g[j].second = g[j - w].second + 1;
                    }
                }
            }
            /*for(int t = 1; t <= min(k, j/w); t ++){
                Maximize(g[j], f[j - w * t] + v * t);
            }*/
        }
        //for(int j = 0; j <= S; j ++) Maximize(g[j], f[j]);
        for(int j = 0; j <= S; j ++){
            swap(g[j], f[j]);
        }
    }
    int64_t ans = 0;
    for(int s = 0; s <= S; s ++){
        ans = max(ans, f[s].first);
    }
    cout << ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    if(fopen("time.in", "r")){
        freopen("time.in", "r", stdin);
        freopen("time.out", "w", stdout);
    }
    if(fopen("A.inp", "r")){
        freopen("A.inp", "r", stdin);
        freopen("A.out", "w", stdout);
    }
    int tc = 1;
    //cin >> tc;
    while(tc --){
        Solve();
    }
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen("time.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         freopen("time.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen("A.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen("A.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...