Lightoj 1295 Lighting System Design (sort +DP)

Source: Internet
Author: User
Tags cas

Title Link: Lightoj 1295 Lighting System Desig

Test instructions: Give the n kinds of lamps (V,K,C,L) is the lamp (voltage, the required power cost, the unit price of the lamp, the number of lights required), the high voltage lamp can replace the low voltage lamp but the low voltage lamp cannot replace the voltage is high, each kind of lamp voltage are all the same, ask to choose the minimum cost of n lamps.

Ideas:

Because a high-voltage lamp can replace a low-voltage lamp-Sort by voltage high to low,

Then ask for the prefix and-----because when the substitution occurs, the cost of the lamp can be quickly counted.

Then there is the dp--state equation: dp[i] The minimum cost of the pre-purchase Class I lamps, sum[i] prefix and. Dp[i]=min (dp[i],dp[j-1]+ (sum[i]-sum[j-1]) *P[J].C+P[J].K);



AC Code:


#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;struct node {    int V , K,c,l;}; struct node P[1010];bool CMP (node A,node b) {    return A.V>B.V;} int dp[1010];//The minimum price of the first class I lamp int sum[1010];int main () {    int j,cas=1;    int t,i,n;    scanf ("%d", &t);    while (t--) {        scanf ("%d", &n);        for (i=1;i<=n;i++)            scanf ("%d%d%d", &P[I].V,&P[I].K,&P[I].C,&P[I].L);        Sort (p+1,p+n+1,cmp);        for (i=1;i<=n;i++) {            sum[i]=sum[i-1]+p[i].l;        }        dp[1]=p[1].k+p[1].c*p[1].l;dp[0]=0;        for (i=2;i<=n;i++) {for            (j=i;j>=1;j--) {                int tmp=sum[i]-sum[j-1];//[j,i]                if (j==i) dp[i]=dp[j-1 ]+TMP*P[J].C+P[J].K;                else {                    dp[i]=min (DP[I],DP[J-1]+TMP*P[J].C+P[J].K);        }}} printf ("Case%d:%d\n", Cas++,dp[n]);    } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Lightoj 1295 Lighting System Design (sort +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.