Zoj2096 door to secret, dual-tune DP

Source: Internet
Author: User

This is similar to the dual-tune DP in the previous question, but it is a bit different.

You can select a vertex as the initial vertex. First, right-click some of the vertices, right-click the vertices on the rightmost vertex, and then left-click the remaining vertices.

At the same time, the question requires that the last knock to the right and the first knock to the left are consecutive.

So similar to zoj 2581, but this time we started from the rightmost end, not two paths for two villains

Wa once, because it is infinitely small...

/*************************************** **************************************** # Author: neo Fung # Email: neosfung@gmail.com # Last modified: 2012-05-18 # filename: zoj2096 door to secret. CPP # description: **************************************** **************************************/# ifdef _ msc_ver # define debug # DEFINE _ crt_secure_no_deprecate # endif # include <fstream> # include <stdio. h> # Include <iostream> # include <string. h> # include <string> # include <limits. h> # include <algorithm> # include <math. h> # include <numeric> # include <functional> # include <ctype. h> # define Max 1010 using namespace STD; int num [Max], DP [Max] [Max]; int main (void) {# ifdef debug freopen (".. /stdin.txt "," r ", stdin); freopen (".. /stdout.txt "," W ", stdout); # endif int N, ncase = 1; // scanf (" % d ", & ncase); While (~ Scanf ("% d", & N) {for (INT I = n-1; I> = 0; -- I) scanf ("% d ", & num [I]); For (INT I = 0; I <n; ++ I) for (Int J = 0; j <n; ++ J) DP [I] [J] = 1 <16; DP [0] [0] = 0; For (INT I = 1; I <n; ++ I) for (Int J = 0; j <I; ++ J) {DP [I] [I-1] = min (DP [I] [I-1], DP [I-1] [J] + ABS (Num [J]-num [I]); // J goes to IDP [I] [J] = min (DP [I] [J], DP [I-1] [J] + ABS (Num [I-1]-num [I]); // I-1 goes to I} int ans = int_max; For (INT I = 0; I <n-1; ++ I) ans = min (DP [n-1] [I], ANS); printf ("% d \ n", ANS);} 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.