Hi everyone. Can somebody please help me to solve this problem. Thank you very much. It is from JBOI 2015, it's called Startup.
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | 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 | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Name |
---|
I think it can be solved with some clever "bruteforce" approach. The first observation you should make is that for the given sequence a1, a2, ..., an we should consider sequences in form ak, ak + 1, ..., an, a1, a2, ..., ak - 1. So we should take some suffix of a and put it in front of it. Another small idea: instead of checking every prefix sum we can just check one, the minimal. So we have a correct presented sequence b1, b2, ... , bn if and only if . After we conclude this we can come up with an O(N) algorithm that solves the problem by calculating the above value for prefixes and suffixes.
I hope it helps if you have more questions just ask.
Thank you veru much, i will try to solve the problem after this algorithm, later and i will tell you if it worked.
Hi thanks for answering my question but i am still not able to solve it, may be you could be more specific or show me the code, but of course it would be better to explain more than to show me the code. Thank you very much.
Here is the code for the solution, it got 100 points thanks to mraron. Here is the code. Explenation: What it does is basically calculate two arrays of length n, L and R. L[i] means the smallest prefix sum of the original array till index i (you can see the recurrence in the code). R[i] means the smallest prefix sum of suffix i..n-1 (as you can see it in the code)
From then we shall see that the ith rotation meets the conditions if and only if R[i]>=0 (because i..n-1 suffix will be the new start of the array) and sum of the elements from i to n-1 plus L[i-1] is non negative because otherwise we would have some negative prefix in the middle. If you draw it, it becomes more clearer.