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 | 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 |
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
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)