Tyvj 1342 instructor bubble chang'e on DP

Source: Internet
Author: User
342Tutorial leader time: 1000 ms/Space: 131072kib/Java class name: Main background at 03:14:35 P.M. on May 25, December 21, 2012, presidents and leaders from all over the world have gathered on the ark of China.
But there are also many civilians who want to take the ark. After all, they don't want to leave the world so they decide to either climb the ark or destroy it.

After hearing about the incident, the lhx instructor threw away the ticket in his hand. At the moment when the Earth was about to be destroyed, the principal made a small rocket and ran to the moon ......

After the instructor boarded the moon, he found that his girlfriend had forgotten to bring the moon, and he cried for a month.
But the careful teacher immediately remembered a text that he had learned in elementary school, called "The Moon", so the teacher decided to let Chang 'e become his new girlfriend.


Description the instructor took out his latest lhx (let's be happy Xixi * ^__^ *) satellite positioning system and easily located it in the Guanghan Palace.
After seeing chang'e, the instructor instantly lost his eyes with gentle and sharp eyes, but chang'e also wanted to test the instructor.
Chang'e said to the instructor, "Have you seen the crater over there? I will promise you if you walk around the ring above ~ "

The instructor used the lhx satellite positioning system to view the terrain of the ring-shaped mountains. There were N identifiable points on the ring-shaped mountains, 1 ~ clockwise ~ N number. Each ending point has an elevation, and the adjacent ending point has a different elevation (1st and N ).
The instructor can choose to start from any position and walk clockwise or counterclockwise. Each time he walks to an adjacent position, he can finally return to this position.
At any time, the instructor may be in the "Rising" or "downgrading" status.

When the instructor moves from the position I to the position J (I and j are adjacent)
J's altitude is higher than I's altitude: If the instructor is in the rising state, the instructor needs to spend two absolute values of height difference; otherwise, the instructor consumes two square meters of height difference.
J's altitude is lower than I's altitude: If the instructor is in a descending state, the instructor needs to spend two absolute values of height difference; otherwise, the instructor consumes two square meters of height difference.

Of course, when the instructors reach a foothold, they can choose to switch their status (ascending → descending → rising). Each switchover requires M points of physical strength. At the starting point, the instructors can select their own status and do not calculate the switching status. That is to say, the instructors can select any status at the beginning without consuming their physical strength.

The instructor hopes to spend the least amount of energy to become his girlfriend.


The first act of input in the input format is two positive integers n and M, that is, the number of foothold and the physical strength consumed by switching.
The next line contains n positive integers separated by spaces, indicating the height of each foothold. The question ensures that the height of the adjacent foothold is different.

The output format output contains only one positive integer, that is, the minimum physical strength required by the instructor to take a circle.


Test Example 1 Input
6 7
4 2 6 2 5 6

Output
27
Remarks [Example]
Move forward from the first destination to the lower, and switch to the rising status when the first destination is reached.
In this way, it takes 4 + (7) + 3 + 1 + 2 ^ 2 + 2 ^ 2 + 4 = 27 points of physical strength.

[Data scale]
For 10% of the data, n ≤ 10;
For 30% of data, n ≤ 100, a [I] ≤ 1000;
For 50% of data, n ≤ 1000, A [I] ≤ 100000;
For 100% of the data, n ≤ 10000, a [I] ≤ 1000000, m ≤ 1000000000. If I remember correctly, I used to split the ring into a chain of ring tree DP, however, we have never done ring DP at the end of enumeration. The specific idea is clearly explained in the tyvj question, which is roughly to enumerate the status of N points, and to do DP, however, I did not understand how to discard the redirection omitted by the breakpoint, so I opened one more dimension to show whether I had switched, and reduced m in the final statistical answer. The data size of this question is relatively large. When you change int to long, you should also increase the INF value.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MAXN 11000#define UP 0#define DN 1#define sqr(x) ((x)*(x))#define INFL 0x3f3f3f3f3f3f3f3ftypedef long long qword;inline void deal(qword &x,qword y){        if (x>y)x=y;}qword dp[MAXN][2][2][2];qword h[MAXN];int main(){        freopen("input.txt","r",stdin);//        freopen("output.txt","w",stdout);        int i,j,k,k2,n;        qword m;        scanf("%d%lld",&n,&m);        for (i=1;i<=n;i++)                scanf("%lld",h+i);        memset(dp,0x3f,sizeof(dp));        dp[1][UP][UP][0]=(h[n]

 

Tyvj 1342 instructor bubble chang'e on DP

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.