C5- Disposal

Title Description

Garbage is coming, a main garbage dump Nan hope in time for the garbage will smash all the factory waste landfill. For this purpose a special head of the farm to rent are n Disposal, Disposal each have a maximum usage time ti, can handle a total mi tons of garbage during this time, you can use any duration at any time, but with End can not be reused. Since the field was too poor at the same time can only run one Disposal, now I want to ask before the advent of garbage, trash up to the number of pieces. To simplify the calculation, all the time units is calculated in hours.

Entry

The first two and the number N is the number coming from garbage Disposal hour time T

Next N lines two integers corresponding to ti and mi

All numbers are not greater than 1e5

Export

Output line, the maximum weight of the garbage can handle, 2 decimal places, tonnes

SAMPLE INPUT

1 2
2 3

Sample Output

3.00
 

Code - -

Standard greedy, greedy can be greedy

Elected ti / mi maximum every time you put it runs out

If not fit, then loaded part

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct Goods
{
    int v,w;
    double p;
} g[100005];
int cmp(struct Goods x,struct Goods y)
{
    return x.p>y.p;
}

int main()
{
        int N, V;
        double sum = 0;

        cin>>N>>V;
        for(int i = 0; i < N ; i++)
        {
            cin >>g[i].w >> g[i].v;
            g[i].p = (double)g[i].v / (double)g[i].w;
        }
        sort(g,g+N,cmp);
        for(int i = 0; i < N ; i++)
        {
            if(V >= g[i].w)
            {
                V -= g[i].w;
                sum += g[i].v;
            }
            else
            {
                sum += (double)(V  * g[i].p);
                break;
            }
        }
        printf("%.2lf\n",sum);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/kubab119/p/11938722.html