zoukankan      html  css  js  c++  java
  • 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)

    http://cojs.tk/cogs/problem/problem.php?pid=714

    在hzwer的刷题记录上,默默地先跳过2题T_T。。。求凸包和期望的。。T_T那是个啥。。得好好学习

    看到这题,。

    太水了。

    按价值排序后计算即可。(本来不想放题解的,但是为了满足下自己的虚荣心吧)

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << #x << " = " << x << endl
    #define printarr(a, n, m) rep(aaa, n+1) { rep(bbb, m+1) cout << a[aaa][bbb]; cout << endl; }
    inline int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    
    struct dt { 
    	int w, v; 
    	const bool operator< (const dt &a) const { return w<a.w; }
    }p[5005];
    
    int main() {
    	freopen("milk.in", "r", stdin); freopen("milk.out", "w", stdout);
    	int m, n;
    	read(m); read(n);
    	rep(i, n) { read(p[i].w); read(p[i].v); }
    	sort(p, p+n);
    	int ans=0;
    	rep(i, n) {
    		if(m<p[i].v) {
    			ans+=m*p[i].w; m=0;
    			break;
    		}
    		m-=p[i].v;
    		ans+=p[i].w*p[i].v;
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    

    描述 USACO 1.3.2

    由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要。帮助梅丽乳业找到最优的牛奶采购方案。

    梅丽乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的。此外,就像每头奶牛每天只能挤出固定数量的奶,每位奶农每天能提供的牛奶数量是一定的。每天梅丽乳业可以从奶农手中采购到小于或者等于奶农最大产量的整数数量的牛奶。

    给出梅丽乳业每天对牛奶的需求量,还有每位奶农提供的牛奶单价和产量。计算采购足够数量的牛奶所需的最小花费。

    注:每天所有奶农的总产量大于梅丽乳业的需求量。


    格式

    PROGRAM NAME: milk

    INPUT FORMAT:file milk.in

    第 1 行共二个数值:N,(0<=N<=2,000,000)是需要牛奶的总数;M,(0<= M<=5,000)是提供牛奶的农民个数。

    第 2 到 M+1 行:每行二个整数:Pi 和 Ai

    Pi(0<= Pi<=1,000) 是农民 i 的牛奶的价格。

    Ai(0 <= Ai <= 2,000,000)是农民 i 一天能卖给Marry的牛奶制造公司的牛奶数量。

    OUTPUT FORMAT:file milk.out

    单独的一行包含单独的一个整数,表示Marry的牛奶制造公司拿到所需的牛奶所要的最小费用


    SAMPLE INPUT

     100 5
     5 20
     9 40
     3 10
     8 80
     6 30
    


    SAMPLE OUTPUT

    630
  • 相关阅读:
    191. Number of 1 Bits
    190. Reverse Bits
    532. K-diff Pairs in an Array
    485. Max Consecutive Ones
    236. Lowest Common Ancestor of a Binary Tree
    235. Lowest Common Ancestor of a Binary Search Tree
    面试题68:树中两个节点的最低公共祖先
    Java—方法重写
    Java—继承
    代码块(Java)
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3931458.html
Copyright © 2011-2022 走看看