everything seems okay , where is the problem ? can you explain ? problem link : http://codeforces.net/contest/855/problem/B
my code : https://paste.ubuntu.com/25608466/
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
everything seems okay , where is the problem ? can you explain ? problem link : http://codeforces.net/contest/855/problem/B
my code : https://paste.ubuntu.com/25608466/
Name |
---|
consider this input: 3 1 1 -1 -1 100 1000
should output: 1000
your solution: -1
But why ? i was always taking the maximum answer greedily....!!!
1 ≤ i ≤ j ≤ k ≤ n.
ooo , may be got it , thank you.
As I told you before, greedy approach will not work here.
You can try Dynamic programming to solve this type of problem where greedy doesn't work. But for this there is a brute force solution.
for each number in the array, let's say that this is aj and we multiply it with q. So we are fixing the middle element every time. Now we have to find ai and ak [i ≤ j and j ≤ k] such that ai * p + ak * r is maximized. So we can easily get an O(n2) solution. How to make it O(n)? Well the array is static. So we can pre-calculate the minimum and maximum in each suffix and prefix of the array and use them instead of running a loop each time to find ai and ak. This is the same solution described in the editorial btw.