Choosing a Language
Authors: Nathan Wang, Benjamin Qi
What languages you can use for programming contests.
What Languages Does The USACO Support?
The most popular languages that USACO supports are C++17, Java, and Python 3. C is also supported, but it's essentially a strictly inferior version of C++ and doesn't have the built-in data structures that are often used.
What Are The Differences Between C++11 and C++17?
If you're just starting out, you probably won't be using any C++17-specific features, so submitting in either C++11 or C++17 should suffice. For information about the features introduced in C++11, C++14, and C++17, check the links below.
What Are The Differences Between Python 2 and Python 3?
As the link below mentions, there are many differences between Python 2 and 3. Python 3 is newer and an overwhelming majority of USACO contestants use it over Python 2.
What Language Should I Start Out With?
In general, we recommend the following:
- If you already know one or more of these languages, just use the one you are most comfortable with.
- If you don't know any of these languages, you might as well start with C++, as C++ users generally don't need to worry as much about their solutions being a constant factor too slow. Furthermore, most modules currently lack Java and Python support.
Don't overthink choosing a language -- you can always change languages later!
Can I Pass Every Problem in Every Language?
C++ is typically faster than Java, which in turn is typically faster than Python. Although both Python and Java receive two times the C++ time limit in USACO, this is not the case for most other websites (ex. CodeForces, CSES). Even with the extended time limits, Python and Java sometimes have trouble passing.
- It is guaranteed to be able to receive full credit on all Bronze contests with Python, C++, and Java.
- Python lacks a data structure that keeps its keys in sorted order (the equivalent of
std::set
in C++), which is required for some problems in Gold and Platinum. - We don't have any examples of USACO problems that are impossible to pass with Java, though there are instances where the official C++ code is not fast enough to receive full credit if translated into equivalent Java code.
Wormhole Sort
Example -The Java solution presented in the analysis requires over 3s to run (out of a time limit of 4s).
import java.io.*;import java.util.*;public class wormsort {public static void main(String[] args) throws IOException{BufferedReader br = new BufferedReader(new FileReader("wormsort.in"));StringTokenizer st = new StringTokenizer(br.readLine());int n = Integer.parseInt(st.nextToken());int m = Integer.parseInt(st.nextToken());loc = new int[n];component = new int[n];
A comparable C++ solution runs in less than 800ms:
#include<bits/stdc++.h>using namespace std;int n,m;vector<int> loc, lhs, rhs, weight;vector<vector<int>> edges;vector<int> component;void dfs(int curr, int label) {if(component[curr] == label) return;
A comparable Python solution only passes the first five test cases:
import syssys.setrecursionlimit(1000000)sys.stdin = open("wormsort.in","r")sys.stdout = open("wormsort.out","w")n,m = map(int,input().split())loc = [0]*ncomponent = [0]*nedges = [[] for i in range(n)]
It is possible to optimise this approach to pass all testcases. This takes around 3.8s to run.
def main():f = open("wormsort.in","rb")n, m = map(int, f.readline().split())loc = [*map(int, f.readline().split())]edges = [[] for _ in range(n)]weights = []def valid(loc, minW):component = [-1] * nnumcomps = 0
Finally, the approach below uses DSU (a Gold topic), which takes around 1s to run:
# Author: Nicolas Hsufile = open('wormsort.in')N, M = map(int, file.readline().split())P = tuple(map(int, ('0 '+file.readline()).split()))W = [ tuple(map(int, file.readline().split())) for i in range(M) ]W.sort(key=lambda w:-w[2])par = list(range(N+1))def find(u):
What Am I Expected to Know?
You should know how to code in at least one of the languages listed above before continuing onto the Bronze section of this guide. For a more detailed list on what you should know, read the "Expected Knowledge" module.
Module Progress:
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!