ACM学习历程——POJ1260 Pearls(动态规划)
Description
Every month the stock manager of The Royal Pearl prepares a
list with the number of pearls needed in each quality class. The pearls
are bought on the local pearl market. Each quality class has its own
price per pearl, but for every complete deal in a certain quality class
one has to pay an extra amount of money equal to ten pearls in that
class. This is to prevent tourists from buying just one pearl.
Also The Royal Pearl is suffering from the slow-down of the
global economy. Therefore the company needs to be more efficient. The
CFO (chief financial officer) has discovered that he can sometimes save
money by buying pearls in a higher quality class than is actually
needed.No customer will blame The Royal Pearl for putting better pearls
in the bracelets, as long as the
prices remain the same.
For example 5 pearls are needed in the 10 Euro category and
100 pearls are needed in the 20 Euro category. That will normally cost:
(5+10)*10+(100+10)*20 = 2350 Euro.Buying all 105 pearls in the 20 Euro
category only costs: (5+100+10)*20 = 2300 Euro.
The problem is that it requires a lot of computing work
before the CFO knows how many pearls can best be bought in a higher
quality class. You are asked to help The Royal Pearl with a computer
program.
Given a list with the number of pearls and the price per
pearl in different quality classes, give the lowest possible price
needed to buy everything on the list. Pearls can be bought in the
requested,or in a higher quality class, but not in a lower one.
Input
test case starts with a line containing the number of categories c
(1<=c<=100). Then, c lines follow, each with two numbers ai and
pi. The first of these numbers is the number of pearls ai needed in a
class (1 <= ai <= 1000).
The second number is the price per pearl pi in that class (1
<= pi <= 1000). The qualities of the classes (and so the prices)
are given in ascending order. All numbers in the input are integers.
Output
Sample Input
2
2
100 1
100 2
3
1 10
1 11
100 12
Sample Output
330
1344 这是一道dp题,关键在于想出状态转移方程。
对于n个,一开始最初的计算方式来计算的话,n个是独立的个体,而到最后最优的计算方式,是把某几个设为一类。也是就是相当于把n个插板来分成若干集合,其中的价格取集合中最大的。
这样的话,可以看成这个最终状态是分几步实现的。
从纵向看可以,考虑任意前i个的最优。
从横向来看,任意j,从第j个到第i个都有可能合并成一个集合。.
横向的每次生成对i的一个集合。
纵向形成所有的集合。
状态转移方程为:
dp[i] = min{dp[j] + (sum[i]-sum[j])*p[i]) (j < i); 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10 using namespace std; int a[105], p[105];
int sum[105], dp[105]; int main()
{
//freopen("test.txt", "r", stdin);
int T, n;
scanf("%d", &T);
for (int times = 0; times < T; ++times)
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &a[i], &p[i]);
}
sum[0] = 0;
dp[0] = 0;
for (int i = 1; i <= n; ++i)
{
sum[i] = sum[i-1] + a[i];
dp[i] = dp[i-1] + (a[i]+10)*p[i];
}
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < i; ++j)
{
dp[i] = min(
dp[i],
dp[j] + (sum[i]-sum[j]+10)*p[i]);
}
}
printf("%d\n", dp[n]);
}
return 0;
}
ACM学习历程——POJ1260 Pearls(动态规划)的更多相关文章
- ACM学习历程—HDU2476 String painter(动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...
- ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...
- ACM学习历程—Hihocoder 1290 Demo Day(动态规划)
http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...
- ACM学习历程—HDU5586 Sum(动态规划)(BestCoder Round #64 (div.2) 1002)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5586 题目大意就是把一段序列里面的数替换成f(x),然后让总和最大. 首先可以计算出初始的总和,以及每 ...
- ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...
- ACM学习历程—SNNUOJ1213 加油站问题(动态规划 || 数学)
题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1213 这是这次微软实习面试的一道题,当时只相出了一个2n的做法,面试官让我优化成n的做 ...
- ACM学习历程—广东工业大学2016校赛决赛-网络赛E 积木积水(最值问题 || 动态规划)
题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=4 这个题目自然会考虑到去讨论最长或者最短的板子. 笔上大概模拟一下的话,就 ...
随机推荐
- spring boot 发布成包所需插件
在pom.xml里配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...
- Java中的Enum的继承
public interface Icolor{ int apply(int x,int y); } public enum color implements Icolor{ plus("+ ...
- 设置jvm运行内存
:1.右击项目—Bulid Path—Configure Build Path—Libraries,找到JRE System Libraary[Sun JDK 1.6.0_13],选中JRE Syst ...
- C#中web.config文件详解
C#中web.config文件详解 一.认识Web.config文件 Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NE ...
- ios美颜 调研 GPUImage GPUImageBeautifyFilter BeautifyFaceDemo
最近需要给直播项目中添加美颜的功能,调研了很多SDK和开源代码(视决,涂图,七牛,金山云,videoCore等),综合成本/效果/对项目侵入性,最后决定使用一款基于GPUImage实现的 Beauti ...
- PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案
一:PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案 (1) php服务器把图片处理成缩率图或指定大小的图片在通过PHP程序代码 操作FTP 上传到图片服务器. 二:图片服务器 ...
- WCF配置心得
根据蒋金楠老师的博文所说的, WCF的终结点有三个要素组成,分别是地址(Address).绑定(Binding)和契约(Contract),简记可写成Endpoint = ABC. 地址:地址决定了服 ...
- 离线安装Cloudera Manager5.3.4与CDH5.3.4(一)
这几天一直在安装CDH,头都搞大了,安装第三次,最终成功了. 第一次问题非常多.后面卸载了.由于没有卸载干净导致第二次安装失败. 后来索性重装系统了.直接使用了纯净系统进行安装.一个人跑到学院机房去装 ...
- uva--10714+找规律
题意: 一根长度为len的木棍上有n仅仅蚂蚁.蚂蚁们都以1cm/s的速度爬行;假设一仅仅蚂蚁爬到了木棍的端点,那么他就会掉下去;假设两仅仅蚂蚁碰到一起了,他们就会掉头往相反方向爬行.输入len和n仅仅 ...
- EntityFramework走马观花之CRUD(下)
我在Entity Framework系列文章的CRUD上篇中介绍了EF的数据查询,中篇谈到了EF的数据更新,下篇则聊聊EF实现CRUD的内部原理. 跟踪实体对象状态 在CRUD上篇和中篇谈到,为了实现 ...