제출 #1097618

#제출 시각아이디문제언어결과실행 시간메모리
1097618kiethm07Knapsack (NOI18_knapsack)C++11
12 / 100
778 ms556 KiB
#include <bits/stdc++.h>

#define pii pair<int,int>
#define pli pair<long long,pii>
#define fi first
#define se second

#define vi vector<int>
#define vii vector<pii>
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef long double ld;

const int inf = 1e9;
const ll linf = 1e16;
const double pi = acos(-1);
const int N = 1e5 + 5;
const int S = 2e3 + 5;

int n,s;
int dp[2][S];
int v[N], w[N], k[N];
vector<pii> f[S];

void build(int i){
    bool pre = (i & 1) ^ 1;
    for (int j = 0; j < w[i]; j++) f[j].clear();
    for (int j = 0; j <= s; j++){
        int id = j % w[i];
        f[id].push_back(pii(dp[pre][j] - v[i] * (j / w[i]),j));
    }
}

int query(int x,int j){
    int res = -inf;
    int id = j % w[x];
    for (int i = 0; i < f[id].size(); i++){
        int tmp = (j - f[id][i].se) / w[x];
        if (tmp > k[x] || tmp < 0) continue;
        //cout << "Q: " << x << " " << j << " " << id << " " << tmp << "\n";
        res = max(res, f[id][i].fi);
    }
    return res;
}

int main(){
    #define TEXT "knapsack"
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen(TEXT".inp","r")){
        freopen(TEXT".inp","r",stdin);
        freopen(TEXT".out","w",stdout);
    }
    cin >> s >> n;
    for (int i = 1; i <= n; i++){
        cin >> v[i] >> w[i] >> k[i];
    }
    for (int i = 1; i <= n; i++){
        bool cur = i & 1;
        bool pre = cur ^ 1;
        build(i);
        for (int j = 0; j <= s; j++){
            dp[cur][j] = max(dp[pre][j],v[i] * (j / w[i]) + query(i,j));
        }
    }
    int res = 0;
    for (int j = 0; j <= s; j++){
        res = max(res, dp[n & 1][j]);
    }
    cout << res << "\n";
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'int query(int, int)':
knapsack.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i = 0; i < f[id].size(); i++){
      |                     ~~^~~~~~~~~~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         freopen(TEXT".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:54:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         freopen(TEXT".out","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...