| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 701350 | tgp072 | Knapsack (NOI18_knapsack) | C++14 | 28 ms | 2544 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <tuple>
#include <numeric>
#include <climits>
#include <bitset>
#include <iomanip>
#include <random>
#include <ctime>
using namespace std;
//change the long long to int if you need to save memory/time really badly
typedef long long ll;
//Comment this define when working on interactive problems
#define endl "\n"
#define sqrt(n) sqrt((long double) n)
const ll MAXN = 2e5 + 5;
const ll ZER = 0;
const ll ONE = 1;
const ll INF = LLONG_MAX;
const ll MOD = 1e9 + 7;
ll min(ll a, ll b) {
if (a < b) {
return a;
} else {
return b;
}
}
ll max(ll a, ll b) {
if (a > b) {
return a;
} else {
return b;
}
}
ll mod(ll num) {
return (num >= MOD ? num % MOD : num);
}
ll __gcd(ll a, ll b) {
if (b == 0) {
return a;
}
if (a == 0) {
return b;
}
if (a < b) {
swap(a, b);
}
return __gcd(b, a % b);
}
ll __lcm(ll a, ll b) {
ll prod = a*b;
return prod/__gcd(a, b);
}
ll powmod(ll a, ll b){
a %= MOD;
if (a == 0) return 0;
ll product = 1;
while(b > 0){
if (b&1){ // you can also use b % 2 == 1
product *= a;
product %= MOD;
--b;
}
a *= a;
a %= MOD;
b /= 2; // you can also use b >> 1
}
return product;
}
//calculates the inverse of a number mod MOD
ll inv(ll a) {
return powmod(a, MOD - 2);
}
void solve(ll ca)
{
ll s, n;
cin >> s >> n;
vector<pair<ll, ll>> items[s+1]; //list of items with the same weight, sorted by the value
for (ll i = 0; i < n; i++) {
ll v, w, k;
cin >> v >> w >> k;
items[w].push_back({v, k});
}
vector<ll> weights[s+1];
for (ll i = 0; i <= s; i++) {
sort(items[i].begin(), items[i].end(), greater<>());
ll cur = 0;
for (ll j = 0; j < items[i].size(); j++) {
ll k = 0;
while (cur+i <= s && k < items[i][j].second) {
weights[i].push_back(items[i][j].first);
k++;
cur += i;
}
if (cur+i >= s) {
break;
}
}
}
/*
for (ll i = 0; i <= s; i++) {
cout << i << endl;
for (auto el: weights[i]) {
cout << el << " ";
}
cout << endl;
}
*/
ll dp[s+1]; //dp[i] is the maximum value you can get with a weight of i
for (ll i = 0; i <= s; i++) {
dp[i] = -1;
}
dp[0] = 0;
for (ll we = 0; we <= s; we++) {
if (weights[we].size() == 0) {
continue;
}
for (ll i = s; i >= 0; i--) {
if (dp[i] != -1) {
ll cur = i; ll csum = 0; ll ind = 0;
while (cur+we <= s && ind < weights[we].size()) {
dp[cur+we] = max(dp[cur+we], dp[i]+csum+weights[we][ind]);
csum += weights[we][ind];
cur += we;
ind++;
}
}
}
}
ll ans = 0;
for (ll i = 0; i <= s; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return;
}
int main()
{
//mt19937 rng(0);
//Fast IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
/*
freopen("feast.in", "r", stdin);
freopen("feast.out", "w", stdout);
*/
ll t = 1;
//cin >> t;
ll co = 1;
while (t--) {
solve(co);
++co;
}
}
//Always write your algorithm down on a piece of paper and question EACH and EVERY step if you aren't able to find an implementation mistake in your code.
컴파일 시 표준 에러 (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... | ||||
