이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 998244353; //1e9 + 7;
const int MX = 2e5+5;
const ll INF = 1e18;
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;
}
#define MXN 100001
ll limit, n, val, wt, num, dp[2][2001];
map<ll, vpl> byWeight;
void init(void){
cin >> limit >> n;
F0R(i, n){
cin >> val >> wt >> num;
byWeight[wt].pb({val, num});
}
F0R(i, 2) F0R(j, 2001) dp[i][j] = -1;
dp[0][0] = 0;
}
void solve(void){
for(auto &[w, items] : byWeight){
sort(all(items), greater<pl>());
ll curUsed = 0;
ll copies = 0;
ll profit = 0;
ll idx = 0;
while(idx < sz(items) && (copies + 1) * w <= limit){
copies++;
profit += items[idx].f;
F0R(i, limit + 1){
dp[1][i] = max(dp[1][i], dp[0][i]);
if(i >= copies * w && dp[0][i - copies * w] != -1)
dp[1][i] = max(dp[1][i], dp[0][i - w * copies] + profit);
}
curUsed++;
if(curUsed == items[idx].s){
curUsed = 0;
idx++;
}
}
swap(dp[0], dp[1]);
// cout << w << ": \n";
// F0R(i, limit + 1)
// cout << dp[0][i] << ' ';
// cout << '\n';
}
cout << *max_element(dp[0], dp[0] + limit + 1);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
// setIO("CodeforcesEasy");
init();
solve();
rtn 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:101:19: 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]
101 | while(idx < sz(items) && (copies + 1) * w <= limit){
| ^
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |