Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

Jimmy_Admin's blog

By Jimmy_Admin, history, 3 hours ago, In English

Please help me with this problem. Time limit is 1s. Thanks!

  • Vote: I like it
  • +1
  • Vote: I do not like it

»
2 hours ago, # |
Rev. 3   Vote: I like it +1 Vote: I do not like it

Not an Expert in this, but I think Ternary Search + DP might work (open to suggestions and discussions), here is how it goes. Proof: While calculating dp[i], we obviously will look after the prefix of array from arr[0] to arr[i-1]. Now the dp values will always be monotonously increasing from dp[0] to dp[i-1], also if we want to minimize dp[i] = dp[j] + (arr[i]-arr[j])^2 + C, where 0=<j<i then you see there are two parameters. One parameter is the squared difference between the elements and another parameter being the dp value at the selected index. The difference increases as we go from i-1 to 0 (since array is sorted) but dp values increase as we go from 0 to i-1 (as we discussed before monotonously increasing). So technically there must be an index j between 0 and n-1 (both inclusive) where the net value (sum of both parameters) is minimized. So we can ternary search over that prefix and do this till we obviously reach last index and dp[n-1] will be our required answer.

»
96 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

Sample test case might help :)

»
72 minutes ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

If I am not wrong this problem can be solve using convex hull optimization or Li chao tree. you can check this video https://www.youtube.com/watch?v=DknbfinVLLk&t=1924s for reference