Help on a DP problem

Revision en6, by thecasuist, 2020-08-19 14:50:40

Hi everyone. I am fairly new to programming. I would be grateful if anyone could provide a counter test case to a solution I had attempted to implement for the problem "Stones" of AtCoder Educational DP Contest.

For the sake of convenience, I have attached the screenshot of the problem here.

My approach was as follows:

declare a dp table consisting of boolean values (initialised to 0) [dimension (n rows & k+1 columns) The definition of my dp state is as follows: dp[i][j] ==> State of the situation when there are j stones and I am considering [A0, A1, ... Ai] choices of taking the stones In a nutshell I am attempting to take the OR of the result dp[i-1][j] & (if j >= a[i]) !(dp[i][j-a[i]])

SOURCE CODE:

#include <bits/stdc++.h>
using namespace std;
 
void test(vector<int> &a, int k){
	bool dp[a.size()][k+1];
	memset(dp, 0, sizeof(dp));
	int n = a.size();
	for(int i = a[0]; i <= k; i++){
		dp[0][i] = !(dp[0][i-a[0]]);
	}
	for(int i = 1; i < a.size(); i++){
		for(int j = 1; j <= k; j++){
			bool taken = 0, ignored = 1;
			if(j &mdash; a[i]>=0){
				taken = !(dp[i][j-a[i]]);
			}
			ignored = dp[i-1][j];
			dp[i][j] = taken||ignored;
		}
	}
	if(dp[n-1][k]){
		cout << "First" << endl;
	}
	else{
		cout << "Second";
	}
	return;
}
 
int main(){
	int n, k;
	cin >> n >> k;
	vector<int> a(n);
	for(int i =0 ; i < n; i++){
		cin >> a[i];
	}
	test(a, k);
	return 0;
}
Tags #dynamic programing, atcoder educational dp, #atcoder

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English thecasuist 2020-08-19 14:53:35 4
en6 English thecasuist 2020-08-19 14:50:40 37
en5 English thecasuist 2020-08-19 14:49:40 37 Tiny change: 'n}\n~~~~~'' -> 'n}\n~~~~~'\n~~~~~\nYour code here...\n~~~~~\n\n'
en4 English thecasuist 2020-08-19 14:48:03 2
en3 English thecasuist 2020-08-19 14:47:26 15
en2 English thecasuist 2020-08-19 14:46:15 2
en1 English thecasuist 2020-08-19 14:45:22 1461 Initial revision (published)