Submission #1016867

#TimeUsernameProblemLanguageResultExecution timeMemory
1016867sexo69696969Knapsack (NOI18_knapsack)C++14
12 / 100
0 ms604 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef local
  #include "debug.hpp"
  #define pr(...) debug(#__VA_ARGS__, __VA_ARGS__)
  #define prs(...) debug_nameless(__VA_ARGS__)
#else
  #define pr(...) 69
  #define prs(...) 69
#endif

#define endl '\n'

const int inf = 1e9;
const long long binf = 1e18;

struct Item{
  int p, w, f;
};

void solve(int tc){
  int s, n;
  cin >> s >> n;
  vector <vector<Item>> items(s+1);
  for(int i = 0; i < n; i++){
    int p, w, f;
    cin >> p >> w >> f;
    items[w].push_back({p, w, f});
  } 
  for(auto it : items){
    sort(it.begin(), it.end(), [&](Item a, Item b){return a.p > b.p;});
  }
  auto print = [&](vector<Item> a){
    for(auto x : a){
      cout << "{" << x.p << ", " << x.w << ", " << x.f << "} ";
    }
    if(a.size()) cout << "Penis" << endl;
  };
  for(auto it : items){
    //print(it);
  }
  vector <pair<int,int>> arr;
  for(int i = 1; i <= s; i++){
    int total = 0;
    for(int pos = 0; pos < items[i].size(); pos++){
      for(int j = 0; j < items[i][pos].f && total < s; j++){
        if(total + items[i][pos].w <= s){
          arr.push_back({items[i][pos].w, items[i][pos].p});  
        }
        total += items[i][pos].w;
      }
      if(pos > 0){
        assert(items[i][pos-1].p >= items[i][pos].p && items[i][pos-1].w == items[i][pos].w);
      }
    }
  }
  pr(arr);
  vector <long long> dp(s+1, 0);
  for(int i = 1; i <= arr.size(); i++){
    for(int j = s; j >= 0; j--){
      if(j - arr[i-1].first >= 0){
        dp[j] = max(dp[j], dp[j-arr[i-1].first] + arr[i-1].second);
      }
    }
  }
  cout << dp[s] << endl;
}  

signed main(){
  ios_base::sync_with_stdio(0); cin.tie(0);

  int tc = -1;
  tc = 1;
  if(tc != 1) cin >> tc;

  for(int t = 0; t < tc; t++){
    pr(t); prs(string(50, '-'));
    solve(t);
    prs(string(50, '-') + "\n");
  }

  return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void solve(int)':
knapsack.cpp:46:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for(int pos = 0; pos < items[i].size(); pos++){
      |                      ~~~~^~~~~~~~~~~~~~~~~
knapsack.cpp:9:19: warning: statement has no effect [-Wunused-value]
    9 |   #define pr(...) 69
      |                   ^~
knapsack.cpp:58:3: note: in expansion of macro 'pr'
   58 |   pr(arr);
      |   ^~
knapsack.cpp:60:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |   for(int i = 1; i <= arr.size(); i++){
      |                  ~~^~~~~~~~~~~~~
knapsack.cpp:34:8: warning: variable 'print' set but not used [-Wunused-but-set-variable]
   34 |   auto print = [&](vector<Item> a){
      |        ^~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:9:19: warning: statement has no effect [-Wunused-value]
    9 |   #define pr(...) 69
      |                   ^~
knapsack.cpp:78:5: note: in expansion of macro 'pr'
   78 |     pr(t); prs(string(50, '-'));
      |     ^~
knapsack.cpp:10:20: warning: statement has no effect [-Wunused-value]
   10 |   #define prs(...) 69
      |                    ^~
knapsack.cpp:78:12: note: in expansion of macro 'prs'
   78 |     pr(t); prs(string(50, '-'));
      |            ^~~
knapsack.cpp:10:20: warning: statement has no effect [-Wunused-value]
   10 |   #define prs(...) 69
      |                    ^~
knapsack.cpp:80:5: note: in expansion of macro 'prs'
   80 |     prs(string(50, '-') + "\n");
      |     ^~~
#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...