Hdu-3916-Sequence Decomposition

Source: Internet
Author: User

Question: a sequence is actually a function. The subscript starts from 1 and splits this function into the sum of several switching functions G (x, y). There can be multiple methods, calculate the maximum value of y-x + 1 in each method. --> This question is greedy! For: 2 2 2 obviously, the minimum length is y-x + 1 = 3 when it is divided into 3G (1, 3). If not, then, the minimum length must be smaller than 3 -- get -- when the two numbers are equal, they should be put into a G. For: 2 4 2 If Step 2, 4 is returned to a G instead of 2, 2G (1, 2) + 2G (2, 3), the minimum length is 2 at this time. If the three numbers at the beginning are classified into one G, 2G (1, 3) + 2G (2, 2) is obtained ), at this time, the minimum length is 1 -- available -- the minimum length is increased to 1G, and the minimum length is reduced to 1G by hours. At the beginning, I saved the input data to array a, for a's a [I] Again and again --, until all a [I] = 0, the result does not need to be said -- TLE; then, we can see that for 2 3 3 7 4 1, a [1] to a [4] can be directly-2 (the first smallest number on the left 2), for 4 4 4 7 4 1, a [1] to a [4] can be directly-3 (the largest number on the right-the number on the right). In this way, all a [I] can be reduced to 0, the result is the same-TLE; you can think of it. You cannot directly reduce all a [I] to 0 !!! --> Finally, consider G (L, R) and use m [R] to represent the amount to be subtracted from a [L] to a [R]. Scan, maintain m and the temp to be subtracted from the minimum number on the left, which is similar to the downloading mechanism of the line tree. [Cpp] # include <cstdio> # include <algorithm> # include <cstring> using namespace std; const int maxn = 10000 + 10; int main () {int T, N, i; long a [maxn], m [maxn]; // a is the input array, m [R] indicates the amount between a [L] And a [R] scanf ("% d", & T); while (T --) {scanf ("% d", & N); for (I = 1; I <= N; I ++) scanf ("% I64d ", & a [I]); a [N + 1] = 0; // Add 0 at the end. Handle int L = 1, R = 1, temp = 0, minn = 2147483647; // G (L, R), temp is the amount to be subtracted according to the specified a [L]. minn is the result. Memset (m, 0, sizeof (m); while (L <= N) {for (; L <= N; L ++) if (! (A [L]-temp) temp-= m [L]; // update else break; // find the first a [L] if (L> N) continue that is not 0; // it is possible to complete if (R <L) R = L, for example, 2 2 2 3 3 for (; R <= N; R ++) if (a [R + 1] <a [R]-m [R]) // find the right boundary {www.2cto.com minn = min (minn, R-L + 1 ); // update long dis = a [R]-m [R]-a [R + 1]; // dis G (L, R) dis = min (dis, a [L]-temp); m [R] + = dis; // accumulate incremental temp + = dis; // update break ;}} printf ("% d \ n", minn);} 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.