Codeforces 13C. Sequence

Source: Internet
Author: User

Here is a string of 5000-length numbers. You need to change some numbers. Yes, the new series is not decreasing... This type of question can be left-side tree... It doesn't matter if the data range is large. However, since the data range is 5000, use an n ^ 2 character first, is the number after transformation or the number in the original sequence, why? Assume that each number is a tree of a certain height. If we find that the height of a tree in a certain range is not monotonous, we should change the height of a tree, you can either increase it or cut it down. Which value does it increase or cut down? It must be the height of a nearby tree !! It is okay to draw a sketch, so we can construct such a DP. dp [I] [j] indicates the number of the first I in the constructed sequence, the number of I is the minimum cost less than or equal to a [j. Use a scrolling array to implement this function. The space is limited to [cpp] # include <cstdio >#include <cstdlib >#include <cstring >#include <algorithm> using namespace std; const int N = 5100; const _ int64 inf = (_ int64) 1 <62; _ int64 a [N], B [N], dp [2] [N]; _ int64 min (_ int64 a ,__ int64 B) {return a <B? A: B;} int main () {int n, I, j; while (scanf ("% d", & n )! = EOF) {for (I = 1; I <= n; I ++) {scanf ("% I64d", & a [I]); B [I] = a [I];} sort (B + 1, B + n + 1); memset (dp [0], 0, sizeof (dp [0]); int cur = 0; for (I = 1; I <= n; I ++) {cur ^ = 1; fill (dp [cur], dp [cur] + n + 1, inf); for (j = 1; j <= n; j ++) {dp [cur] [j] = min (dp [cur] [J-1], dp [cur ^ 1] [j] + abs (a [I]-B [j]); }}_ int64 ans = dp [cur] [1]; for (I = 2; I <= n; I ++) {if (dp [cur] [I] <ans) ans = dp [cur] [I];} printf ("% I64d \ n", ans );}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.