제출 #1282591

#제출 시각아이디문제언어결과실행 시간메모리
1282591quan606303Knapsack (NOI18_knapsack)C++20
100 / 100
673 ms5380 KiB
/*
 * @Author: RMQuan 
 * @Date: 2025-10-23 15:47:29 
 * @Last Modified by: RMQuan
 * @Last Modified time: 2025-10-23 21:37:57
 */
/*idea : 



*/
#include <bits/stdc++.h>
bool M1;
#define int long long
#define ll long long
#define INTMAX INT_MAX
#define INTMIN INT_MIN
#define LONGMAX LLONG_MAX
#define LONGMIN LLONG_MIN
#define fi first
#define se second
#define memfull(a,b) memset(a,b,sizeof(a));
#define endl '\n'
#define TASK "TEST"
#define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
using namespace std;
const int MOD=1e9+7;
const int LOG=11;
const int maxn=100005;
int dp[2005],S,n,V[maxn],W[maxn],K[maxn];
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    file();
    cin>>S>>n;
    for (int i=1;i<=n;i++)cin>>V[i]>>W[i]>>K[i];
    vector<pair<int,int> > weight[2005];
    for (int i=1;i<=n;i++)weight[W[i]].push_back({V[i],K[i]});
    for (int j=1;j<=2000;j++)
    {
        int need=S/j;
        sort(weight[j].begin(),weight[j].end(),greater<pair<int,int> >());
        for (auto &i:weight[j])
        {
            int used=min(i.se,need);
            vector<int> vt;
            for (int x=0;x<=LOG;x++)
            {
                if (used-(1<<x)>=0)
                {
                    used-=(1<<x);
                    vt.push_back(1<<x);
                }
            }
            if (used)vt.push_back(used);
            for (auto u:vt)
            {
                for (int x=S;x>=u*j;x--)dp[x]=max(dp[x],dp[x-u*j]+u*i.fi);
            }
            need-=used;
            if (need==0)break;
        }
    }
    cout<<dp[S];
    bool M2;
    cerr<<"-------------------------------------------------"<<endl;
    cerr<<"Time : "<<clock()<<" ms"<<endl;
    cerr<<"Memory : "<<abs(&M2-&M1)/1024/1024<<" MB"<<endl;
    cerr<<"-------------------------------------------------"<<endl;
    return 0;
}

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

knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:25:50: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
      |                                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:36:5: note: in expansion of macro 'file'
   36 |     file();
      |     ^~~~
knapsack.cpp:25:81: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
      |                                                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:36:5: note: in expansion of macro 'file'
   36 |     file();
      |     ^~~~
#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...