# Allow repeats in solution to USACO Silver Field Reduction

My solution to USACO Silver Field Reduction (USACO) was failing on testcases 4 and 10.

But once I read the after-contest analysis and changed my code to allow repeating cows to be skipped, it passed 10/10 testcases.

My question is that why is it written in the solution we need to allow repeating cows to be in our excluded list. Can someone give an example testcase which supports the above point.

My code:

``````#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

class Cow {
public:
int x;
int y;

bool operator!=(const Cow& a) {
return this->x!=a.x && this->y!=a.y;
}
};

bool cowXComparator(Cow A, Cow B) {
return A.x > B.x;
}

bool cowYComparator(Cow A, Cow B) {
return A.y > B.y;
}

int getArea(vector<Cow> cows) {
int Xlow = 40000+1, Xhigh = 0, Ylow = 40000+1, Yhigh = 0;
for(int i = 0; i < cows.size(); i++) {
if (cows[i].x < Xlow) {
Xlow = cows[i].x;
}
if (cows[i].x > Xhigh) {
Xhigh = cows[i].x;
}
if (cows[i].y < Ylow) {
Ylow = cows[i].y;
}
if (cows[i].y > Yhigh) {
Yhigh = cows[i].y;
}
}
return (Xhigh-Xlow) * (Yhigh-Ylow);
}

int findCow(vector<Cow> cows, Cow cow) {
for(int i = 0; i < cows.size(); i++) {
if(cows[i].x==cow.x && cows[i].y==cow.y) {
return i;
}
}
return -1;
}

int main() {
freopen("reduce.in", "r", stdin);
freopen("reduce.out", "w", stdout);

int n;
cin >> n;
vector<Cow> cows(n);
for(int i = 0; i < n; i++) {
cin >> cows[i].x >> cows[i].y;
}
vector<Cow> topCows;
sort(cows.begin(), cows.end(), cowXComparator);
int numTake = 4;
for(int i = 0; i < cows.size(); i++) {
if(topCows.size()==numTake) {
break;
}
topCows.push_back(cows[i]);
}
for(int i = n-1; i >= 0; i--) {
if(topCows.size()==2*numTake) {
break;
}
topCows.push_back(cows[i]);
}
sort(cows.begin(), cows.end(), cowYComparator);
for(int i = 0; i < cows.size(); i++) {
if(topCows.size()==3*numTake) {
break;
}
topCows.push_back(cows[i]);
}
for(int i = n-1; i >= 0; i--) {
if(topCows.size()==4*numTake) {
break;
}
topCows.push_back(cows[i]);
}
int res = -1;
for(int i = 0; i < topCows.size(); i++) {
for(int j = i+1; j < topCows.size(); j++) {
for(int k = j+1; k < topCows.size(); k++) {
// if(topCows[i]!=topCows[j] && topCows[j]!=topCows[k] && topCows[i]!=topCows[k]) {
// CHANGED: to allow there to be repeats in the excluded cows list
vector<Cow> tmp(cows);
tmp.erase(tmp.begin() + findCow(tmp, topCows[i]));
if(findCow(tmp, topCows[j])!=-1) {
tmp.erase(tmp.begin() + findCow(tmp, topCows[j]));
}
if(findCow(tmp, topCows[k])!=-1) {
tmp.erase(tmp.begin() + findCow(tmp, topCows[k]));
}
int area = getArea(tmp);
if (res == -1 || area < res) {
res = area;
}
// }
}
}
}
cout << res << endl;
return 0;
}
``````