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 | 3821 |
3 | Benq | 3736 |
4 | Radewoosh | 3631 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3388 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 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