USACO--1.3Mixing Milk

就是一道简单贪心,肯定是要先买价格低牛奶,所以先按价格排序就行了。

代码如下:

/*
ID: 15674811
LANG: C++
TASK: milk
*/
#include<cstring>
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;

typedef struct
{
       int p;
       int num;
}M;
M m[5500];

bool cmp(M m1,M m2)
{
        return m1.p<m2.p;
}

int main()
{
      ofstream cout("milk.out");
      ifstream cin("milk.in");
     int n,m1;
     while(cin>>n>>m1)
     {
             for(int i=1;i<=m1;i++)
                  cin>>m[i].p>>m[i].num;
             sort(m+1,m+m1+1,cmp);
             int sum=0,price=0;
             for(int i=1;i<=m1;i++)
             {
                    if(sum+m[i].num<=n)
                    {
                          sum+=m[i].num;
                          price+=m[i].num*m[i].p;
                    }
                    else
                    {
                           price+=(n-sum)*m[i].p;
                           break;
                    }
             }
             cout<<price<<endl;
     }
   return 0;
}
时间: 2024-10-26 01:20:40

USACO--1.3Mixing Milk的相关文章

usaco Mother&#39;s Milk

给了三个木桶的容量,起始状态下C木桶装满,然后三个木桶相互倒牛奶,问当A木桶为空时的C木桶的牛奶体积的可能情况. 直接深度优先搜索,模拟三个木桶相互倒的情况 相互倒得情况本可以写的更简洁,但为了可阅读性我并没有这么干 不过这道题在本机上调试时,我曾将一个我认为无关紧要的地方写错,因为两个数组大小一样,所以在sizeof的括号中写了另外一个,结果却出不了正解. 有机会要好好查查memcyp函数的用法. /* ID: modengd1 PROG: milk3 LANG: C++ */ #includ

USACO Mother&#39;s Milk(bfs)

题目请点我 题解: 水杯倒水的问题很经典,套路也是一样的,bfs找出所有状态.这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的很方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #

USACO 5.3 Milk Measuring

Milk MeasuringHal Burch Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest milk and deliver it in one big bottle to a customer. He fills that bottle with exactly the number of quarts that the customer orders. Farmer John has always b

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l

USACO 1.3 Mixing Milk(贪心)

USACO Mixing Milk 简单的贪心,读入数据,按单价从小到大排序,然后从最便宜的买起,直到买够为止. /* ID:twd30651 PROG:milk LANG:C++ */ #include<iostream> #include<fstream> #include<stdlib.h> using namespace std; int N; int M; typedef struct node { int P; int A; }node; node data

USACO milk

/* ID:kevin_s1 PROG:milk LANG:C++ */ #include <iostream> #include <string> #include <cstring> #include <cstdio> #include <algorithm> #define MAXN 5001 using namespace std; int N,M; struct farmer{ int Pi; int Ai; }farmers[MAXN

USACO 1.4 Mother&#39;s Milk

Mother's Milk Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours m

USACO Section1.4 Mother&#39;s Milk 解题报告

milk3解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 有三个桶,容量分别是A.B.C,开始C桶是满的. 你可以不断将某个桶的奶倒到另一个桶里,但只允许全倒过去,或者将后者倒满,前者留下

【USACO】Mother&#39;s Milk(搜索)

一开始还在想去重的问题,结果发现后台数据貌似没有重复的情况= = /* ID: 18906421 LANG: C++ PROG: milk3 */ #include<cmath> #include<cstdio> #include<vector> #include<algorithm> using namespace std; const int maxn = 25; int vis[maxn][maxn][maxn] = {0}; vector<in