Competitive Programming

Yung-Sheng Lu

FEB 02, 2015

@NCKU-CSIE

Lecture 1

Outline

  • Online Judge (OJ)

  • Competitions

  • About Contest

  • Input and Output (I/O)

  • Practices

Online Judge (OJ)

常見的 Online Judge

Online Judge

國內常見的 Online Judge

Online Judge

其他推薦的 Online Judge

Online Judge

Competitions

國內重要的程式競賽

Competitions

國際重要的程式競賽

Competitions

About Contest

  • 問題限制 (Limits)

    • 時間限制 (Time Limit)

    • 記憶體限制 (Memory Limit)

  • 參考資訊

    • Total Submissions

    • Accepted

  • 問題描述 (Description)

  • 輸入格式 (Input)

  • 輸出格式 (Output)

  • 輸入範例 (Sample Input)

  • 輸出範例 (Sample Output)

Problem Format

Notations

  • Big O Notation - 至多

  • Big θ  Notation - 恰巧

  • Big Ω Notation - 至少

Time Complexity

Common Time Complexities

  • Constant Time

  • Logarithmic Time - 

  • Linear Time - 

  • Quadratic Time - 

  • Quasilinear Time - 

  • Factorial Time - 

Time Complexity

O(1)
O(1)O(1)
O(\log{n})
O(logn)O(\log{n})
O(n)
O(n)O(n)
O(n^2)
O(n2)O(n^2)
O(n\log{n})
O(nlogn)O(n\log{n})
O(n!)
O(n!)O(n!)

Some Examples

Time Complexity

for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
         a[i][j] = i * j;   
    }
}
O(n^2)
O(n2)O(n^2)
/* Maximum n = m */
int two(int n) {
    if (n < 2)
        return 1 << n;
    return two(n - 1) + two(n - 1);
}
O(2 ^ m)
O(2m)O(2 ^ m)

Some Results (Given input size               )


  •  
  •                       近似於 1 sec
O(n) = O(1000)
O(n)=O(1000)O(n) = O(1000)
n = 1000
n=1000n = 1000
O(n ^ 2) = O(1000000)
O(n2)=O(1000000)O(n ^ 2) = O(1000000)
O(n\log{n}) \sim O(9965)
O(nlogn)O(9965)O(n\log{n}) \sim O(9965)
O(1000000)
O(1000000)O(1000000)

Input and Output (I/O)

  • Input functions




     
  • Output functions

Input and Output (I/O)

int scanf(const char *format, ...);
int sscanf(char *s, const char *format, ...);
int getchar(void);
char *gets(char *s);
int printf(const char *format, ...);
int sprintf(char *s, const char *format, ...);
int putchar(int c);
int puts(const char *s);
#include <cstdio>
using namespace std;
  • Input functions





     
  • Output functions

Input and Output (I/O)

#include <iostream>
using namespace std;
// Extract formatted input
istream::operator>>
// Get characters
istream::get
// Get line
istream::getline
// Get character count
istream::gcount
// Read block of data
istream::read
// Insert formatted output
ostream::operator<<
// Put characters
ostream::put
// Write block of data
ostream::write
// Flush output stream buffer
ostream::flush
  • File open

  • File close
     
  • File input

     
  • File output

File Input and Output (File I/O)

FILE *fopen(const char *filename, const char *mode);
int fclose(FILE *stream);
#include <cstdio>
using namespace std;
int fscanf(FILE *stream, const char *format, ...);
int fgetc(FILE *stream);
char *fgets(char *s, int n, FILE *stream);
int fprintf(FILE *stream, const char *format, ...);
int fputs(const char *s, FILE *stream);
int fputs(const char *s, FILE *stream);
/* Sample code */

fPtr = fopen("filename.txt", "r");

if (!fPtr) {
    printf("File open failed.\n");
    exit(1);
}

fscanf(fPtr, "%s%d%s", c1, &c3, c4);

fprintf(fPtr, "%d\n", y);

fclose(fPtr);
  • File open


     
  • File is open or not
     
  • File close
     
  • Swap internals (C++11)

 

File Input and Output (File I/O)

void open (const char* filename, 
           ios_base::openmode mode = ios_base::in | ios_base::out);
void open (const string& filename, 
           ios_base::openmode mode = ios_base::in | ios_base::out);
bool is_open() const;
#include <fstream>
using namespace std;
void close();
void swap (fstream& x);
/* Sample code */

fs.open ("test.txt");

if (fs.is_open())
    fs.close();
else
    cout << "Error opening file";

About C++ STL

 Three well-structured components

  • Containers

    • Containers are used to manage collections of objects of a certain kind. There are several different types of containers like deque, list, vector, map etc.

  • Algorithms

    • Algorithms act on containers. They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of containers.

  • Iterators

    • Iterators are used to step through the elements of collections of objects. These collections may be containers or subsets of containers.

About C++ STL

std::array
 

  • Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.
  • Unlike the other standard containers, arrays have a fixed size and do not manage the allocation of its elements through an allocator: they are an aggregate type encapsulating a fixed-size array of elements.
  • Zero-sized arrays are valid, but they should not be dereferenced (members front, back, and data).

About C++ STL - Containers

template < class T, size_t N > class array;

std::vector

 

  • Vectors are sequence containers representing arrays that can change in size.

  • Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays.

  • But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

​std::vector<bool>

 

About C++ STL - Containers

template < class T, class Alloc = allocator<T> > class vector;
template < class T, class Alloc = allocator<T> > class vector;
template < class Alloc > class vector < bool, Alloc >; 

std::list
 

  • Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions.
     

std::forward_list
 

  • Forward lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence.
  • Forward lists are implemented as singly-linked lists; Singly linked lists can store each of the elements they contain in different and unrelated storage locations.

About C++ STL - Containers

template < class T, class Alloc = allocator<T> > class list;
template < class T, class Alloc = allocator<T> > class forward_list;

std::stack
 

  • Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.
  • Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.
     

About C++ STL - Containers

template < class T, class Container = deque<T> > class stack;

std::queue
 

  • Queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.
     

std::priority_queue
 

  • Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion.

About C++ STL - Containers

template < class T, class Container = deque<T> > class queue;
template < class T, class Container = vector<T>, 
           class Compare = less < typename Container::value_type > > class priority_queue;

std::deque
 

  • Deque (usually pronounced like "deck") is an irregular acronym of double-ended queue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back).

About C++ STL - Containers

template < class T, class Alloc = allocator<T> > class deque;

std::set
 

  • Sets are containers that store unique elements following a specific order.
     

std::multiset
 

  • Multi-sets are containers that store elements following a specific order, and where multiple elements can have equivalent values.

About C++ STL - Containers

template < class T, class Compare = less<T>, class Alloc = allocator<T> > class set;
template < class T, class Compare = less<T>, class Alloc = allocator<T> > class multiset;

std::map

 

 

  • Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.
     

std::multimap

  • Multi-maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order, and where multiple elements can have equivalent keys.

About C++ STL - Containers

template < class Key, class T, 
           class Compare = less<Key>, 
           class Alloc = allocator < pair < const Key, T > > > class map;
template < class Key, class T, 
           class Compare = less<Key>, 
           class Alloc = allocator < pair<const Key, T > > > class multimap;

Practices

Competitive Programming - Lecture 1

By David Lu

Competitive Programming - Lecture 1

Competitive Programming - Lecture 1

  • 1,333