简体   繁体   中英

getting WA when I try to submit mkbudget spoj

I cannot understand/think of a case where my code fails to give correct output. Link to the problem: http://www.spoj.pl/problems/MKBUDGET/

The problem clearly has a DP solution. I am posting my solution below:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<vector <int> > opt;

void compute_opt(vector<int> A,int n,int hire,int fire,int sal,int max_a)
{
    for(int i = A[0]; i <= max_a; i++)  //for num workers in 1st month
        opt[0][i] = i*(hire + sal);

    for(int i = 1; i < n; i++)  //num of months
        for(int j = A[i]; j <= max_a; j++)  //num of workers for ith month >=A[i] and <= max_a
            {
                opt[i][j] = opt[i-1][A[i-1]] + j*sal + (A[i] > A[i-1] ? (A[i]-A[i-1])*hire : (A[i-1] - A[i])*fire);

                for(int k = A[i-1]; k <= max_a; k++)
                    opt[i][j] = min(opt[i][j], opt[i-1][k] + j*sal + (j>k ? (j-k)*hire : (k-j)*fire));
            }   
}

int ans(vector<int> A, int n, int max_a)
{
    int ret = opt[n-1][A[n-1]];
    for(int i = A[n-1]; i <= max_a; i++)
        ret = min (ret, opt[n-1][i]);

    return ret; 
}

int main()
{
    vector<int> A;
    int n, hire, fire, sal,max_a, c = 1;
    while(1)
    {
        cin >> n;
        if(n == 0)
            break;

        A.clear();
        opt.clear();
        max_a = 0;
        cin >> hire >> sal >> fire;
        A.resize(n);
        for(int i = 0; i < n; i++)  
            {cin >> A[i];
             max_a = max(max_a,A[i]);
             }

        opt.resize(n);  
        for(int i = 0; i < n; i++)
            opt[i].resize(max_a + 2);

        compute_opt(A,n,hire,fire,sal,max_a);
        cout << "Case " << c << ", cost = $" << ans(A,n,max_a) << endl;
        c++;
    }
    return 0;
}

I am getting correct answers for the two sample test cases but I get a WA when I submit. Any help ?

OK, so your problem is that you disallow the case where you hire any number of employees between A[i] and A[i - 1]. Maybe it's a good idea to fire some unneeded employees, but not all. That's why you get WA. I modified your code and got it accepted:

void compute_opt(vector<int> A,int n,int hire,int fire,int sal,int max_a)
{
    // Fill all disallowed entries with infinity
    for (int i = 0; i < A[0]; ++i)
        opt[0][i] = 1000000000;
    for(int i = A[0]; i <= max_a; i++)  //for num workers in 1st month
        opt[0][i] = i*(hire + sal);

    for(int i = 1; i < n; i++)
        for(int j = 0; j <= max_a; j++)
            {
                // No need for special case handling,
                //just check all previous numbers of employees
                opt[i][j] = 1000000000;
                if (A[i] > j) continue;
                for(int k = 0; k <= max_a; k++)
                    opt[i][j] = min(opt[i][j],
                       opt[i-1][k] + j*sal + (j>k ? (j-k)*hire : (k-j)*fire));
            }   
}

By the way, there's a "greedier" solution than the one you have that does not depend on the number of employees being small (so that the table can be allocated).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM