Overview
Contest-specific templates tailored for different competitive programming platforms with their unique requirements and conventions.
USACO Template
Template for USA Computing Olympiad with file I/O.
/**
* @created : [Date]
*/
#include <bits/stdc++.h>
using namespace std;
class Task {
private:
void clear() { ; }
void read() { ; }
void solveOne() {
;
}
public:
void solve() {
int T = 1;
cin >> T;
while (T-- > 0) {
clear();
read();
solveOne();
}
}
private:
void print() { cout << '\n'; }
template <class H, class... T> void print(const H &h, const T &...t) {
cout << h;
if (sizeof...(t))
cout << ' ';
print(t...);
}
};
void set_usaco(string filename = "") {
if ((int)filename.size() > 0) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
set_usaco(""); // Set filename without extension
Task solver;
solver.solve();
cout.flush();
return 0;
}
USACO-Specific Features
set_usaco
void set_usaco(string filename)
Sets up file I/O for USACO problems. Pass problem name without extension.
Usage Example:
// For problem "cowjump"
set_usaco("cowjump");
// Reads from cowjump.in, writes to cowjump.out
USACO Tips
File I/O
Always use file I/O (problemname.in/out)
Time Limits
USACO typically has 2-4 second time limits
Memory Limits
Usually 256 MB, be mindful of large arrays
Subtasks
Some problems have subtasks with smaller constraints
Google Code Jam Template
Template for Google Code Jam with case number formatting.
/**
* @author : [Your Name] <[email]>
* @created : [Date]
* @handle : @[Handle]
*/
#include <bits/stdc++.h>
using namespace std;
class Task {
private:
void clear() {}
void read() {}
void solveOne() {
;
}
public:
void solve() {
int T = 1, test_number = 1;
cin >> T;
while (T-- > 0) {
cout << "Case #" << test_number++ << ": ";
clear();
read();
solveOne();
}
}
private:
void print() { cout << '\n'; }
template <class H, class... T> void print(const H &h, const T &...t) {
cout << h;
if (sizeof...(t))
cout << ' ';
print(t...);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Task solver;
solver.solve();
cout.flush();
return 0;
}
Google Code Jam Specific Requirements
Output Format:
Case #1: [answer]
Case #2: [answer]
Case #3: [answer]
Important:
- Each case starts with
Case #X:
- Case numbers start at 1
- Format must be exact (including space after colon)
Code Jam Tips
Output Format
Exact format required: “Case #X: answer”
Interactive Problems
Use cout.flush() after each output
Large Inputs
Download input files for some rounds
Visible Test Sets
Start with visible tests, then hidden
Interactive Problem Template
void query(int x) {
cout << x << endl; // endl flushes automatically
// Or: cout << x << "\n" << flush;
}
int get_response() {
int response;
cin >> response;
return response;
}
void solve() {
// Read parameters
int n;
cin >> n;
// Interactive solving
for(int i = 0; i < n; i++) {
query(i);
int resp = get_response();
// Process response
}
// Submit answer
cout << "answer" << endl;
}
LeetCode Template
Minimal template for LeetCode (mostly header needed).
/**
* @author : [Your Name] <[email]>
* @created : [Date]
* @handle : @[Handle]
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Solution methods here
};
LeetCode-Specific Considerations
No main() function needed:
class Solution {
public:
int solveProblem(vector<int>& nums) {
// Your code here
return answer;
}
};
Testing locally:
int main() {
Solution sol;
vector<int> nums = {1, 2, 3, 4, 5};
int result = sol.solveProblem(nums);
cout << result << endl;
return 0;
}
LeetCode Tips
Class Methods
All solutions must be class methods
No Global Variables
Use class member variables instead
Standard Library
Most standard library included automatically
Reference Parameters
Use & for large containers to avoid copying
Universal template that works across platforms.
#include <bits/stdc++.h>
using namespace std;
// Conditional compilation for different platforms
#ifdef USACO
void setup_io(string name = "") {
if(name.size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
#else
void setup_io(string name = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
#endif
void solve() {
// Your solution here
}
int main() {
setup_io();
int T = 1;
#ifndef USACO
cin >> T;
#endif
for(int tc = 1; tc <= T; tc++) {
#ifdef CODEJAM
cout << "Case #" << tc << ": ";
#endif
solve();
}
return 0;
}
Compile with flags:
# For USACO
g++ -DUSACO solution.cpp -o solution
# For Code Jam
g++ -DCODEJAM solution.cpp -o solution
# For other contests
g++ solution.cpp -o solution
Available Templates
| Template | File | Contest |
|---|
| USACO | template_usaco.cpp | USA Computing Olympiad |
| Google | template_google.cpp | Google Code Jam/Kick Start |
| LeetCode | template_std_leetcode.cpp | LeetCode |
| Standard | template_std.cpp | Codeforces, AtCoder, etc. |
Comparison Table
| Feature | USACO | Code Jam | LeetCode | Codeforces |
|---|
| File I/O | Required | No | No | No |
| Case Numbers | No | Yes | No | No |
| Class-based | Optional | Optional | Required | No |
| Interactive | Rare | Common | No | Common |
| Time Limit | 2-4s | 5-30s | Varies | 1-2s |
Contest-Specific Notes
USACO
- Bronze/Silver: Usually straightforward implementation
- Gold/Platinum: Advanced algorithms required
- Always read from
problemname.in, write to problemname.out
Google Code Jam
- Test Set 1: Smaller constraints, visible
- Test Set 2: Full constraints, hidden
- Interactive problems common in later rounds
- Download large input files for some problems
LeetCode
- Focus on clean, efficient code
- Multiple test cases run automatically
- Consider edge cases carefully
- Time and space complexity shown
See Also
Standard Template
Basic competitive programming template
Debugging Tools
Debugging utilities for contests