Description

In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jewelry with pearls in it. The Royal Pearl has its name because it delivers to the royal family of Pearlania. But it also produces bracelets and necklaces for ordinary people. Of course the quality of the pearls for these people is much lower then the quality of pearls for the royal family.In Pearlania pearls are separated into 100 different quality classes. A quality class is identified by the price for one single pearl in that quality class. This price is unique for that quality class and the price is always higher then the price for a pearl in a lower quality class.
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

The first line of the input contains the number of test cases. Each
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

For each test case a single line containing a single number: the lowest possible price needed to buy everything on the list.

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(动态规划)的更多相关文章

  1. ACM学习历程—HDU2476 String painter(动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...

  2. ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...

  3. ACM学习历程—Hihocoder 1290 Demo Day(动态规划)

    http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...

  4. ACM学习历程—HDU5586 Sum(动态规划)(BestCoder Round #64 (div.2) 1002)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5586 题目大意就是把一段序列里面的数替换成f(x),然后让总和最大. 首先可以计算出初始的总和,以及每 ...

  5. ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...

  6. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

  7. ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)

    http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...

  8. ACM学习历程—SNNUOJ1213 加油站问题(动态规划 || 数学)

    题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1213 这是这次微软实习面试的一道题,当时只相出了一个2n的做法,面试官让我优化成n的做 ...

  9. ACM学习历程—广东工业大学2016校赛决赛-网络赛E 积木积水(最值问题 || 动态规划)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=4 这个题目自然会考虑到去讨论最长或者最短的板子. 笔上大概模拟一下的话,就 ...

随机推荐

  1. SpringMVC请求流程与原理分析

    SpringMVC的工作原理图: SpringMVC流程 1.  用户发送请求至前端控制器DispatcherServlet. 2.  DispatcherServlet收到请求调用HandlerMa ...

  2. error_logger 爆炸

    具有讽刺意味的是,负责错误日志的进程竟然是最为脆弱的之一.在Erlang的缺省安装中,error_logger39负责记录日志(写入磁盘或者发送到网络上),它的速度要比错误产生的速度慢得多.尤其是在记 ...

  3. HDU 1452 Happy 2004(唯一分解定理)

    题目链接:传送门 题意: 求2004^x的全部约数的和. 分析: 由唯一分解定理可知 x=p1^a1*p2^a2*...*pn^an 那么其约数和 sum = (p1^0+p1^1^-+p1^a1)* ...

  4. TCP交换数据流——Nagle算法简单记录

    Nagle算法: 该算法提出的目的是想解决网络中大量的小的TCP数据包造成网络拥塞的问题,举个例子,当客户端要发送一个字节的TCP数据包到服务器时,我们实际上产生了41字节长的分组:包括20字节的IP ...

  5. 九度OJ 1178:复数集合 (插入排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8393 解决:1551 题目描述: 一个复数(x+iy)集合,两种操作作用在该集合上: 1.Pop 表示读出集合中复数模值最大的那个复数,如 ...

  6. 九度OJ 1046:求最大值 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:9861 解决:4013 题目描述: 输入10个数,要求输出其中的最大值. 输入: 测试数据有多组,每组10个数. 输出: 对于每组输入,请输 ...

  7. HNOI2016

    本蒟蒻表示终于$AC$了$HNOI2016$的六道毒瘤题... 高兴! 附上各个题的题解: $DAY1$: $T1$: BZOJ4537: [Hnoi2016]最小公倍数 $T2$: BZOJ4538 ...

  8. 关于SAP S4 HANA 的13个问题

    SAP S/4HANA的路线图是怎样的?价格是多少?下一步还将添加哪些新模块?莫不闻专业SAP问答平台结合SAP HANA及SAP HANA应用商务套件开发全球负责人Uwe Grigoleit帮大家整 ...

  9. clone和dup

    ruby中clone和dup都是对一个对象的浅拷贝,其区别如下: 1.clone会拷贝单例方法,而dup不会. a = Object.new def a.hello "hello" ...

  10. 《程序员代码面试指南》第八章 数组和矩阵问题 找到无序数组中最小的k 个数

    题目 找到无序数组中最小的k 个数 java代码 package com.lizhouwei.chapter8; /** * @Description: 找到无序数组中最小的k 个数 * @Autho ...