Hdu 3507 Print Article slope optimization DP

Source: Internet
Author: User

/* Dp [I] is the minimum cost of writing I characters dp [I] = min (dp [I], dp [j] + (sum [I]-sum [j]) * (sum [I]-sum [j]) + m ); dp [I] = min (dp [I], dp [k] + (sum [I]-sum [k]) * (sum [I]-sum [k]) + m); k is better than k when j is set, k <j => dp [j] + (sum [I]-sum [j]) * (sum [I]-sum [j])> = dp [k] + (sum [I]-sum [k]) * (sum [I]-sum [k]) + draft paper ==> [(dp [j] + sum [j] * sum [j]) -(dp [k] + sum [k] * sum [k])]/2 (sum [j]-sum [k]) <= sum [I] ==> G [j, k] = (yj-yk)/(xj-xk) <= 2 * sum [I]; [When doing the question, consider it as--> (yj-yk) <= 2 * sum [I] * (xj-xk); when division is prevented ], When, k <j, otherwise k> j; when g [k, j]> g [j, I], j can be removed (that is, the top Convex Point, proof can be divided into g [j, I] <sum [I] and> sum [I) */# include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define maxn 500005int sum [maxn]; int dp [maxn]; int a [maxn]; int q [maxn]; int y (int I) {return dp [I] + sum [I] * sum [I];} int Gup (int j, int k) // molecule {return y (j)-y (k);} int Gdown (int j, int k) // denominator {return sum [j]-sum [k];} inline int ReadInt () // click the top {char ch = ge Tchar (); int data = 0; while (ch <'0' | ch> '9') {ch = getchar ();} do {data = data * 10 + ch-'0'; ch = getchar () ;}while (ch> = '0' & ch <= '9 '); return data;} int main () {int n, m; while (scanf ("% d", & n, & m )! = EOF) {sum [0] = 0; for (int I = 1; I <= n; I ++) {a [I] = ReadInt (); sum [I] = sum [I-1] + a [I];} int head = 0, tail =-1; q [+ tail] = 0; for (int I = 1; I <= n; I ++) {// head this is fine while (head <tail & Gup (q [head + 1], q [head]) <= 2 * sum [I] * Gdown (q [head + 1], q [head]) head ++; dp [I] = dp [q [head] + (sum [I]-sum [q [head]) * (sum [I]-sum [q [head]) + m; // dp equation // tail note that the denominator has a size relationship, pass the past to change the number AH, WA dead me Ah while (head <tail & Gup (I, q [tail]) * Gdown (q [tail], q [tail-1]) <= Gup (q [tail], q [tail-1]) * Gdown (I, q [tail]) // k <j <I => tail, tail-1, I; tail --; q [++ tail] = I;} printf ("% d \ n ", dp [n]);} return 0 ;}

 

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.