# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
683702 | vjudge1 | Knapsack (NOI18_knapsack) | C++17 | 118 ms | 17916 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define task "a"
#define etr "\n"
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pli pair<long long,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define bg begin
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define lwb lower_bound
#define upb upper_bound
#define range(x, l, r) x+l, x+1+r
#define all(x) (x).bg(), (x).end()
#define compact(x) x.resize(unique(all(x)) - (x).bg())
#define sq(x) ((x)*(x))
void freop()
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const int N=1e5, M=2e3, mod=1e9+7;
int n, s;
ll v[N*10+5], w[N*10+5], dp[M+5];
multiset<ll> a[M+5];
void process()
{
cin >> s >> n;
for (int i=1; i<=n; i++)
{
ll k;
cin >> v[i] >> w[i];
cin >> k;
k = min(k, s/w[i]);
int bit = 0;
while (true)
{
if (k >= (1 << bit))
{
v[i+bit] = v[i] * (1 << bit);
w[i+bit] = w[i] * (1 << bit);
k -= (1 << bit);
} else
{
v[i+bit] = v[i] * k;
w[i+bit] = w[i] * k;
k -= k;
}
a[w[i+bit]].insert(v[i+bit]);
if (a[w[i+bit]].size() > s/w[i+bit]) a[w[i+bit]].erase(a[w[i+bit]].bg());
if (k == 0) break;
bit++;
}
i += bit;
n += bit;
}
int idx=0;
for (int i=1; i<=s; i++)
{
for (ll x : a[i])
{
w[++idx] = i;
v[idx] = x;
}
}
ll res=0;
for (int i=1; i<=idx; i++)
{
for (int j=s; j>=w[i]; j--)
{
dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
res = max(res, dp[j]);
}
}
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t=1; //cin >> t;
while (t--) process();
return 0;
}
컴파일 시 표준 에러 (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... |