제출 #943374

#제출 시각아이디문제언어결과실행 시간메모리
943374Hadi_AlhamedKnapsack (NOI18_knapsack)C++17
컴파일 에러
0 ms0 KiB
//to live is to die #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long int ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<long long> vl; typedef vector<pair<int, int>> vpi; typedef vector<pair<ll, ll>> vpl; #define Clear(a, n) \ for (int i = 0; i <= n; i++) \ { \ a[i] = 0; \ } #define clearMat(a, n, m, d) \ for (int i = 0; i <= n; i++) \ { \ for (int j = 0; j <= m; j++) \ a[i][j] = d; \ } #define YES cout << "YES\n" #define NO cout << "NO\n" #define PB push_back #define PF push_front #define MP make_pair #define F first #define S second #define rep(i, n) for (int i = 0; i < n; i++) #define repe(i, j, n) for (int i = j; i < n; i++) #define SQ(a) (a) * (a) #define rep1(i, n) for (int i = 1; i <= n; i++) #define Rrep(i, start, finish) for (int i = start; start >= finish; i--) #define db(x) cerr << #x <<" "; _print(x); cerr << endl; #define forn(i, Start, End, step) for (int i = Start; i <= End; i += step) #define rforn(i, Start, End, step) for (int i = Start; i >= End; i -= step) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() // ll arr[SIZE]; /* how to find n % mod ; n < 0? x = (n+mod)%mod if(x < 0) x += mod; */ void _print(int x) { cerr << x; } void _print(ll x) { cerr << x; } void _print(string x) { cerr << x; } void _print(char x) { cerr << x; } void _print(double x) { cerr << x; } void _print(ull x) { cerr << x; } void _print(vl x) { for(auto e : x) { cerr << e << " "; } cerr << "\n"; } void print(vpi x) { for(auto e : x) { cerr << e.F << " " << e.S << "\n"; } cerr << "\n"; } void _print(vi x) { for(auto e : x) { cerr << e << " "; } cerr << "\n"; } void _print(deque<ll>x) { for(auto e : x) { cerr << e << " "; } cerr << "\n"; } template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using ordered_multiset = tree<T, null_type, less_equal <T>, rb_tree_tag, tree_order_statistics_node_update>; template<class T> bool ckmin(T& a, const T& b) { return b<a?a=b,1:0; } template<class T> bool ckmax(T& a, const T& b) { return a<b?a=b,1:0; } template<typename T> istream& operator>>(istream& in, vector<T>& a) { for(auto &x : a) in >> x; return in; }; template<typename T> ostream& operator<<(ostream& out, vector<T>& a) { for(auto &x : a) out << x << ' '; return out; }; // priority_queue<data type , the container that would hold the values , greater<pair<int,int>>> // greater means that we want the smallest value on top // less means that we want the largest // x ^ (n) mod m = ( (x mod m)^(n) ) mod m char to_char(int num) { return (char)(num + '0'); } ll const MAX = 1e18+1; ll const oo = 1e18 + 1; ll const INF = 1e9 + 10; const ll MOD = 1e9 + 7; ll const SIZE = 2e5 + 900; //const int MAX_N = 100'005; const int LOG = 20; void solve() { int S , N; cin >> S >> N; map<int , vpi >by_weight ; // weight : {value , copies}} rep(i , N) { int V , W , K; cin >> V >> W >> K; K = min(K , S); if(W <= S) { by_weight[W].PB({V , K}); } } //dp[i][j] : max value we can achieve using first i weight //with weight equal to j vector<vector<int>>dp(N + 2 , vector<int>(S + 2 , -1)); dp[0][0] = 1; int at = 1; //go weight by weight and see if we take that many copies //what is the profit and what is the best answer for the current //dp[i][weight] ll answer = 0; for(auto& [w , items] : by_weight) { sort(rall(items)); for(int i = 0; i <= S ; i++) { dp[at][i] = dp[at - 1][i];//leave int copies = 0; int total_weight = 0; ll total_profit = 0; int index = 0; int copies_of_current_item = 0; while((copies + 1) * w <= i && index < (int)items.size()) { total_profit += items[index].F; total_weight += w; copies_of_current_item++; copies++; if(i - total_weight >= 0) { dp[at][i] = max(dp[at][i] , dp[at - 1][i - total_weight] + total_profit); answer = max(answer , dp[at][i]); } if(copies_of_current_item == items[index].S) { copies_of_current_item = 0; index++; } } } at++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("taming.in" , "r" , stdin); // freopen("taming.out", "w" , stdout); int T = 1; // cin >> T; while(T--) { solve(); } return 0; } /* stuff you should look for * WRITE STUFF DOWN, ON PAPER * BFS THEN DFS * int overflow, array bounds * special cases (n=1?) * do sm th instead of nothing and stay organized * DON'T GET STUCK ON ONE APPROACH * (STUCK?)******** Try to simplify the problem(keeping in mind the main problem), (): * 1- problem to subProblem * 2- from simple to complex: start with a special * problem and then try to update the solution for general case * -(constraints - > solve it with none , one,two ... of them till you reach the given problem -(no constraints - > try to give it some) -how a special case may be incremented * 3-Simplification by Assumptions * REVERSE PROBLEM * PROBLEM ABSTRACTION * SMALL O BSERVATIONS MIGHT HELP ALOT * WATCH OUT FOR TIME * RETHINK YOUR IDEA,BETTER IDEA, APPROACH? * CORRECT IDEA, NEED MORE OBSERVATIONS * CORRECT APPROACH, WRONG IDEA * WRONG APPROACH * THINK CONCRETE THEN SYMBOL, * having the solution for the first m state , can we solve it for m + 1 ? * in many cases incremental thinking needs data sorting */

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

