답안 #1036246

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1036246 2024-07-27T06:56:17 Z sh1ro Knapsack (NOI18_knapsack) C++14
100 / 100
95 ms 36628 KB
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 4;
const int oo = 1e18;
const int mod = 1e9 + 7;
int t, n, q, m, k, a[N], ans, w, cnt, cur, dem, coun, profit;
map<int, vector<pair<int, int>>>mp;
vector<pair<int, int>>v;
vector<vector<int>>dp;
bool cmp(pair<int, int>a, pair<int, int>b){
    return a.first > b.first;
}
void solve(){
    cnt = 1;
    cin >> k >> n;
    for (int i = 1; i <= n; i++){
        int x, y, z; cin >> x >> y >> z;
        if (y <= k)mp[y].push_back({x, z});
    }
    dp.resize(mp.size() + 5, vector<int>(k + 5, -1));
    dp[0][0] = 0;
    for (auto p : mp){
        w = p.first, v = p.second;
        sort(v.begin(), v.end(), cmp);
        for (int i = 0; i <= k; i++){
            dem = coun = 0, cur = 1;
            dp[cnt][i] = dp[cnt - 1][i];
            profit = 0;
            while (cur * w <= i && dem < v.size()){
                profit += v[dem].first;
                if (dp[cnt - 1][i - cur * w] != -1)dp[cnt][i] = max(dp[cnt][i], dp[cnt - 1][i - cur * w] + profit);
                cur++;
                coun++;
                if (coun == v[dem].second){
                    coun = 0;
                    dem++;
                }
            }
        }
        cnt++;
    }
    for (int i = 0; i <= mp.size(); i++){
        for (int j = 0; j <= k; j++)ans = max(ans, dp[i][j]);
    }
    cout << ans;
}
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("clocktree.in", "r", stdin); freopen("clocktree.out", "w", stdout);
    t = 1;
    //cin >> t;
    while (t--)solve();
    return 0;
}

Compilation message

knapsack.cpp: In function 'void solve()':
knapsack.cpp:31:40: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |             while (cur * w <= i && dem < v.size()){
      |                                    ~~~~^~~~~~~~~~
knapsack.cpp:44:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::map<long long int, std::vector<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for (int i = 0; i <= mp.size(); i++){
      |                     ~~^~~~~~~~~~~~
knapsack.cpp: At global scope:
knapsack.cpp:49:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   49 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 424 KB Output is correct
3 Correct 1 ms 472 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 2 ms 1884 KB Output is correct
8 Correct 2 ms 1884 KB Output is correct
9 Correct 2 ms 1884 KB Output is correct
10 Correct 2 ms 1884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 424 KB Output is correct
3 Correct 1 ms 472 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 2 ms 1884 KB Output is correct
8 Correct 2 ms 1884 KB Output is correct
9 Correct 2 ms 1884 KB Output is correct
10 Correct 2 ms 1884 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 2 ms 1884 KB Output is correct
17 Correct 3 ms 1884 KB Output is correct
18 Correct 2 ms 1884 KB Output is correct
19 Correct 2 ms 1884 KB Output is correct
20 Correct 2 ms 2004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 424 KB Output is correct
7 Correct 1 ms 472 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 2 ms 1884 KB Output is correct
11 Correct 2 ms 1884 KB Output is correct
12 Correct 2 ms 1884 KB Output is correct
13 Correct 2 ms 1884 KB Output is correct
14 Correct 2 ms 1884 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 4 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 2 ms 1884 KB Output is correct
21 Correct 3 ms 1884 KB Output is correct
22 Correct 2 ms 1884 KB Output is correct
23 Correct 2 ms 1884 KB Output is correct
24 Correct 2 ms 2004 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 6 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 3 ms 1884 KB Output is correct
31 Correct 2 ms 1880 KB Output is correct
32 Correct 2 ms 1884 KB Output is correct
33 Correct 2 ms 1884 KB Output is correct
34 Correct 2 ms 1880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 424 KB Output is correct
7 Correct 1 ms 472 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 2 ms 1884 KB Output is correct
11 Correct 2 ms 1884 KB Output is correct
12 Correct 2 ms 1884 KB Output is correct
13 Correct 2 ms 1884 KB Output is correct
14 Correct 2 ms 1884 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 4 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 2 ms 1884 KB Output is correct
21 Correct 3 ms 1884 KB Output is correct
22 Correct 2 ms 1884 KB Output is correct
23 Correct 2 ms 1884 KB Output is correct
24 Correct 2 ms 2004 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 6 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 3 ms 1884 KB Output is correct
31 Correct 2 ms 1880 KB Output is correct
32 Correct 2 ms 1884 KB Output is correct
33 Correct 2 ms 1884 KB Output is correct
34 Correct 2 ms 1880 KB Output is correct
35 Correct 24 ms 6356 KB Output is correct
36 Correct 30 ms 6552 KB Output is correct
37 Correct 28 ms 6356 KB Output is correct
38 Correct 23 ms 4408 KB Output is correct
39 Correct 27 ms 5332 KB Output is correct
40 Correct 94 ms 36628 KB Output is correct
41 Correct 78 ms 35664 KB Output is correct
42 Correct 95 ms 35880 KB Output is correct
43 Correct 90 ms 35668 KB Output is correct
44 Correct 95 ms 35664 KB Output is correct
45 Correct 31 ms 8028 KB Output is correct
46 Correct 21 ms 7120 KB Output is correct
47 Correct 37 ms 11352 KB Output is correct
48 Correct 57 ms 20608 KB Output is correct
49 Correct 21 ms 5200 KB Output is correct
50 Correct 51 ms 4956 KB Output is correct