제출 #874581

#제출 시각아이디문제언어결과실행 시간메모리
874581iag99Knapsack (NOI18_knapsack)C++17
컴파일 에러
0 ms0 KiB
#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;

int main()
{
	int s,n;
	cin>>s>>n;
	vector<int> w(n);
	vector<ll> v(n),k(n);
	//w_i <= 2000
	//n <= 1e5
	map<int,vector<pair<int,int> > > mp;
	for(int i=0; i<n; i++)
	{
		cin>>v[i]>>w[i]>>k[i];
		if(w[i]<=s && k[i]>0) mp[w[i]].push_back(make_pair(v[i]),k[i]);
	}
	vector<vector<ll> > dp(mp.size()+1, vector<ll>(s+1, INT_MIN));
	dp[0][0]=0;
	int at=1;
	/*
	 * dp[i][w]= answer after processing first i items, weight=w
	 * dp[i][w]=max(dp[i-1][w], dp[i-1][w-w[i]]+v[i])
	 * Sort each copy of an item by value, only take the greatest S/w_i copies
	 */		
	 for(auto &[w,items] : mp)
	 {
		 sort(items.begin(), items.end(), greater<pair<int,int>>());
		 for(int i=0; i<=s; i++)
		 {
			 dp[at][i]=dp[at-1][i];
			 int copies=0;
			 int type_at=0;
			 int curr_used=0;
			 ll profit=0;
			 while((copies+1)*w <= i && type_at < items.size())
			 {
				 copies++;
				 profit+=items[type_at].first;
				 if(dp[at-1][i-copies*w]!=INT_MIN)
				 {
					 dp[at][i]=max(dp[at][i], dp[at-1][i-copies*w]+profit);
				 }
				 curr_used++;
				 if(curr_used==items[type_at].second)
				 {
					 curr_used=0;
					 type_at++;
				 }
			 }
		 }
		 at++;
	 }
	 cout << *std::max_element(dp.back().begin(), dp.back().end()) << endl;

    return 0;
}

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:19:58: error: no matching function for call to 'make_pair(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type&)'
   19 |   if(w[i]<=s && k[i]>0) mp[w[i]].push_back(make_pair(v[i]),k[i]);
      |                                                          ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
                 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:1:
/usr/include/c++/10/bits/stl_pair.h:567:5: note: candidate: 'template<class _T1, class _T2> constexpr std::pair<typename std::__strip_reference_wrapper<typename std::decay<_Tp>::type>::__type, typename std::__strip_reference_wrapper<typename std::decay<_Tp2>::type>::__type> std::make_pair(_T1&&, _T2&&)'
  567 |     make_pair(_T1&& __x, _T2&& __y)
      |     ^~~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:567:5: note:   template argument deduction/substitution failed:
knapsack.cpp:19:58: note:   candidate expects 2 arguments, 1 provided
   19 |   if(w[i]<=s && k[i]>0) mp[w[i]].push_back(make_pair(v[i]),k[i]);
      |                                                          ^
knapsack.cpp:39:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     while((copies+1)*w <= i && type_at < items.size())
      |                                ~~~~~~~~^~~~~~~~~~~~~~