Link to problem: Solution - Circular Barn (USACO Bronze 2016 February)
My code:
#include <bits/stdc++.h>
using namespace std;
int main(){
freopen("cbarn.in","r",stdin);
freopen("cbarn.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
int cowsinroom[n];
int moves[n];
int totalcows=0;
for(int i=0;i<n;i++){
cin>>cowsinroom[i];
totalcows +=cowsinroom[i];
for(int j=0;j<n;j++){
if(j>i){
moves[i]+=(j-i)*cowsinroom[j];
}
else if(i!=j){
moves[i]+=(n-abs(i-j))*cowsinroom[j];
}
}
}
int k=sizeof(moves)/sizeof(moves[0]);
int ans=min(moves, moves+k);
cout<<ans;
cout<<moves;
return 0;
}
So the the code is meant to brute force all of the possible cases for which door is chosen as the entry door and then sum the distances for the number of cows * the distance from their door to the entry door. Then, it saves it in an array “moves”, and finally we find the minimum value of the array moves to find the answer.
When I submit to USACO, I get an error that says "no matching function for call to ‘min(int[n], int*).’ How can I fix this error, and also does this code make sense to get the problem right?