1. 程式人生 > 實用技巧 >洛谷P1208

洛谷P1208

題目描述

由於乳製品產業利潤很低,所以降低原材料(牛奶)價格就變得十分重要。幫助 Marry 乳業找到最優的牛奶採購方案。

Marry 乳業從一些奶農手中採購牛奶,並且每一位奶農為乳製品加工企業提供的價格是不同的。此外,就像每頭奶牛每天只能擠出固定數量的奶,每位奶農每天能提供的牛奶數量是一定的。每天 Marry 乳業可以從奶農手中採購到小於或者等於奶農最大產量的整數數量的牛奶。

給出 Marry 乳業每天對牛奶的需求量,還有每位奶農提供的牛奶單價和產量。計算採購足夠數量的牛奶所需的最小花費。

注:每天所有奶農的總產量大於 Marry 乳業的需求量。

輸入格式

第一行二個整數n,mn,m,表示需要牛奶的總量,和提供牛奶的農民個數。

接下來mm行,每行兩個整數p_i,a_ipi,ai,表示第ii個農民牛奶的單價,和農民ii一天最多能賣出的牛奶量。

輸出格式

單獨的一行包含單獨的一個整數,表示 Marry 的牛奶製造公司拿到所需的牛奶所要的最小費用。

輸入輸出樣例

輸入 #1
100 5
5 20
9 40
3 10
8 80
6 30
輸出 #1
630

說明/提示

【資料範圍】
對於100\%100%的資料:
0 \le n,a_i \le 2 \times 10^60n,ai2×106,0\le m \le 50000m5000,0 \le p_i \le 10000pi1000

題目翻譯來自 NOCOW。

排序,貪心。

把價格低的放前面。

程式碼:

#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long int ll;
const int maxn=100005;
int n,m,ans,tot=1;
struct node{
    int c,w;
}t[maxn];
bool cmp(node a,node b){
    return a.c<b.c;
}
int main(){
    scanf("%d%d",&n,&m);
    for
(int i=1;i<=m;i++){ scanf("%d%d",&t[i].c,&t[i].w); } sort(t+1,t+1+m,cmp); while(n!=0){ if(n>t[tot].w){ n-=t[tot].w; ans+=t[tot].c*t[tot].w;tot++; }else{ ans+=t[tot].c*n;tot++; n=0; } } printf("%d\n",ans); return 0; }

USACO Training Section 1.3