USACO 2019 December Contest, Bronze Problem 1. Cow Gymnastics

link

My code is not working as intended, and after viewing the official solution and modifying my code to match, it still does not work. Any suggestions?

#include <bits/stdc++.h>

using namespace std;

int n,k;
int data[10][20];

int better (int a, int b) {
    int total = 0;

    for(int s = 0; s<k; s++) {
        int apos, bpos;
        for (int i = 0; i< n; i++) {
            if (data[s][i]==a) {apos = i;}
            if (data[s][i]==b) {bpos = i;}
        }
        if (apos<bpos) {
            total++;
        }
        
    }
    return total;
}

int main() {
    freopen("gymnastics.in","r",stdin);
    freopen("gymnastics.out","w",stdout); 
    cin>>n>>k;  
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cin >> data[i][j];  
        }           
    }
    
    int count=0;

    for (int a=1; a<=n; a++) {
        for (int b=1; b<=n; b++) {
            if (better(a,b)==k) {
                count++;
            }
        }
    }
    cout<<count;
    return 0;
}

official solution

1 Like

The input is given as K, N, not N, K. Changing

cin >> n >> k

to

cin >> k >> n

yields AC :slight_smile:

3 Likes