Submission #806833

#TimeUsernameProblemLanguageResultExecution timeMemory
806833loveubotaKnapsack (NOI18_knapsack)C++17
100 / 100
584 ms6176 KiB
// In the name of God #include <bits/stdc++.h> using namespace std ; #define ll long long #define pb push_back #define ld long double #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define remove(x) x.erase(unique(all(x)), x.end()) #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, b, a) for (int i = (b); i >= (a); i--) const int N = 1e5 + 1 , inf = (1 << 30) , L = 20 ; const ld eps = 1e-6 ; const ll INF = 1e18 + 123 , mod = 1e9 + 7 ; int s , n , v[N] , w[N] , k[N] ; vector<vector<ll>> dp ; ll go(int i , int j) { if (i > n) return 0 ; if (~dp[i][j]) return dp[i][j] ; ll ans = 0 ; rep(c, 0, k[i]) { if (c * w[i] > j) break ; ans = max(ans , go(i + 1, j - (1ll * c * w[i])) + 1ll * c * v[i]) ; } dp[i][j] = ans ; return ans ; } /* 15 5 4 12 1 2 1 1 10 4 1 1 1 1 2 2 1 */ void solve(int &tc) { cin >> s >> n ; rep(i , 1 , n) { cin >> v[i] >> w[i] >> k[i] ; } if (n <= 100 && s <= 2000) { dp = vector<vector<ll>>(n + 1, vector<ll>(s + 1, -1)) ; cout << go(1,s) ; } else { vector<pair<ll,ll>> obj[s + 1]; long long dp1[2001]; memset(dp1, 0, sizeof dp1); for (int i = 1; i <= n; i++) { obj[w[i]].pb({v[i], k[i]}); } for (int i = 0; i <= s; i++) { if (obj[i].size() == 0) continue; sort(all(obj[i]), greater<pair<int, int>>()); int id = 0; for (int j = 0; j * i < s; j++) { if (id >= obj[i].size()) break; for (int k = s; k >= i; k--) { dp1[k] = max(dp1[k], dp1[k - i] + obj[i][id].first); } --obj[i][id].second; if (obj[i][id].second == 0) ++id; } } cout << dp1[s] << endl; } return ; } int main() { #ifdef ONPC freopen("inp", "r", stdin) ; #endif ios_base::sync_with_stdio(false) ; cin.tie(0) ; int T = 1 ; // cin >> T ; rep(i, 1 ,T) { solve(i) ; } return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'void solve(int&)':
knapsack.cpp:68:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         if (id >= obj[i].size()) break;
      |             ~~~^~~~~~~~~~~~~~~~
#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...