mercredi 30 décembre 2020

Rental Service Usaco [closed]

Problem: http://www.usaco.org/index.php?page=viewproblem2&cpid=787

I've been getting only 3 cases right (1,5 and 7) in this problem but I can't find why.

So first of all when looking at this problem I realized that you want to rent the cows that produce less milk and sell first to the ranches that pay more, so after collecting the data I sorted it like this

by money

10 25

15 15

2 10

by money but I assign them the cow that gives the least amount of gallons

250 1

80 2

100 4

40 6

0 7

The commented code prints these

I then traverse the Rent vector from n-1 to 0 and I compare the money we get if a cow is rented vs the one if its milk is sold

In this function, if s which is the variable I assign to the gallons is more than the gallons I'm allowed to sell then I subtract the gallons I am allowed to sell to s I update the total and I go to the next ranch, else I update the total, set the variable Sale2 to the gallons I can sell- s and I set s to 0

if the total is more than what I get from renting I set Sale2 to the gallons the actual ranch buys, update the variable I use to traverse the sale vector, and I return the total, else I return the Rent

And if we are done with the ranches I only return the renting money

Here is my code:

#include<bits/stdc++.h>

using namespace std;

struct trip {
    long long c, m;

    bool operator < (const trip& y) const {
        return m > y.m;
    }

};

long long n, m, r, amount = 0;
vector < long long > v;
vector < pair < long long, long long >> Rent;
vector < long long > p;
vector < trip > Sale;

long long x = 0;
long long RentOrSale(long long act) {
    long long total = 0;
    long long s = Rent[act].second;
    long long i = x;
    long long Sale2 = 0;
    if (i >= m) {
        return Rent[act].first;
    }
    while (s > 0 && i < m) {
        if (s >= Sale[i].c) {
            total += Sale[i].m * Sale[i].c;
            s -= Sale[i].c;
            i++;
        }
        else {
            total += s * Sale[i].m;
            Sale2 = Sale[i].c - s;
            s = 0;
        }
    }
    if (Rent[act].first < total) {

        if (Sale2 != 0) {
            Sale[i].c = Sale2;
        }
        x = i;
        return total;
    }

    return Rent[act].first;
}

int main() {
    ifstream cin("rental.in");
    ofstream cout("rental.out");
    cin >> n >> m >> r;

    for (long long i = 0; i < n; i++) {
        long long a;
        cin >> a;
        v.push_back(a);
        amount += a;
    }

    sort(v.begin(), v.end());

    for (long long i = 0; i < m; i++) {
        long long a, b;

        cin >> a >> b;

        Sale.push_back({
          a,
          b
            });
    }

    sort(begin(Sale), end(Sale));

    for (long long i = 0; i < r; i++) {

        long long a;
        cin >> a;
        p.push_back(a);

    }
    sort(Rent.rbegin(), Rent.rend());

    for (long long i = 0; i < n; i++) {
        if (i < r) {
            Rent.push_back(make_pair(p[i], v[i]));
        }
        else {
            Rent.push_back(make_pair(0, v[i]));
        }
    }

    /*
      for (int i = 0; i < m; i++) {
          cout << Sale[i].c << ' ' << Sale[i].m <<endl;
      }
      cout << endl << endl;

      for (int i = 0; i < n; i++) {
          cout << Rent[i].first <<' '<< Rent[i].second<<endl;
      }


  */
    long long act = n - 1;
    long long money = 0;

    for (act = n - 1; act >= 0; act--) {

        money += RentOrSale(act);
        amount -= Rent[act].second;
        if (amount == 0) {
            break;
        }
    }

    cout << money;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire