tony4875's blog

By tony4875, history, 14 months ago, In English

Hi codeforces,

Could anyone help me explain why my code got AC in C++20 and C++23 but failed in C++17?

The code is identical for all 3 submissions.
C++17 submission
C++20 submission
C++23 submission

I used C++17 on my local, so I prefer to use C++17 to submit.

Thank you.

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By tony4875, history, 17 months ago, In English

Problem:

You are given an integer $$$n$$$ and a target array $$$a = [a_1, a_2, \dots, a_n]$$$. Initially, you have an array $$$b$$$ of length $$$n$$$ with all elements equal to $$$0$$$. In one operation, you select a segment $$$[l, r]$$$ ($$$1 \le l \le r \le n$$$) and positive $$$x$$$, and add $$$x$$$ to each element in that segment: $$$b[i] = b[i] + x$$$ for all $$$i$$$ in range $$$[l, r]$$$

Determine the minimum number of operations required to transform array $$$b$$$ into array $$$a$$$.

Constraints:

$$$ 1 \lt = n \lt = 10^5 $$$
$$$ 0 \lt = a_i \lt = 10^9$$$
$$$ x \gt 0 $$$

I think computing the sum of positive differences between adjacent elements $$$ans$$$ $$$+=$$$ $$$\sum_{i = 1}^n $$$ $$$max(a_i - a_{i - 1}, 0)$$$ where we set $$$a_0 = 0$$$. However, this approach only works when $$$x = 1$$$.

I appreciate any help you can provide.

Full text and comments »

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