【USACO】混合牛奶
题目描述
输入
输出
样例输入
100 5
5 20
9 40
3 10
8 80
6 30
样例输出
630
来源
n,m,i,k,s:longint;
p,a:array[0..5000]of longint;
procedure qsort(s,t:longint);
var x,y,w,r:longint;
begin
x:=s; y:=t; r:=p[(x+y)div 2];
repeat
while p[x]<r do inc(x);
while p[y]>r do dec(y);
if x<=y then
begin
w:=p[x]; p[x]:=p[y]; p[y]:=w;
w:=a[x]; a[x]:=a[y]; a[y]:=w;
inc(x); dec(y);
end;
until x>y;
if s<y then qsort(s,y);
if x<t then qsort(x,t);
end;
begin
readln(n,m);
for i:=1 to m do readln(p[i],a[i]);
qsort(1,m);
s:=0;
for i:=1 to m do
begin
if a[i]>n then k:=n else k:=a[i];
dec(n,k);
inc(s,k*p[i]);
if n=0 then break;
end;
writeln(s);
end.
//冒泡排序解法
var m,n,i,j,t,c:longint; u,v:array[1..10000] of longint; begin readln(m,n); for i:=1 to n do read(u[i],v[i]); for i:=1 to n-1 do for j:=1 to n-i do if u[j]>u[j+1] then begin t:=u[j]; u[j]:=u[j+1]; u[j+1]:=t; t:=v[j]; v[j]:=v[j+1]; v[j+1]:=t; end; for i:=1 to n do begin if m<=v[i] then begin c:=c+m*u[i]; break; end; c:=c+u[i]*v[i]; m:=m-v[i]; end; writeln(c); end.
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
struct farmer{ int p,a;};
int cmp(farmer a,farmer b){return a.p<b.p;}
int main(){
freopen("milk.in","r",stdin);
freopen("milk.out","w",stdout);
int need,i,n,ans=0;
farmer a[5010];
scanf("%d%d",&need,&n);
for(int i=0; i<n; i++)scanf("%d%d",&a[i].p,&a[i].a);
sort(a, a+n, cmp);
for(i=0;i<n;i++){
if(need<=0)break;
if(a[i].a<=need){ans+=a[i].p*a[i].a; need-=a[i].a;}
else{ans+=a[i].p*need; need=0;}
}
printf("%d\n",ans);
return 0;
}
var i,m,n,x:longint;
a,p:array[0..5010] of longint;
procedure BUB;
var i,j,t:longint;
begin
for i:=1 to n-1 do
for j:=1 to n-i do
if a[j]>a[j+1] then
begin
t:=a[j]; a[j]:=a[j+1]; a[j+1]:=t;
t:=p[j]; p[j]:=p[j+1]; p[j+1]:=t;
end;
end;
begin
readln(m,n);x:=0;
for i:=1 to n do read(a[i],p[i]);
BUB;
for i:=1 to n do
begin
if m<=0 then break;
if p[i]<=m then x:=x+p[i]*a[i] else
x:=x+m*a[i];
m:=m-p[i];
end;
writeln(x);
end.