# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1128693 | jackofall718 | Kamenčići (COCI21_kamencici) | C++20 | 0 ms | 0 KiB |
#include <cstdio> // Include the C standard I/O header for input and output functions
const int MAXN = 352; // Define a constant MAXN for the maximum size of the array plus 2
int n, k, prefix[MAXN], dp[MAXN][MAXN][MAXN]; // Declare global variables for number of pebbles (n), red pebbles to avoid (k), prefix sums, and the DP table
int can_win(int l, int r, int uk) { // Define a function to determine if the current player can force a win
int& res = dp[l][r][uk]; // Reference to the DP table entry for current state for easier access and modification
if (res != -1) return res; // If this state has already been calculated, return the stored result
int total_red = prefix[n-1]; // Calculate the total number of red pebbles in the entire array
int red_left = prefix[r] - (l ? prefix[l-1] : 0); // Calculate the number of red pebbles in the subarray [l, r]
int other_red = total_red - red_left - uk; // Calculate how many red pebbles the opponent has based on the total, the interval, and the current player's count
if (uk >= k) return res = 0; // If the current player has collected k or more red pebbles, they lose (return 0)
if (other_red >= k) return res = 1; // If the opponent has collected k or more red pebbles, they lose (current player wins, return 1)
return res = !can_win(l+1, r, other_red) || !can_win(l, r-1, other_red); // Recur for the next states by removing one pebble from either end, check if any move forces a win
}
int main() { // Main function to handle input/output and initiate the solution process
scanf("%d %d %s", &n, &k, prefix); // Read the number of pebbles, the critical number of red pebbles, and the array of pebbles as a string
for (int i = 0; i < n; i++) { // Loop through the pebble string to compute the prefix sum of red pebbles
prefix[i] = (i ? prefix[i-1] : 0) + (prefix[i] == 'C'); // Accumulate the count of 'C' from the start to the current position
}
memset(dp, -1, sizeof(dp)); // Initialize the DP table with -1 indicating that no state has been computed yet
puts(can_win(0, n-1, 0) ? "DA" : "NE"); // Determine if Antun can force a win starting from the full range of pebbles with no red pebbles collected and print the result
}