제출 #1276931

#제출 시각아이디문제언어결과실행 시간메모리
1276931nhmktuKnapsack (NOI18_knapsack)C++17
0 / 100
157 ms327680 KiB
#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pi pair<int,int>
#define pii pair<int,pi>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) (int) a.size()
#define endl '\n'
#define data "Knapsack"

using namespace std;
const ll linf = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7, N = 1e5;

void add(int &a, int b)
{
    a += b;
    if(a>=MOD)
        a-=MOD;
    if(a<0)
        a += MOD;
}

int modulo(int x)
{
    if(x<=1)
        return 1;
    return (MOD - MOD/x) * modulo(MOD/x) % MOD;
}

int mul(int a, int b)
{
    return (1ll *a%MOD * b%MOD) % MOD;
}

int S, n;
map< int, vector<pi> > M;
ll dp[N+3][2003];

void inp(void)
{
    cin >> S >> n;
    FOR(i, 1, n)
    {
        int v, w, k;
        cin >> v >> w >> k;
        if(w <= S && k)
            M[w].pb(make_pair(v, k));
    }
}

void solve(void)
{
    memset(dp, -0x3f, sizeof(dp));
    dp[0][0] = 0;
    int i = 1;
    for(map<int, vector<pi> > ::iterator it = M.begin(); it != M.end(); ++it)
    {
        vector<pi> &V = it -> se;
        int w = it -> fi;
        sort(all(V), greater<pi>());

        FOR(j, 0, S)
        {
            dp[i][j] = dp[i-1][j];
            int used = 0;
            int W = 0;
            int p = 0;
            ll cost = 0;

            while(W + w <= j && p < sz(V))
            {
                W += w;
                cost += V[p].fi;
                ++used;
                dp[i][j] = max(dp[i][j], dp[i-1][j - W] + cost);
                if(used == V[p].se)
                {
                    used = 0;
                    ++p;
                }
            }
        }

        ++i;
    }

    cout << *max_element(dp[i-1], dp[i-1]+S+1);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    if(fopen(data".inp", "r"))
    {
        freopen(data".inp","r",stdin);
        freopen(data".out","w",stdout);
    }

    inp();
    solve();
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'int main()':
knapsack.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |         freopen(data".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         freopen(data".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...