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;
} }
Computers(线性DP)的更多相关文章
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- hdu1712 线性dp
//Accepted 400 KB 109 ms //dp线性 //dp[i][j]=max(dp[i-1][k]+a[i][j-k]) //在前i门课上花j天得到的最大分数,等于max(在前i-1门 ...
- 动态规划——线性dp
我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...
- POJ 2479-Maximum sum(线性dp)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33918 Accepted: 10504 Des ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- nyoj44 子串和 线性DP
线性DP经典题. dp[i]表示以i为结尾最大连续和,状态转移方程dp[i] = max (a[i] , dp[i - 1] + a[i]) AC代码: #include<cstdio> ...
- 『最大M子段和 线性DP』
最大M子段和(51nod 1052) Description N个整数组成的序列a[1],a[2],a[3],-,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M &g ...
- 『最长等差数列 线性DP』
最长等差数列(51nod 1055) Description N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 10 12 13 14 等差子数列包括(仅包括两项的不 ...
- cf909C 线性dp+滚动数组好题!
一开始一直以为是区间dp.. /* f下面必须有一个s 其余的s可以和任意f进行匹配 所以用线性dp来做 先预处理一下: fffssfsfs==>3 0 1 1 dp[i][j] 表示第i行缩进 ...
随机推荐
- mysql查询所有表名
mysql使用sql查询表名的两种方法: 1.show tables; 2.SELECT TABLE_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WH ...
- Java学习笔记之log4j与commons-logging<转>
Java学习笔记之log4j与commons-logging<转> (2011-02-16 11:10:46) 转载▼ 标签: 杂谈 分类: 技术学习之其他 Logger来自log4j自己 ...
- js中关于this的理解
常见:在普通函数中的this,指向 全局 function thisObj(name){ this.name = name; console.log(this); } 但是在严格模式下的函数,this ...
- 解决Android 打包为apk文件时已设置签名,在OPPO手机上安装时却出现“未设置签名”的错误
解决办法:
- Java并发——ThreadPoolExecutor线程池解析及Executor创建线程常见四种方式
前言: 在刚学Java并发的时候基本上第一个demo都会写new Thread来创建线程.但是随着学的深入之后发现基本上都是使用线程池来直接获取线程.那么为什么会有这样的情况发生呢? new Thre ...
- fetch和axios区别,proxy代理配置
1.今天使用fetch调用接口时使用console.log(res.data)始终是undefined,使用anxios请求则可以成功请求到数据,非常奇怪,于是查了一圈,才搞明白是我自以为了,哎,浪费 ...
- TigerGraph REST++API
简介 - 什么是REST ++? TigerGraph TM 系统使用着名的REpresentational State Transfer(REST)架构来管理与TigerGraph核心组件,图形处理 ...
- 类似QQ在线离线好友界面
把头像设置成圆形的代码如下: package com.example.lesson6_11_id19; import android.content.Context; import android.c ...
- 在git远程仓创建项目之后,提交本地项目的使用方法
命令介绍 git 用户配置 git config --global user.name "张三" git config --global user.email "zhag ...
- zabbix server端配置
# wget http://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/2.0.6/zabbix-2.0.6.tar. ...