Hi Guys,
can anyone help me solve this problem using DP ? is that even possible ?
http://acm.timus.ru/problem.aspx?space=1&num=1005
So far only the brute force (generate all permutations) approach seems to be working.
Many thanks
Hi Guys,
can anyone help me solve this problem using DP ? is that even possible ?
http://acm.timus.ru/problem.aspx?space=1&num=1005
So far only the brute force (generate all permutations) approach seems to be working.
Many thanks
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 152 |
| 3 | Proof_by_QED | 146 |
| 3 | Um_nik | 146 |
| 5 | Dominater069 | 144 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
| Name |
|---|



Use bit-masks. Take a integer number and look at its bits. If i-bit is 1, then we put stone i+1 in first pile, else in second pile. You need check all numbers between 0 and 2^N - 1 (all possible variants of ones).
It's not DP, but it's fast brut force O (N * 2^N). Also this task can be solved by time O (N * 2^(N/2)) with method meet-in-middle.
For dp solution you can use idea of Knapsack problem.
dp[i][j] = true iff using some of i first stones we can get the pile of weight j.
Look. Let f[i][j]=1 if using some of i first stones we can get the pile of weight j.
Then f[i][j]=f[i-1][j] | f[i-1][j-a[i]];
Exactly =) I forgot about it. But with 2 lines it'll work also, only 4 Mb(or 500 Kb) of 16 Mb.
Divide-and-conquer technique solves this problem in O(2^(N/2)*N)