1
1
2
2
/* *
3
3
4
- Problem : 0-1 Knapsack
4
+ F(i,W) = max( include ith item , exclude ith item )
5
5
6
- In 0-1 Knapsack problem, we are given a set of items, each with a weight and a value and
7
- we need to determine the number of each item to include in a collection so that
8
- the total weight is less than or equal to a given limit and the total value is as large as possible.
6
+ Here ,
7
+ include ith item = F(i+1,W+w[i]) + v[i]
8
+ exclude ith item = F(i+1,W)
9
+
10
+ Base case:
11
+
12
+ F(i,W>MAX_WEIGHT) = INT_MIN
13
+ F(i>n,W<=MAX_WIGHT) = 0
14
+ F(i>n,W>MAX_WIGHT) = INT_MIN
9
15
10
16
**/
11
17
12
- /* *Which of the favors of your Lord will you deny ?**/
18
+ /* * Which of the favors of your Lord will you deny ? **/
13
19
14
20
#include < bits/stdc++.h>
15
21
using namespace std ;
@@ -20,116 +26,152 @@ using namespace std;
20
26
#define MP make_pair
21
27
#define F first
22
28
#define S second
23
- #define INF INT_MAX
24
29
25
30
#define ALL (x ) (x).begin(), (x).end()
26
31
#define DBG (x ) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
32
+ #define READ freopen (" alu.txt" , " r" , stdin)
33
+ #define WRITE freopen (" vorta.txt" , " w" , stdout)
27
34
28
35
#include < ext/pb_ds/assoc_container.hpp>
29
36
#include < ext/pb_ds/tree_policy.hpp>
30
37
using namespace __gnu_pbds ;
31
38
32
- template <class TIn >
33
- using indexed_set = tree<
34
- TIn, null_type, less<TIn>,
35
- rb_tree_tag, tree_order_statistics_node_update>;
39
+ template <class TIn >using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
40
+
41
+ /* *
36
42
37
- /*
38
43
PBDS
39
44
-------------------------------------------------
40
45
1) insert(value)
41
46
2) erase(value)
42
47
3) order_of_key(value) // 0 based indexing
43
48
4) *find_by_order(position) // 0 based indexing
44
49
45
- */
50
+ **/
51
+
52
+ template <class T1 , class T2 >
53
+ ostream &operator <<(ostream &os, pair<T1,T2>&p);
54
+ template <class T >
55
+ ostream &operator <<(ostream &os, vector<T>&v);
56
+ template <class T >
57
+ ostream &operator <<(ostream &os, set<T>&v);
46
58
47
59
inline void optimizeIO ()
48
60
{
49
61
ios_base::sync_with_stdio (false );
50
62
cin.tie (NULL );
51
63
}
52
64
53
- const int nmax = 100 +7 ;
65
+ const int nmax = 1e2 +7 ;
66
+ const int nmax2 = 1e5 +7 ;
54
67
const LL LINF = 1e17 ;
55
68
56
- string to_str (LL x)
69
+ template <class T >
70
+ string to_str (T x)
57
71
{
58
72
stringstream ss;
59
73
ss<<x;
60
74
return ss.str ();
61
75
}
62
76
63
- // bool cmp(const PII &A,const PII &B)
64
- // {
65
- //
66
- // }
67
-
68
- vector<int > vara = { -1 , 20 , 5 , 10 , 40 , 15 , 25 };
69
- vector<int > wara = { -1 , 1 , 2 , 3 , 8 , 7 , 4 };
70
- int max_cap = 10 ;
77
+ int n,MAX_WEIGHT;
71
78
72
- /* * 1 based indexing **/
79
+ LL w[nmax];
80
+ LL v[nmax];
81
+ LL dp[nmax][nmax2];
73
82
74
- /* *
75
-
76
- dp[i][j] = max(include the item , exclude the item)
77
-
78
- **/
79
-
80
- int dp[nmax][nmax];
81
-
82
- int solve (int i,int w)
83
+ LL solve (int pos,int W) /* * maximum value to get using maximum weight W **/
83
84
{
84
- if (w<0 ) return INT_MIN; /* * **/
85
-
86
- if (i==0 || w==0 ) return 0 ;
85
+ if (W>MAX_WEIGHT) return INT_MIN; /* * if anytime W>MAX_HEIGHT , not possible . This is used just to avoid RTE **/
87
86
88
- int &ret = dp[i][w];
87
+ if (pos>n) /* * Done with n items and now check condition **/
88
+ {
89
+ return W<=MAX_WEIGHT ? 0 : INT_MIN;
90
+ }
89
91
92
+ LL &ret = dp[pos][W];
90
93
if (ret!=-1 ) return ret;
91
94
92
- int inc = vara[i] + solve (i- 1 ,w-wara[i]) ;
93
- int exc = solve (i- 1 ,w );
95
+ LL inc = solve (pos+ 1 ,W+w[pos]) + v[pos] ;
96
+ LL exc = solve (pos+ 1 ,W );
94
97
95
98
return ret = max (inc,exc);
96
99
}
97
100
98
- string solve_print (int i ,int w )
101
+ string solve_print (int pos ,int W )
99
102
{
100
- if (w<0 ) return " " ; /* * **/
103
+ if (W>MAX_WEIGHT) return " " ;
104
+ if (pos>n) return " " ;
101
105
102
- if (i==0 || w==0 ) return " " ;
106
+ LL inc = solve (pos+1 ,W+w[pos]) + v[pos];
107
+ LL exc = solve (pos+1 ,W);
103
108
104
- int inc = vara[i] + dp[i-1 ][w-wara[i]];
105
- int exc = dp[i-1 ][w];
106
-
107
- if (inc>exc)
108
- return to_str (vara[i]) + " " + solve_print (i-1 ,w-wara[i]);
109
- else
110
- return solve_print (i-1 ,w);
109
+ if (inc>exc) return to_str (v[pos]) + " " + solve_print (pos+1 ,W+w[pos]);
110
+ else return solve_print (pos+1 ,W);
111
111
}
112
112
113
-
114
113
int main ()
115
114
{
116
115
optimizeIO ();
117
116
118
- memset (dp,- 1 , sizeof dp) ;
117
+ cin>>n>>MAX_WEIGHT ;
119
118
120
- int n = vara.size ()-1 ;
119
+ for (int i=1 ;i<=n;i++)
120
+ cin>>w[i]>>v[i];
121
121
122
- cout<<solve (n,max_cap)<<endl;
123
- cout<<solve_print (n,max_cap)<<endl;
122
+ memset (dp,-1 ,sizeof dp);
124
123
125
- // for(int i=1; i<=n; i++)
126
- // {
127
- // for(int j=1; j<=max_cap; j++)
128
- // cout<<dp[i][j]<<" ";
129
- // cout<<endl;
130
- // }
124
+ cout<<solve (0 ,0 )<<endl;
125
+ cout<<solve_print (0 ,0 )<<endl;
131
126
132
127
return 0 ;
133
128
}
134
129
130
+ /* *
131
+
132
+ **/
133
+
134
+ template <class T1 , class T2 >
135
+ ostream &operator <<(ostream &os, pair<T1,T2>&p)
136
+ {
137
+ os<<" {" <<p.first <<" , " <<p.second <<" } " ;
138
+ return os;
139
+ }
140
+ template <class T >
141
+ ostream &operator <<(ostream &os, vector<T>&v)
142
+ {
143
+ os<<" [ " ;
144
+ for (int i=0 ; i<v.size (); i++)
145
+ {
146
+ os<<v[i]<<" " ;
147
+ }
148
+ os<<" ]" ;
149
+ return os;
150
+ }
151
+
152
+ template <class T >
153
+ ostream &operator <<(ostream &os, set<T>&v)
154
+ {
155
+ os<<" [ " ;
156
+ for (T i:v)
157
+ {
158
+ os<<i<<" " ;
159
+ }
160
+ os<<" ]" ;
161
+ return os;
162
+ }
135
163
164
+ // string solve_print(int i,int w)
165
+ // {
166
+ // if(w<0) return ""; /** **/
167
+ //
168
+ // if(i==0 || w==0) return "";
169
+ //
170
+ // int inc = vara[i] + dp[i-1][w-wara[i]];
171
+ // int exc = dp[i-1][w];
172
+ //
173
+ // if(inc>exc)
174
+ // return to_str(vara[i]) + " " + solve_print(i-1,w-wara[i]);
175
+ // else
176
+ // return solve_print(i-1,w);
177
+ // }
0 commit comments