제출 #1331330

#제출 시각아이디문제언어결과실행 시간메모리
1331330conthoancoKnapsack (NOI18_knapsack)C++20
100 / 100
45 ms17492 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int s, n, dp[N][N];
vector<pair<int,int>> group[N];
vector<int> val[N];
void input()
{
    cin >> s >> n;

    for(int i = 1; i <= n; ++i) {
        int v, w, k;
        cin >> v >> w >> k;
        group[w].push_back({v, k});
    }
}

void solve()
{
    for(int w = 1; w <= s; ++w) {
        sort(group[w].begin(), group[w].end(), greater<pair<int,int>>());
        int cur = 0;
        val[w].push_back(0);
        for(int k = 1; k <= s / w; ++k) {
            if(cur >= group[w].size()) break;
            if(group[w][cur].second == 0) cur++;
            if(cur >= group[w].size()) break;
            val[w].push_back(val[w].back() + group[w][cur].first);
            group[w][cur].second--;
        }
    }

    memset(dp, -0x3f, sizeof(dp));
    dp[0][0] = 0;
    int ans = 0;
    for(int w = 1; w <= s; ++w) {
        for(int t = 0; t <= s; ++t) {
            for(int k = 0; k <= t / w && k < val[w].size(); ++k) {
                dp[w][t] = max(dp[w][t], dp[w - 1][t - w * k] + val[w][k]);
            }
            ans = max(ans, dp[w][t]);
        }
    }
    cout << ans;
}
#undef int
int main()
{
    if(fopen("trash.inp" , "r"))
        freopen("trash.inp" , "r" , stdin) , freopen("trash.out" , "w" , stdout);
    // else freopen(".inp" , "r" , stdin) , freopen(".out" , "w" , stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int test = 1;
//    cin >> test;
    while(test--) {
        input();
        solve();
    }
}

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen("trash.inp" , "r" , stdin) , freopen("trash.out" , "w" , stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:50:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen("trash.inp" , "r" , stdin) , freopen("trash.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...