zoukankan      html  css  js  c++  java
  • Computers(线性DP)

    描述

    Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, there is always a convenient way to deal with. You can replace your computer and get a brand new one, thus saving some maintenance cost. Of course, you must pay a fixed cost for each new computer you get. 

    Suppose you are considering an n year period over which you want to have a computer. Suppose you buy a new computer in year y, 1<=y<=n Then you have to pay a fixed cost c, in the year y, and a maintenance cost m(y,z) each year you own that computer, starting from year y through the year z, z<=n, when you plan to buy - eventually - another computer. 

    Write a program that computes the minimum cost of having a computer over the n year period.

    输入

    The program input is from a text file. Each data set in the file stands for a particular set of costs. A data set starts with the cost c for getting a new computer. Follows the number n of years, and the maintenance costs m(y,z), y=1..n, z=y..n. The program prints the minimum cost of having a computer throughout the n year period. 

    White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

    输出

    For each set of data the program prints the result to the standard output from the beginning of a line.

    样例输入

    3

    3

    5 7 50

    6 8

    10

    样例输出

     19

    提示

    An input/output sample is shown above. There is a single data set. The cost for getting a new computer is c=3. The time period n is n=3 years, and the maintenance costs are:

    • For the first computer, which is certainly bought: m(1,1)=5, m(1,2)=7, m(1,3)=50, 
    • For the second computer, in the event the current computer is replaced: m(2,2)=6, m(2,3)=8, 
    • For the third computer, in the event the current computer is replaced: m(3,3)=10.

    题目大意:给定电脑成本c,需要使用时间n,n行维修花费,选若干台电脑,使n年都有电脑,求最小花费。

    坑点:1.没有给定n的范围,就随意开了个大小;

      2.多组数据;

    简单dp题。

    #include <bits/stdc++.h>
    #define dbg(i) cout<<i<<endl;
    using namespace std;
    const int N=2e3+5;//随机开的数组大小
    int a[N][N],dp[N];
    int main()
    {
        int c,n;
        while(cin>>c>>n)
        {
            for(int j=i;j<=n;j++)
                cin>>a[i][j];
            for(int i=1;i<=n;i++)//dp[i]先选1~i年的那台电脑
                dp[i]=a[1][i]+c;
            for(int i=2;i<=n;i++)
                for(int j=1;j<i;j++)
                    dp[i]=min(dp[i],dp[i-j]+a[i-j+1][i]+c);
            //for(int i=1;i<=n;i++)
            //    dbg(dp[i]);
            cout<<dp[n]<<endl; 
        }
        
    }
    

      

  • 相关阅读:
    js-url打开方式
    eclipse删除所有空行
    Oracle重启 error: ora-01034:oracle not available ora-27101:shared memory realm does not exist
    最近面试遇到了高阶函数的概念混乱了
    关于跨域的cookie问题
    二叉树 呜呜
    函数的尾递归
    react context
    二叉树
    dom3级事件
  • 原文地址:https://www.cnblogs.com/zdragon1104/p/8976120.html
Copyright © 2011-2022 走看看