Milking Order Code Debugging

Problem Id: https://usaco.org/index.php?page=viewproblem2&cpid=832#

#include <bits/stdc++.h>
using namespace std;
using ll =long long int;
int main(){
    freopen("milkorder.in", "r", stdin);
    freopen("milkorder.out", "w", stdout);
    int n,m,k;
    cin >> n >> m >> k;
    vector <int> l1(n+1,0);
    vector <int> hierarchy(n+1);
    vector <int> f_table(n+1);
    vector <int> marked = {0};
    int final = -1;
    int current = 0;
    for (int i = 0; i < m; i++){
        int a;
        cin >> a;
        hierarchy[a] += 1;
        f_table[a] = i;
    }
    for (int i = 0; i < k; i++){
        int c,p;
        cin >> c >> p;
        l1[p] = c;
    }
    int zero_count = 0;
    if (hierarchy[1] == 0){
        for (int i = 1; i<= n; i++){
            if (l1[i] == 0){
                zero_count += 1;
            }
            else if(hierarchy[l1[i]] == 1){
                marked.push_back(i);
                current += 1;
                if (zero_count > f_table[l1[i]] - (current - 1)){
                    for (int j = marked[current-1] + 1; j < marked[current]; j++){
                        if (l1[j] == 0){
                            final = j;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        if (final == -1){
            for (int i = marked[marked.size() - 1] + 1; i <= n; i++){
                if (l1[i] == 0){
                    final = i;
                    break;
                }
            }
        }
    }
    else{
        int start = -1;
        int end = -1;
        for (int i = 0; i < n; i++){
            if (hierarchy[l1[i]] == 1){
                current += 1;
                marked.push_back(i);
                if (f_table[l1[i]] > f_table[1]){
                    end = i;
                    start = marked[current-1] - 1;
                }
            }
        }
        if (start <= 0){
            start = 0;
        }
        if (end == -1){
            start = marked[marked.size() - 1];
            end = n + 1;
        }
        for (int i = start + 1; i < end; i++){
            if(l1[i] == 0){
                zero_count += 1;
            }
            if (zero_count >= f_table[1] - f_table[l1[start]]){
                final = i;
                break;
            }
        }
    }
    cout << final << "\n";
}

I’ve tried multiple times but I still only got 6/10, can anyone explain why, I don’t know why, is there anyway to see the test cases

yes, press on “Return to Problem List” on the top right and then look for “Test data”

1 Like