Monday, December 10, 2018

[toph.co] The Perfect Collection

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : The Perfect Collection
Source            : toph.co
Category          : Dynamic Programing
Algorithm         : Coin Change
Verdict           : Accepted

#include "bits/stdc++.h"
 
using namespace std;
 
int dp[100+5][100+5][2050];
bool memoize[100+5][100+5][2050];
 
int n, q, k, arr[100+5];
 
inline int minimumXor(int pos, int take, int xorVal)
{
	if (take == 0) return xorVal;
	if (pos > n or take < 0) return INT_MAX;
	int &ret = dp[pos][take][xorVal];
	bool &mem = memoize[pos][take][xorVal];
	if (mem) return ret;
	mem = 1;
	int choice1, choice2;
	choice1 = minimumXor(pos+1, take - 1, xorVal xor arr[pos]);
	choice2 = minimumXor(pos+1, take, xorVal);
	return ret = min(choice1, choice2);
}
 
int main()
{
	cin >> n >> q;
	for (int i = 1; i <= n; i++) cin >> arr[i];
	while (q--)
	{
		cin >> k;
		cout << minimumXor(1, k, 0) << endl;
	}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.