luogu1208 [USACO1.3]混合牛奶 Mixing Milk

https://www.luogu.com.cn/problem/P1208

#include<bits/stdc++.h>
using namespace std;

struct farmer{
    int price;
    int amount;
}; 

struct farmer a[5050];

bool cmp(farmer x,farmer y)
{
    return x.price<y.price;
}
int main()
{
    int n,m;
    cin>>n>>m;//n,m表示需要牛奶的总量,和提供牛奶的农民个数
    int cnt=0;//计数器 
    int cur=0;//现在牛奶总量 
    int ans=0;//费用 
    for(int i=0;i<m;i++)
    {
        scanf("%d",&a[i].price);//pi,ai,表示第 i 个农民牛奶的单价,和农民 i 一天最多能卖出的牛奶量 
        scanf("%d",&a[i].amount);
    }
    sort(a,a+m,cmp);
    while(n>cur)
    {
        cur+=a[cnt].amount;
        if(cur<n)
        ans+=a[cnt].amount*a[cnt].price;
        else 
        ans+=(n-(cur-a[cnt].amount))*a[cnt].price;
        cnt++;
    }
    cout<<ans;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/cyfe67373/p/12419745.html