Hi Friends , Can someone be kind enough to help me in solving this DP problem. I managed to find the find the states (Command_no , Changes_made , Direction) .
# | 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 | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Hi Friends , Can someone be kind enough to help me in solving this DP problem. I managed to find the find the states (Command_no , Changes_made , Direction) .
Name |
---|
my solution
bool dp[position_on_the_coordinate_axis][prefix_of_commands_we_did][number_of_changes_we_did][our_direction] — can we stay on "pos" if we did "pref" commands and chamged "num" commands and we look(left, if dir == 0, right if dir == 1)
Consider an easier version of this problem which just requires changing any command at most once or not changing it at all, so you will have to process the string letter by letter. and for each one you decide changing it or not.
for example, if the current command processed = 'F', you have two options: - 1) decide not to change it (which means increasing your current position by 1 in the current direction) - 2) if there are enough changes that haven't been done, decide to change it once (which means changing your direction and decrementing number of changes required) - the same applies if current = 'T', just inverse the logic.
so it is obvious that what we need to remember in the state is the (current command the decisions are applied to), (the current direction), (the position) , (the remaining changes).
the original problem allows us that if we decided to change any command, we can change it a lot of times, as long as there are remaining changes.
so when you are deciding to change a command you can change it from (1) to (remaining changes) no. of times, so if(remaining changes > 0) you can loop from (1 -> remaining changes) and for each one get the corresponding character for this change, and apply again the same logic mentioned above, but with decrementing (remaining changes) by the number of changes you used, and in each iteration maximize that sub-problem, lets call that res2. and the result of not changing the command is res1.
the result for this sub-problem is max(res1,res2)