ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

How to write C/C++ solutions

C and C++ programs are compiled on the server with the 32-bit Microsoft Visual C++ 2017 or MinGW GCC 7.1 or Clang 4.0.1. All compilers are provided in C11 and C++14 standards.

Visual C++ 2017 has a version included in Visual Studio 2017 v15.3. It is invoked with the following arguments:

// C
cl /TC /MT /EHsc /GL /O2 /W3 /Za /D "_CRT_SECURE_NO_WARNINGS" 
   /D "_CRT_SECURE_NO_DEPRECATE" /D "ONLINE_JUDGE"

// C++
cl /TP /MT /EHsc /GL /O2 /W3 /Za /std:c++14 /D "_CRT_SECURE_NO_WARNINGS" 
   /D "_CRT_SECURE_NO_DEPRECATE" /D "ONLINE_JUDGE"

You can get the online C/C++ Language and Standard Libraries and download the free Visual Studio Community 2017.

The MinGW GCC 7.1 is invoked with the following arguments:

// C
gcc -static -fno-strict-aliasing -DONLINE_JUDGE -lm -s 
    -std=c11 -Wl,--stack=67108864 -O2 -o %1.exe %1

// C++
g++ -static -fno-strict-aliasing -DONLINE_JUDGE -lm -s 
    -x c++ -std=c++14 -Wl,--stack=67108864 -O2 -o %1.exe %1

Despite -static option the programs are linked against MSVCRT. To improve compatibility with the latest standards MinGW is configured to link programs against libmsvcr120 (Visual C++ 2013 CRT) instead of default libmsvcrt (Visual C++ 6 CRT).

You can get the full MinGW toolchain on the official web page.

The Clang 4.0.1 works on top of the MinGW GCC 7.1 toolchain and is invoked with the following arguments:

// C++
clang++ -static -fno-strict-aliasing -DONLINE_JUDGE -lm -s 
    -x c++ -std=c++14 -Wl,--stack=67108864 -O2 -o %1.exe %1

You can get the online documentation and the pre-built binaries on the LLVM web page.

An example of solving a problem

This is a sample solution for the A + B problem in the C language:

#include <stdio.h>
int main()
{
   int a, b;
   scanf("%d%d", &a, &b);
   printf("%d\n", a + b);
   return 0;
}

And this is a sample solution for the same problem in C++:

#include <iostream>
int main()
{
   int a, b;
   std::cin >> a >> b;
   std::cout << a + b << std::endl;
   return 0;
}

Input/output

An example of how to use input/output is given above. C++ programmers may always choose between two input/output libraries: standard (scanf, printf) or stream (cin, cout) input/output. Stream input/output is easy to use, but it works much slower than standard input/output. This may even cause receiving the Time limit exceeded verdict. Therefore, if you see that a problem requires reading a lot of input data (for example, more than a megabyte) or a large output is expected, then you should better use standard input/output.

Remember that symbol reading functions return negative values for symbols with codes 128-255. If the input data may contain such symbols, then it is better to transform the type of the symbol read to unsigned char explicitly than to get the Wrong answer or Runtime error (access violation) verdicts because of this.

You should know how to read the input data until the end of the stream to solve certain problems. The following examples show how to implement reading input until the end of stream in case input data consists of numbers, lines or separate characters.

#include <stdio.h>
...
// numbers
int n;
while (scanf("%d", &n) != EOF)
{
   ...
}
// lines
char line[1000];
while (gets(line))
{
   ...
}
// characters
int c;
while ((c = getchar()) != EOF)
{
   ...
}
#include <iostream>
...
// numbers
int n;
while (std::cin >> n)
{
   ...
}
// lines
std::string line;
while (std::getline(std::cin, line))
{
   ...
}
// characters
char c;
while (std::cin.get(c))
{
   ...
}

The distinctions of the compiler as compared to other 32-bit C++ compilers

