Submission #495982

#TimeUsernameProblemLanguageResultExecution timeMemory
495982SinsAriesKnapsack (NOI18_knapsack)C++17
0 / 100
1 ms332 KiB
#include<bits/stdc++.h> using namespace std; // type using ll = long long; using ld = long double; using db = double; using str = string; // pairs using pi = pair<int,int>; using pl = pair<ll,ll>; using pd = pair<db,db>; // vector using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; #define pb push_back #define pf push_front #define all(x) x.begin(), x.end() #define sz(x) x.size() #define sor(x) sort(all(x)) #define rsz resize #define rtn return // binary search #define lb lower_bound #define ub upper_bound template<class T> ll lwb(vector<T>& a, const T& b) { rtn lb(all(a), b) - a.begin(); } template<class T> ll upb(vector<T>& a, const T& b) { rtn ub(all(a), b) - a.begin(); } // pairs #define mp make_pair #define f first #define s second // loops #define FOR(i,a,b) for (ll i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (ll i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define each(a,x) for (auto& a: x) #define cont continue #define bre break // FILE I/O void setIn(str s) { freopen(s.c_str(),"r",stdin); } void setOut(str s) { freopen(s.c_str(),"w",stdout); } void setIO(str s) { setIn(s+".inp"); setOut(s+".out"); } const int MOD = 1e6 + 3; //998244353; const int MX = 2e5+5; const ll INF = 1e15; const db PI = acos((db)-1); const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>; // modulo, exponentiation, inverse ll pow_mod(ll a, ll b, ll m) { ll res = 1; while(b){ res = res * (b & 1 ? a : 1) % m; a = a * a % m; b >>= 1; } rtn res; } ll inv(ll a, ll m) { rtn pow_mod(a, m - 2, m); } ll nxt(void){ // fast way for cin operation ll x; cin >> x; rtn x; } ll n, m; map<ll, vpl> by_w; void init(void){ cin >> n >> m; F0R(i, n){ ll weight, value, num; cin >> weight >> value >> num; by_w[weight].pb({value, num}); } } void solve(void){ vector<vl> best(sz(by_w) + 1, vl(m + 1, -INF)); best[0][0] = 0; ll cur = 1; for(auto& [w, diamonds] : by_w){ sort(all(diamonds), greater<pl>()); F0R(i, m + 1){ best[cur][i] = best[cur - 1][i]; ll num = 1; ll profit = 0; ll curUsed = 0; ll idx = 0; while(num * w <= i && idx < sz(diamonds)){ profit += diamonds[idx].f; curUsed++; if(best[cur - 1][i - num * w] != -INF) best[cur][i] = max(best[cur][i], best[cur - 1][i - num * w] + profit); if(curUsed == diamonds[idx].s){ curUsed = 0; idx++; } num++; } } cur++; } cout << *max_element(all(best.back())); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); // setIO("CAYKHE"); init(); solve(); rtn 0; }

Compilation message (stderr)

knapsack.cpp: In function 'void solve()':
knapsack.cpp:103:39: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |             while(num * w <= i && idx < sz(diamonds)){
      |                                       ^
knapsack.cpp: In function 'void setIn(str)':
knapsack.cpp:54:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
knapsack.cpp: In function 'void setOut(str)':
knapsack.cpp:55:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 | void setOut(str s) { freopen(s.c_str(),"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...