11900 – Boiled Eggs UVA Solution

This is an easy problem from uva judge http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3051

Greedy , Adhoc..

How I did it

  • Read the number of cases.
  • For each case read the number of eggs, max eggs that the bowl support, and max weight that bowl support.
  • Read the weight of the eggs and push it inside a vector.
  • Init tot and totW to zero. // If tot >= eggs or totW > max bowl weight I break the loop.
  • Traverse the vector until  tot <  eggs && totW < max bowl weight. Increment tot, and totW.
  • Print the answer.

Hints

  • The weight of the eggs is already sorted.
  • The input is really small.

Code

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main()
{
int casos, eggs, maxEggs, maxWeight, val;
int tot, totW;
scanf("%d", &casos);
for(int a=1; a<=casos; a++)
{
    vector<int> vec;
    scanf("%d %d %d", &eggs, &maxEggs, &maxWeight);
    for(int b=0; b<eggs; b++)
    {
        scanf("%d", &val);
        vec.push_back(val);
    }

    tot = totW = 0;
    for(int x=0; x<vec.size(); x++)
    {
        totW += vec[x];
        if(totW > maxWeight)
           break;
        else if( tot < maxEggs)
                tot++;
        else
            break;
    }
    printf("Case %d: %d\n", a, tot);
}
return 0;
}

Leave a comment