knapsack.cpp: In function 'void solve()':
knapsack.cpp:190:92: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, ll)'
  190 |                     dp[at][i] = max(dp[at][i] , dp[at - 1][i - total_weight] + total_profit);
      |                                                                                            ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from knapsack.cpp:2:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  254 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note:   template argument deduction/substitution failed:
knapsack.cpp:190:92: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
  190 |                     dp[at][i] = max(dp[at][i] , dp[at - 1][i - total_weight] + total_profit);
      |                                                                                            ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from knapsack.cpp:2:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  300 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note:   template argument deduction/substitution failed:
knapsack.cpp:190:92: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
  190 |                     dp[at][i] = max(dp[at][i] , dp[at - 1][i - total_weight] + total_profit);
      |                                                                                            ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from knapsack.cpp:2:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3480 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note:   template argument deduction/substitution failed:
knapsack.cpp:190:92: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  190 |                     dp[at][i] = max(dp[at][i] , dp[at - 1][i - total_weight] + total_profit);
      |                                                                                            ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from knapsack.cpp:2:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3486 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note:   template argument deduction/substitution failed:
knapsack.cpp:190:92: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  190 |                     dp[at][i] = max(dp[at][i] , dp[at - 1][i - total_weight] + total_profit);
      |                                                                                            ^
knapsack.cpp:191:52: error: no matching function for call to 'max(ll&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)'
  191 |                     answer = max(answer , dp[at][i]);
      |                                                    ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from knapsack.cpp:2:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  254 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note:   template argument deduction/substitution failed:
knapsack.cpp:191:52: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
  191 |                     answer = max(answer , dp[at][i]);
      |                                                    ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from knapsack.cpp:2:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  300 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note:   template argument deduction/substitution failed:
knapsack.cpp:191:52: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
  191 |                     answer = max(answer , dp[at][i]);
      |                                                    ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from knapsack.cpp:2:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3480 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note:   template argument deduction/substitution failed:
knapsack.cpp:191:52: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  191 |                     answer = max(answer , dp[at][i]);
      |                                                    ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from knapsack.cpp:2:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3486 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note:   template argument deduction/substitution failed:
knapsack.cpp:191:52: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  191 |                     answer = max(answer , dp[at][i]);
      |                                                    ^