HDOJ---1300 Pearls[DP(先证明出相互代替不会跳跃)]

Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 767    Accepted Submission(s): 325


Problem 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
 

 

Source
 

 

Recommend
Eddy
 
 
 
 
 
 
 
 

题意解释:

有n个等级的珠宝,等级依次升高,等级越高价钱越高,每买一个等级的任何数量的珠宝必须多付10颗此种珠宝的价钱,可以用高等级的珠宝代替低等级的,问要买到若干规定的数量和等级珠宝的最少花费。例如买5颗价值为10的、100颗价值为20的珠宝,有两种方案:一种为分别买两种等级的珠宝价钱为(5+10)*10+(100+10)*20 = 2350;另一种是将等级低的(即价格低)的珠宝全部换为等级高的,此时价钱为(5+100+10)*20 = 2300,故第二种方案较优。

 

选题原因:

此题是比较典型的动态规划在实际问题中应用的问题,难度适中,特征为通过组合子问题的解来解决整个问题,且子问题不是互相独立,即各子问题包含公共的子子问题,这也是所有动态规划问题的共同特点。(摘自《算法导论》)

 

解题思路:

首先需要证明一点,用高一等级的代替低一等级的必须是连续代替。例如 3 5,90 7,100 12要想代替3个5价值的首先考虑用7价值的代替,因为3*7=21 (3+10)*5=65 显然比较合算,这样就不需要考虑用12价值的那个代替了,因为前面已经有了最优解,也就是说相互代替不会跳跃。

证明这一点后开始分析,用sum[i]表示前i中珠宝总数,对于第i等级珠宝,ans[i]表示其最优解,那么ans[i]可能是(sum[i]+10) *p[i] 即前面所有的珠宝用i的价值去买,或者(sum[i]-sum[1]+10)*p[i]+ans[1] 即从第二个开始前面的所有珠宝用i的价值去买,或者(sum[i]-sum[2] +10)*p[i] + ans[2] 即从第三个。。。依次计算,其中最小的一个就是i的解。注意,上面提到的跳跃是指不用i+1等级珠价值去计算i等级以及其以下的珠宝,但可以将i以下等级的珠宝归为等级为i的价格进行计算,这样就会保证当前价值最小。这样状态方程便有ans[i]=min(ans[i],(sum[i]-sum[j]+10)*p[i]+ans[j]),(1<=j<=i)。

在写状态方程时是整体到局部的思路去考虑,具体写代码特别是边界条件时则需要自底下向上推导。通常需要将状态数组(此题为ans[])分开处理,即先对其边界赋值,处理特殊情况,再按状态方程进行计算。

 
 
 
 
 
 
PS:
比赛的时候没有做出来,后来去网上找了解题报告,发现这道题目的思想值得我去好好学习,DP弱爆了。
code:
 1 #include <iostream>   
 2 #include <iomanip>   
 3 #include <fstream>   
 4 #include <sstream>   
 5 #include <algorithm>   
 6 #include <string>   
 7 #include <set>   
 8 #include <utility>   
 9 #include <queue>   
10 #include <stack>   
11 #include <list>   
12 #include <vector>   
13 #include <cstdio>   
14 #include <cstdlib>   
15 #include <cstring>   
16 #include <cmath>   
17 #include <ctime>   
18 #include <ctype.h> 
19 using namespace std;
20 
21 #define MAXN 1100
22 
23 typedef struct kind
24 {
25     int num,price;
26 }Kind;
27 Kind kind[MAXN];
28 
29 int sum[MAXN];
30 int ans[MAXN];
31 
32 int main()
33 {
34     int t;
35     int i,j;
36     int n;
37     scanf("%d",&t);
38     while(t--)
39     {
40         scanf("%d",&n);
41         sum[0]=0;
42         memset(ans,0,sizeof(ans));
43         for(i=1;i<=n;i++)
44         {
45             scanf("%d%d",&kind[i].num,&kind[i].price);
46             sum[i]=sum[i-1]+kind[i].num;
47             ans[i]=(sum[i]+10)*kind[i].price;
48         }
49         for(i=1;i<=n;i++)                                
50             for(j=1;j<=i;j++)                                    //枚举
51                 ans[i]=min(ans[i],(sum[i]-sum[j]+10)*kind[i].price+ans[j]);
52         printf("%d\n",ans[n]);
53     }
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/XBWer/archive/2012/08/13/2636836.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值