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
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
意思是说买这些钻石最少花多少钱,买每种质量的钻石必须多买10个,可以买比要求质量好的钻石。
仔细想可以发现,如果买了比一种要求质量的高的钻石是最好情况,那么在要求质量和购买质量之间的钻石都要买成那个购买质量的钻石。比如质量2的钻石买成了质量5,那么质量3,4也都要买5。假设3不买5,买3或4,说明3,4在多购买10个后比5不多购买10个还便宜,既然这么便宜,那么质量2买3,4就比买5情况优,矛盾了。
dp[i]表示前i个东西的最好情况(这i个东西要是质量从小到大排列),所以dp的时候只要循环从第几个之后全买它,找到最好的情况。
代码:
#include<stdio.h>
#include<string.h>
int a[110],b[110],c[110],dp[110];
int main()
{
int T,N;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
int i,j,min;
c[0]=0;
dp[0]=0;
for(i=1; i<=N; i++)
{
scanf("%d%d",&a[i],&b[i]);
c[i]=c[i-1]+a[i]; //c[i]记下到i一共要买多少个,方便算从j到i有多少个
}
for(i=1; i<=N; i++)
{
min=999999999;
for(j=0; j<i; j++) //循环比i小的,j=0的时候全买i
if(min>dp[j]+(c[i]-c[j]+10)*b[i]) min=dp[j]+(c[i]-c[j]+10)*b[i];
dp[i]=min;
}
printf("%d\n",dp[N]);
}
return 0;
}
Description
You are given the task to design a lighting system for ahuge conference hall. After doing a lot of calculation & sketching, youhave figured out the requirements for an energy-efficient design that canproperly illuminate the entire hall. According to your design, you need lampsof n different power ratings. For some strange current regulationmethod, all the lamps need to be fed with the same amount of current. So, eachcategory of lamp has a corresponding voltage rating. Now, you know the numberof lamps and cost of every single unit of lamp for each category. But theproblem is that you are to buy equivalent voltage sources for all the lampcategories. You can buy a single voltage source for each category (each sourceis capable of supplying to infinite number of lamps of its voltage rating) andcomplete the design. But the accounts section of your company soon figures outthat they might be able to reduce the total system cost by eliminating some ofthe voltage sources and replacing the lamps of that category with higher ratinglamps. Certainly you can never replace a lamp by a lower rating lamp as someportion of the hall might not be illuminated then. You are more concerned aboutmoney-saving rather than energy-saving. Find the minimum possible cost todesign the system.
Input
Input starts with an integer T (≤ 100),denoting the number of test cases.
Each case starts with a line containing an integer n (1≤ n ≤ 1000). Each of the next n lines contains fourintegers Vi, Ki, Ci and Li (1≤ Vi ≤ 105, 1 ≤ Ki ≤1000, 1 ≤ Ci ≤ 10, 1 ≤ Li ≤ 100).Here the integers in ith line havethe following meaning.
1. Vi meansthe voltage rating,
2. Ki meansthe cost of a voltage source of this category,
3. Ci meansthe cost of a lamp of this category and
4. Li meansthe number of required lamps of this category.
You can assume that thevoltage rating for the categories will be distinct.
Output
For each case, print the case number and the minimumpossible cost to design the system.
Sample Input
1
3
100 500 10 20
120 600 8 16
220 400 7 18
Sample Output
Case 1: 778
和上一道题一样,只是先要对灯的功率排一下序。
代码:
#include<stdio.h>
#include<string.h>
int a[1010][4],c[1010],dp[1010];
int cmp(const void *a,const void *b)
{
return *(int*)a-*(int*)b;
}
int main()
{
int T,P=0;
scanf("%d",&T);
for(P=1; P<=T; P++)
{
int N;
scanf("%d",&N);
int i,j;
for(i=1; i<=N; i++) scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i][2],&a[i][3]);
qsort(a[1],N,sizeof(a[0]),cmp);
c[0]=0;
dp[0]=0;
for(i=1; i<=N; i++) c[i]=c[i-1]+a[i][3];
for(i=1; i<=N; i++)
{
int min=999999999;
for(j=0; j<i; j++)
{
if(min>dp[j]+(c[i]-c[j])*a[i][2]+a[i][1]) min=dp[j]+(c[i]-c[j])*a[i][2]+a[i][1];
dp[i]=min;
}
}
printf("Case %d: %d\n",P,dp[N]);
}
return 0;
}
这类买东西的题大概都是你可以买比他质量更好的东西,买每种东西要多付一定的钱,求最少花费。其实只要把质量从小到大排序再按这个方法dp循环就行了,不用管每种东西多少钱。