How to solve this Photoshoot problem in O(n) ? The best I could come up with is a O(n^2) idea.Would be greatful for some hints.
# | 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 |
How to solve this Photoshoot problem in O(n) ? The best I could come up with is a O(n^2) idea.Would be greatful for some hints.
Name |
---|
When $$$n=2$$$, the answer is
1 2
.When $$$n=3$$$, we can brute-force the answer.
Consider when $$$n \geq 4$$$. An observation is that $$$b_i-b_{i-1}=a_{i + 1} - a_{i - 1}$$$. Now we can look at odd and even indices separately. For odd indices, we only have to find the index $$$k_1$$$ that gives the smallest value among all odd indices. Similarly, we find index $$$k_2$$$ that gives the smallest value among all even indices. With these, if we fix the value of $$$a_{k_1}$$$, all odd elements are known, which gives the smallest value of even elements. Then we can fill all even positions. (Or we can first fix the value of $$$a_{k_2}$$$ and do similar things)
Now we can just discuss two cases, whether $$$a_i=1$$$ for some odd $$$i$$$, or $$$a_i=1$$$ for some even $$$i$$$. We can just try both cases and check if the answer we get from one case fits the requirements.
Here is my (ugly) implementation. I'm not sure if this is the simplest $$$O(n)$$$ solution though.
Let a1 = x. For sample B = [4, 6, 7, 6] you get A = [x, 4-x, 2+x, 5-x, x+1]. Since it is guaranteed an answer exists, all the x + k "positive" terms and -x + k "negative" terms have different constants k. For each category (positive or negative), set the smallest term to be equal to 1 and check.
Code