Visual C++ Only. In order not to get a Compilation error, you should know some features of the compiler used on the server.

  • It is impossible to use functions from the header file math.h for integer parameters. For example, this code will not be compiled: sqrt(2). It is correct to write sqrt((double)2) or sqrt(2.0).
  • The header file math.h contains functions j0, j1, jn, y0, y1, and yn. Therefore, you should not declare global variables with such names if you use math.h. The STL header files contains functions next, prev, begin, end, rank, count, hash. These names can not be used for naming global variables and functions.
  • The type long double coincides with double. So, sizeof(long double) = sizeof(double) = 8. When solving problems, it is always sufficient to use the type double (unless a special implementation of floating-point numbers is required).
  • The function itoa is not supported. Use the function sprintf.
  • A variable declared in a for statement is accessible only within the body of this statement.

How to use 64-bit integer data types

The compiler fully supports 64-bit integers (both signed and unsigned). A signed 64-bit integer ranges from –9223372036854775808 to 9223372036854775807 and an unsigned 64-bit integer ranges from 0 to 18446744073709551615. A 64-bit variable can be declared in the following way:

long long a;
unsigned long long b;

64-bit variables can be read and written in two ways as well depending on the input/output library used:

#include <stdio.h>
...
scanf("%lld", &a);
scanf("%llu", &b);
printf("%lld", a);
printf("%llu", b);

#include <iostream>
...
std::cin >> a;
std::cin >> b;
std::cout << a;
std::cout << b;

STL library

The Standard Template Library, or STL, is a C++ library of container classes, algorithms, and iterators; it provides many of the basic algorithms and data structures of computer science, thus helping programmers to shorten their code and to quicken the process of writing a program.

STL containers are ready-made implementations of common data structures. The most useful for solving problems are the following containers:

  • a dynamic array vector;
  • a sorted associative array map and a sorted set set, which are based on red-black trees (search, insert, and delete operations are performed in logarithmic time);
  • queue, priority_queue, deque, and stack.

An iterator in STL is an abstraction that allows traversing elements of a container. From a programmer's point of view, an iterator is any object that supports the operations of dereferencing a pointer to a current element (*, ->) and moving to the next element (++) (the usual pointer is also an iterator). All STL containers provide iterators for traversing their elements; they are available by calling container_type::iterator.

The most popular STL algorithm is the quicksort sort, which works in time O(N × log N). There are altogether around 60 algorithms in the library, including the binary search binary_search, the generation of the next permutation of elements next_permutation, the linear search find, and many others.

Let us consider an example of solving a problem using STL. Given a set of lines, it is required to output all differing lines in the lexicographic order and to specify for each line the number of times it occurs in the set.

#include <iostream>
#include <map>
#include <string>

int main()
{
   std::map<std::string, int> words;
   std::string word;

   while (std::getline(std::cin, word))
      words[word]++;
   for (std::map<std::string, int>::iterator it = words.begin(); it != words.end(); it++)
      std::cout << it->first << " - " << it->second << std::endl;

   return 0;
}

Since operations with the container map are performed in logarithmic time, the complexity of the solution is O(N × log N), where N is the size of the input data.

Other notes

You must not throw exceptions in solutions of problems (even inside a block try … catch)—this will be interpreted by the checker as a Runtime error.

Visual C++ Only. In order to increase the size of a stack and to avoid its overflow when using a “deep” recursion, you should use a special directive (in the example, the size of the stack is set to be 16 MB):

#pragma comment(linker, "/STACK:16777216")

It is convenient to use the conditional directive ONLINE_JUDGE for debugging solutions.

#include <stdio.h>
int main()
{
#ifndef ONLINE_JUDGE
   freopen("input.txt", "rt", stdin);
   freopen("output.txt", "wt", stdout);
#endif
   int a, b;
   scanf("%d%d", &a, &b);
   printf("%d\n", a + b);
   return 0;
}

Note that the function freopen may also be used with stream input/output.

#include <iostream>
int main()
{
#ifndef ONLINE_JUDGE
   freopen("input.txt", "rt", stdin);
   freopen("output.txt", "wt", stdout);
#endif
   int a, b;
   std::cin >> a >> b;
   std::cout << a + b << std::endl;
   return 0;
}

Earlier compilers

  • The Intel C++ Compiler 7.0 was used until August 3, 2010.
  • MinGW GCC 4.7.2 was used until October 3, 2014.
  • Microsoft Visual C++ 2010 was used until Septemper 22, 2015.
  • Microsoft Visual C++ 2013 was used until September 1, 2017.
  • MinGW GCC 4.9.1 was used until September 1, 2017.
  • Clang 3.5 was used until September 1, 2017.