USACO Silver 2020 December - Rectangular Pasture

Author: Kevin Sheng

Official Analysis (C++)

Video Solution


Note: the algorithm in the code may differ from that described in the video.


#include <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
int main() {


import java.util.*;
public final class RPasture {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(;
int cowNum = Integer.parseInt(read.readLine());
HashSet<Integer> seenX = new HashSet<>();
HashSet<Integer> seenY = new HashSet<>();

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!