| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1292169 | dex111222333444555 | Knapsack (NOI18_knapsack) | C++20 | 1095 ms | 1588 KiB |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5, MAXS = 2005;
const long long inf = 1e18 + 3;
int lim, numVal, val[MAXN], weight[MAXN], num[MAXN];
long long dp[2][MAXS];
void calcDP(){
memset(dp[0], -0x3f, sizeof dp[0]);
dp[0][0] = 0;
for(int i = 1; i <= numVal; i++){
memset(dp[1], -0x3f, sizeof dp[1]);
for(int rem = 0; rem < weight[i]; rem++){
deque<pair<int, long long>> dq;
for(int j = 0; rem + j * weight[i] <= lim; j++){
int nxt = rem + j * weight[i];
long long save = dp[0][nxt] - j * val[i];
while(dq.size() && dq.back().second <= save)
dq.pop_back();
dq.push_back({j, save});
while(dq.size() && dq.front().first < j - num[i])
dq.pop_front();
dp[1][nxt] = dq.front().second + j * val[i];
}
}
for(int j = 0; j <= lim; j++) dp[0][j] = dp[1][j];
}
long long res = -inf;
for(int i = 0; i <= lim; i++) res = max(res, dp[1][i]);
cout << res << '\n';
}
void input(){
cin >> lim >> numVal;
for(int i = 1; i <= numVal; i++)
cin >> val[i] >> weight[i] >> num[i];
}
void solve(){
calcDP();
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "teest"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
input();
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
| # | 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... | ||||
