There are N RED balls and M BLACK balls output should be the total number to arrangements with atmost K balls can be together.
ex: Input: n = 3 m = 2 k =1 Output: 1 explantion: RBRBR
input: n = 2 n = 2 k =1 output: 2 explantion: RBRB BRBR
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
There are N RED balls and M BLACK balls output should be the total number to arrangements with atmost K balls can be together.
ex: Input: n = 3 m = 2 k =1 Output: 1 explantion: RBRBR
input: n = 2 n = 2 k =1 output: 2 explantion: RBRB BRBR
Name |
---|
Well, I will suppose that the correct statement of the problem is:
Given $$$n,m,k$$$, you have to print the amount of ways of arranging all the $$$n+m$$$ balls, such that at most $$$k$$$ balls of same color can be adjacent.
Let’s use Dynamic Programming. Let $$$f(i,j,0)$$$ be the amount of arrangements using the first $$$i$$$ balls and the last $$$j$$$ balls are of color red; and let $$$f(i,j,1)$$$ be the amount of arrangements using the first $$$i$$$ balls and the last $$$j$$$ balls are of color black.
Transition are very straightforward. And you can obtain a solution with $$$O((n+m)\cdot k^2)$$$ time complexity. The time complexity can be reduced to $$$O((n+m)\cdot k)$$$ by using prefix sums
can you send the DP solution I just started learning dynamic programming ? that will be very helpful I am very frustrated with this problem
is there any generalized equation to solve or I have to iterate over every combination
This does not solve the problem because it ignores the numbers of balls of each individual color used. Thus for the first example input, your idea will produce an output of 2, counting strings "RBRBR" and "BRBRB", even though only one of these is valid. This is, of course, easy to account for by expanding the DP state space so that $$$f(i, j, 0, c)$$$ is the number of arrangements using $$$i$$$ red balls and $$$j$$$ black balls with the last same-colored block containing exactly $$$c$$$ red balls, and likewise $$$f(i, j, 1, c)$$$ for arrangements with the last same-colored block containing $$$c$$$ black balls.
However, the time complexity will suffer: This approach needs $$$O(nmk)$$$ operations when implemented naively. It can be optimized to $$$O(nm)$$$ by using queues to perform transitions efficiently, or to $$$O((kn)^3 \log(m))$$$ by using matrix multiplication to perform long-range generalized transitions. The latter approach can be optimized to $$$O(k^3 n^2 \log(m))$$$ by noticing that the transition matrices are convolution-like (nearly block-circulant) and implementing those naively, or to $$$O(k^3 \min(n, m) \log(n) \log(m))$$$ by implementing them with FFT.
You can also try this question after you are over with this https://codeforces.net/problemset/problem/118/D In this question there are two parameters K1 and K2 instead of one single K
thanks man