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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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