Other than the tips on DMOJ, there are a few other java tips.
For outputting large outputs, you may want to use Java.io's PrintWriter.
import java.io.PrintWriter;
public class printwriterDemo {
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out);
out.println("Hello World!");
out.flush();
}
}
The implementation is similar to using the default System.out
, but requires a flush at the end to output.
More tips to be added...
C++ Tips
ios::sync_with_stdio(0);
cin.tie(0);
<< endl
for line breaks, use "\n"
; it’s fasterint a = 123456789;
long long b = a*a;
cout << b << "\n"; // -1757895751
// Code copied from Competitive Programmer's Handbook by Antti Laaksonen
Instead change int a
to long long
or do:
long long b = (long long)a*a;
typedef
typedef long long ll // Whenever you use ll in the program, it refers to long long
ll a = 5034059340; // Means long long a = 5034059340;
int
to string
and vice versa// int to string
#include<string>
int x = 23984;
string yay = to_string(x);
// string to int
string testing = "2342"; // String should be a number
int yay = stoi(testing); // Convert to int
char
to int
char x = '8'; // a number digit
int y = x - 48; // The value 8 is now stored in y
Source: images are screenshots from the book
Guide to Competitive Programming: Learning and Improving Algorithms Through Contests by Antti Laaksonen
Last in, First out: LIFO (like a stack of plates)
First in, First out FIFO (like an ice cream truck line up)
An iterator points to a value in a data structure. For example, if we have a vector, v
,
Notice that v.end()
points outside the last element in v
Use *v.begin()
to print out the value an iterator points (change code v.begin()
to an iterator name)
There are set
and unordered_set
that have the same operations. Note that all elements in sets are distinct. So inserting the same element again won’t change the set
*Use #include<iterator> #include<set>
or #include<
set
operations are in unordered_set
is based on a hash table, operations are on average timeUse the function find(x)
NOT set_name[]
to access values in a set. Note find(x)
returns an iterator that points to the element x
.
A map is a pair: a key and it’s corresponding value (analogy: a key unlocks a unique door, and a door can represent a value)
You can think an array as a key-value pair. The index of the array corresponds to a unique value. You can use
map
”based an a balanced binary search tree and accessing elements takes time” (section 5.2.2 of Guide to Competitive Programming)unordered_map
”uses hashing and accessing elements takes time on average” (section 5.2.2 of Guide to Competitive Programming)#include<map>
or #include<unordered_map>
Here’s the syntax:
You can also initialize a map like this:
unordered_map<string,int> testing {
{"testing", 5}, {"yellow", 6}, {"Hello", 2}
};
// Note uniform initialization is used here
// Uniform initialization: initialize variables using {value} instead of = value
And check if a key exists using:
if (testing.count("somekey")) {
// The key exists
}
To list out keys and values, you can do this:
for (auto x : testing){
cout << x.first << " " << x.second << "\n";
}
// x.first is the key
// x.second is the corresponding value
// note we use x.first NOT testing.first
Or in C++ 17 onwards, you can use this: (called structure bindings)
for(const auto& [key, value] : testing) {
cout << key << ": " << value << "\n";
}
An arithmetic series is when the difference between any 2 consecutive terms is the same. For example, and are arithmetic series because they have a common difference between terms.
Sum of arithmetic series: or
If you need to find the sum numbers like 1+2+3+4+5…., don’t use a for
loop. Apply the formula or alternatively use the formula
// Don't do this:
int sum{}; int n = 100;
for (int i = 0; i < n; ++i){
sum += i;
}
// Do this, it's faster
int sum{}; int n = 100;
sum = n*(n+1)/2
To get the next term, you multiply by the same number each time. A geometric series is when the ratio between two consecutive terms is the same.
Sum of a geometric series:
A set is a collection of objects (objects can be numbers, strings, etc.). For example, is an example of a set. Note sets are unordered and each object is distinct.