Reduce time spent by coding and debugging.
These coding guidelines help you code faster and with less errors during contests.
std::vector<T>std::map<K,V>, std::set<T>std::deque<T> (std::stack<T>, std::queue<T>)vector<int> v(4); // v={0,0,0,0} v.size()==4
v.clear(); // v={} v.size()==0
v.push_back(4); // v={4}, v.size()==1
v.push_back(6); // v={4,6}, v.size()==2
v.push_back(3); // v={4,6,3}, v.size()==3
v.pop_back(); // v={4,6}, v.size()==2Do not use: std::list (more than 3× slower in almost any case).
Key-value mapping, always sorted
map<int, int> m; // m={}, m.size()==0
m[3] = 5; // m={3: 5}, m.size()==1
m[7] = 2 // m={3: 5, 7: 2}, m.size()==2
int x = m[2]; // x=0, m={2: 0, 3: 6, 7: 2}, m.size()==3vector<int> v{3,1,4,1,5};
for (int i : v)
cout << i << '\n';
// prints 3 1 4 1 5
map<int, int> m;
m[3] = 5; m[7] = 2; m[1] = 3;
for (auto& p : m)
cout << p.first << ' ' << p.second << '\n';
// prints 1 3, 3 5, 7 2container.begin(), container.end()*it = xit++ und it += nit == it2vector<int> v{5,2,3};
for (auto it=v.begin(); it!=v.end(); ++it)
std::cout << *it << ' ';
// Output: 5 2 3
auto it=v.begin();
*it = 0; // v={0,2,3}
it += 2; // it points to 3
*it = 9; // v={0,2,9}
*--it = 4; // v={0,2,9}Do useful things with iterators!
std::sortstd::binary_search, std::lower_bound, std::upper_boundstd::uniquestd::reverseAlgorithms work between two iterators: start and end (exclusively).
vector<int> v{5,4,3,2,1};
sort(v.begin(), v.begin()+2); // v={4,5,3,2,1}
sort(v.begin()+2, v.end()); // v={4,5,1,2,3}
sort(v.begin(), v.end()); // v={1,2,3,4,5}
bool func(int lhs, int rhs) {
return tie(lhs&1, lhs >> 1) < tie(rhs&1, rhs >> 1);
}
sort(v.begin(), v.end(), func);
// v = ?-D_GLIBCXX_DEBUGnew/delete#include <iostream>
int n;
cin >> n;
vector<int> v;
for (int i=0; i<n; ++i) {
int x;
cin >> x;
v.push_back(x);
}
cout << n.size() << '\n';Do not slow down your programs:
std::endl!std::endl='\n'+std::flush and std::flush is slow, especially on our grader.Use the I/O streams instead of printf and scanf.
%d but long long)int -> long long)Make it even faster:
// Turn off synchronization between cin/cout and scanf/printf
ios_base::sync_with_stdio(false);
// Disable automatic flush of cout when reading from cin
cin.tie(0);cd <directory>
pwd
ls
g++ -Wall -Wextra -std=c++11 -g3 -ggdb3 -D_GLIBCXX_DEBUG x.cc -o x-Wall -Wextra:Enable warnings and extra warnings
-std=c++11 or -std=c++14:Enable C++11. If you have a more recent gcc, you can also enable C++14.
-g3 -ggdb3:Write debug informations for gdb.
-D_GLIBCXX_DEBUG:Bound-checking for containers and iterators
./prog <sample01.in
for f in *.in
do
echo "-- $f --"
./prog <$f
donefor f in *.in; do echo "-- $f --"; ./prog <$f; done$ gdb prog
(gdb) run <sample01.in
(gdb) btfor (int i=0; i<10; ++i) {
cerr << "i=" << i << " sum=" << sum << '\n';
sum += i;
}#define DEB(x) cerr << x << '\n'
for (int i=0; i<10; ++i) {
DEB("i=" << i << " sum=" << sum);
sum += i;
}#define DEB(x) // cerr << x << '\n'If wrong answer:
Time limit: 1 second = 107 calculation steps.
Use std::assert.
#include <cassert>
void dfs(int n) {
// n must not be visited
assert(!graph[n].visited);
graph[n].visited = true;
for (int nb : graph[n].adj) {
if (!graph[nb].visited)
dfs(nb);
}
}#include <bits/stdc++.h>Read the doc: http://en.cppreference.com