Close the street lights

Source: Internet
Author: User

Title Description

A village has installed N street lights on a route, and each lamp has a small amount of power (that is, how much electricity is consumed in the same period of time). Lao Zhang lives in this road in the middle of a street lamp, he has a job is every morning at dawn, one by one to turn off these lights.

In order to save electricity for the village, Lao Zhang recorded the position and power of each street lamp, he every time when the lights are turned off, but Lao Zhang does not know how to turn off the lights to save electricity. Every day he turns off his street light at daybreak, and then he can turn the lights to the left or to the right. At first, he thought that the total power of the left street lamp, then calculate the total power of the right street lamp, and then choose to switch off the power of the side, and then turn off the other side of the street lights, and this is not the case, because in the process of close the proper turn may be more provincial.

It is now known that the old Zhang walked at a speed of 1m/s, the position of each street lamp (is an integer, that is, distance from the beginning of the route, Unit: M), Power (W), the time spent in old Zhang Guan is very short and negligible.

Please make a program for the old Zhang to arrange the order of the lights off, so that from the old Zhang began to turn off the lights at all times to calculate all the lamp consumption of electricity at least (after the lamp is switched off, no longer consumes electricity).

Input/output format

Input format:

The first line of the file is two digits n (1<=n<=50, which represents the total number of street lights) and C (street light number where 1<=c<=n Lao Zhang is located);

The next n rows, two data per line, represent the position and power of the 1th to Nth street lamp. The data ensures that the street lamp position is monotonically increasing.

Output format:

One data, which is the minimum power consumption (in j,1j=1w s).

Why can't you see it's an interval DP?

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 Const intmaxn= $;8 Const intinf=0x7f7f7f7f;9 intn,c;Ten intp[maxn],w[maxn],f[maxn][maxn][3],SUM[MAXN]; One intMain () { ACin>>n>>c;memset (F,inf,sizeof(f));  -    for(intI=1; i<=n;i++) cin>>p[i]>>W[i]; -    for(intI=1; i<=n;i++) sum[i]=sum[i-1]+W[i]; thef[c][c][1]=0; f[c][c][0]=0; -    for(intL=2; l<=n;l++){ -      for(intI=1; i+l-1<=n;i++){ -       intj=i+l-1; +f[i][j][0]=min (f[i+1][j][0]+ (p[i+1]-p[i]) * (sum[n]-sum[j]+Sum[i]), -f[i+1][j][1]+ (P[j]-p[i]) * (sum[n]-sum[j]+sum[i])); +f[i][j][1]=min (f[i][j-1][1]+ (p[j]-p[j-1]) * (sum[n]-sum[j-1]+sum[i-1]), Af[i][j-1][0]+ (P[j]-p[i]) * (sum[n]-sum[j-1]+sum[i-1])); at     } -   } -Cout<<min (f[1][n][0],f[1][n][1]) <<Endl; -}

Close the street lights

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.