题目: | 混合牛奶 | |
来源: | Usaco1.3.1 | |
题目大意: | 给出一个所需牛奶量N,和M个奶农,每个奶农有定量牛奶、且有独自的价格,求获 得N牛奶的最小价钱 | |
数据范围: | 0<=N<=2,000,000、0<= M<=5,000 | |
样例: |
100 5 5 20 9 40 3 10 8 80 6 30 |
630 |
做题思路: | 果断贪心,排序后从最小的开始取知道取够了 | |
知识点: | 贪心 |
{
ID:Dount Nameless
TASK:milk
LANG:PASCAL
}
type
act=record
p,num:longint;
end;
var
a:array[0..5010]of act;
n,i,j,m,ans:longint;
procedure qsort(l,r:longint);{<快排不解释>}
var
i,j,k:longint;
t:act;
begin
i:=l;j:=r;
k:=a[(l+r)shr 1].p;
repeat
while a[i].p<k do inc(i);
while a[j].p>k do dec(j);
ifi<=j then
begin
t:=a[i];a[i]:=a[j];a[j]:=t;
inc(i);dec(j);
end;
until i>j;
ifi<r then qsort(i,r);
ifj>l then qsort(l,j);
end;
begin
assign(input,'milk.in');reset(input);
assign(output,'milk.out');rewrite(output);
readln(n,m);
fori:=1 to m do
readln(a[i].p,a[i].num);
qsort(1,m);
ans:=0; i:=0;
while n>0 do{<取吧,没得说>}
begin
inc(i);
ifn>a[i].num then{<能都取>}
begin
n:=n-a[i].num;
ans:=ans+a[i].p*a[i].num;
end
else{<不能都取了已经>}
begin
ans:=ans+n*a[i].p;
n:=0;
end;
end;
writeln(ans);
close(input);close(output);
end.
题目来源:
http://ace.delos.com/usacoprob2?a=RexmW8CclSt&S=milk