Submission #865775

#TimeUsernameProblemLanguageResultExecution timeMemory
865775thundereyexKnapsack (NOI18_knapsack)C++14
49 / 100
3 ms604 KiB
#include<bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() int main(){ long long int n,k; cin>>n>>k; int max2=0; vector<vector<long long int>>v1; vector<long long int>v2; for(int i=0;i<k;i++){ long long int a,b,c; cin>>a>>b>>c; if(b<=n){ long long int g= n/b; // for(int j=0;j<min(g,c);j++){ v2.push_back(a); v2.push_back(b); v2.push_back(min(g,c)); v1.push_back(v2); v2.clear(); int f=min(g,c); max2=max({max2,f}); // } } // v2.clear(); } vector<long long int>dp(n+1,0); // cout<<max2<<endl; // sort(v1.begin(),v1.end(),greater<vector<int>>()); if (1 < v1.size() && v1.size() <= 1e5 && max2 <= 1e9&&max2>10&&v1.size()>=100) { /* Observation: The numbers of taken product must less than S since 1 <= Wi <= S -> maximum taken product: S / W[i] -> consider the top valued S/W[i] items for each W[i] */ vector<pair<int,int>> obj[n]; long long int dp[2001]; memset(dp, 0, sizeof dp); for (int i = 1; i <= v1.size(); i++) { obj[v1[i][1]].push_back({v1[i][0],v1[i][2]}); } for (int i = 0; i <= n; i++) { if (obj[i].size() == 0) continue; sort(all(obj[i]), greater<pair<int, int>>()); int id = 0; for (int j = 0; j <=n; j++) { if (id >= obj[i].size()) break; for (int k = n; k >= i; k--) { dp[k] = max(dp[k], dp[k - i] + obj[i][id].first); } --obj[i][id].second; if (obj[i][id].second == 0) ++id; } } cout << dp[n] << endl; } else{ // cout<<"ff"<<endl; // if(n>=1&&n<=100&&max2<=10){ long long int max1=0; for (int i = 1; i <= v1.size(); i++) for (int j = n; j >= v1[i-1][1]; j--) for (int t = 0; t <= v1[i-1][2]; t++) if (j >= v1[i-1][1] * t) { dp[j] = max(dp[j], dp[j - v1[i-1][1] * t] + v1[i-1][0] * t); } cout<<dp[n]<<endl; // } } } // #include<bits/stdc++.h> // using namespace std; // int main(){ // vector<pair<int,int>> arr[2]; // arr[1].push_back({100,3}); // arr[1].push_back({50,4}); // cout<<arr[1].size()<<endl; // }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for (int i = 1; i <= v1.size(); i++) {
      |                     ~~^~~~~~~~~~~~
knapsack.cpp:53:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         if (id >= obj[i].size()) break;
      |             ~~~^~~~~~~~~~~~~~~~
knapsack.cpp:67:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |       for (int i = 1; i <= v1.size(); i++)
      |                       ~~^~~~~~~~~~~~
knapsack.cpp:66:19: warning: unused variable 'max1' [-Wunused-variable]
   66 |     long long int max1=0;
      |                   ^~~~
#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...