Vous êtes sur la page 1sur 24

Motivation

Competitive programming fosters creativity , teamwork, and innovation in building new software programs , and enables student to test their ability to perform under pressure. Our mission is to train students to perform well in various programming contests. It also helps in Interviews for renowned companies like Facebook and Google. Every Tech fest has at least one competition on Competitive Programming.

Common pitfalls

Dont Forget to write int main() { return 0; } else you will get NZEC Error. Your program is ran on input files, so one can output the answer of one test case before proceeding to the other. (no need to store all the input cases) Avoid using cin and cout , use scanf and printf. Dont forget to look at the constraints in input data, this becomes a strong tool sometimes to crack a problem. Example: given an array A of 'n' integers with n <= 10^6, A[i] <= 10^3 and your aim is to sort A. If you miss out on the constraints, you'll surely get trapped.

C and C++

C++ is same as C , file name becomes .cpp instead of .c and compiler becomes g++ instead of gcc

#include<stdio.h> //For printf/scanf #include<iostream> //For exiting Libaries using namespace std; //To be written //Normal C Code with some extra weapons in your hand. These weapons are discussed further.

STL (A powerful C++ library for efficient programming).

Algorithm Library

Add #include<algorithm> at the top to use this library as you would have done for math.h Sorting : Structure Sorting: Permutation : lec1.cpp lec2.cpp lec3.cpp

Use of Permutation: How do you enumerate all subsets of r items taken out of n items. Solution: make an array of size n, put r 1 s in lexicographically smallest configuration and put 0 in rest of the array , then use next_permutation( ).

Map

Map

: lec4.cpp

Be careful in using map from int to int ,if it is possible to use array in this situation ,then use it else use map. Reason : log n time for searching in map and O(1) time for searching using arrays.

A problem on usage of map


Consider a list of numbers with 2 operations:

Insert( x ) adds the specified number 'x' to the end of the list Delete( number ) delete the first occurrence of the number 'x' from list if present. Homogenous List : If it contains at least 2 equal number Heterogenous List: If it contains at least 2 different numbers. Number will be from -10^9 to 10^9

INPUT
Given j operations of form insert n and delete n find the type of list which becomes after every operations. Example: INPUT 5 Insert 1 Insert 2 Insert 2 Delete 1 Delete 3

OUTPUT

neither hetero both homo homo

Solution
Maintaining the list is a challenge but if you have idea about maps , you can do it easily Use maps to store the #occurrences of each number. Maintain two flags : one for total number of different numbers(diff) and the other for total number of different numbers 'x' such that count(x) > 1(total_same). diff and total_same are the variables declared in

Reference code.

Maintaining LIST in INSERT OPERATION: find if n has occurred for the second time i.e.. mymap[n]=2 increment total_sum by 1 If it occurred for the first time use i.e.. mymap[n]=1 increment diff by 1

Maintaining LIST in DELETE OPERATION: Find if n is there or not. If n is not there return. If after deleting n,n is not present in the list i.e. mymap[n]=0 decrement diff by 1 If after deleting n, the number of occurrence of became 1 i.e.. mymap[n]=1 decrement total_sum by 1.

If diff>1 and total_sum>0 BOTH If diff>1 and total_sum=0 HETERO If diff=1 and total_sum>0 HOMO If none of the above NEITHER REFERENCE CODE : homo.cpp

More to Explore in STL


Set: It is used to maintain a sorted array . Vectors: Used when dynamic arrays are required (Mainly used for sparse graphs) Queue and Stack: You can directly use them from STL and you can be free of load for coding it. Explore more about STL from cplusplus.com

Another Problem from SPOJ

You are given 'n' pizzas each has same (1 unit) thickness

radius of i^th pizza is r_i. Pizza is to be distributed to 'f' of your friends. The pizza can be cut at any angle each time and any number of times. After distribution, each friend should be given exactly one piece of exactly one pizza.

We need to find a distribution of pizza such that:


Each friend gets exactly same volume of pizza! Volume of pizza that each friend gets is maximized!

Solution

If it is possible to distribute 'x' volume of pizza to each of the 'f' friends then it is always possible to distribute 'y' volume of pizza to 'f' friends if y < x. To check if we can distribute 'x' volume of pizza to each of the 'f' friends:

i^th pizza can be distributed to d_i = Floor( (pi * r_i^2) / x ) friends

Summation I = 1 to n { d _ I } >= f then we cannot distribute 'x' volume to 'f' friends. Reference code: pie.cpp

Pseudo code

We can Binary search on the range of volumes.


Low = 0, high = MAX_VOL, TOLEANCE = 1e-4 While (High low > TOLERANCE) do

Mid = (low + hi)/ 2 If canDistribute(mid):

Low = mid Hi = mid

Else

End While Return Low (or High)

Modular Arithmetic Important tool in Programming.

Inverse Of a Number in a Multiplicative Field.

Meaning : Find a number x such that a*x mod n = 1 , here x is called the multiplicative inverse of a. Examples : Inverse of 2 with n=7,9 are 4 and 5 . Conditions for existing of Inverse : a and n must be relatively prime i.e gcd (a , n) =1 . Proof : (a*x = m* n + 1 , a*x-m*n =1 thus gcd (a,n) should be 1)

Given that a/b is an whole number find (a/b) mod p . (a/b) mod p = (a*1/b)mod p = (a*b*b_inv /b) mod p = (a *b_inv) mod p

FermatEuler theorem

phi(n) is number of numbers less than n and coprime to n Note : phi(p)=p -1 IMPORTANT : (a ^ (p-1) )mod p =1

Compute (a^b^n) mod p

Given a , b , n and p ,find a^b^n mod p CONSTRAINTS: a , b and n varies from 1 to 1e9 and p is a prime number Non Triviality of the Problem: Impossible to calculate the exact value of a^b^n .

Solution

Find b^n mod (p-1) as a^(p-1) mod k =1 using fermat euler theorom . Let Res= b^n mod (p-1) Answer = a^Res mod p. REFERENCE CODE : abnp.cpp If p is not prime then replace p-1 by phi(p) used in the solution.

FB Round1 Problem on Inverse and Sorting

You are given an array a with N<=10000 different integer numbers and a number, k, where 1<=k <=N . For all possible subsets of a of size k find the sum of their maximal elements modulo 1000000007.

Solution

1000000007 is a standard prime number used in many competitions . Sort the array a. Lets see in how many times the number a[i] appears as the maximum number in some subsets , provided that i>=k. From all numbers less than a[i] , we can choose any k-1 numbers which is C(i-1,k-1) . Answer is summation C(i-1,k1) *a[i] i from k to n-1. Only Challenge is to compute C(i-1,k-1) mod 1000000007 for all i from k to n-1.

INPUT
Given j operations of form insert n and delete n find the type of list which becomes after every operations. Example: INPUT 5 Insert 1 Insert 2 Insert 2 Delete 1 Delete 3

OUTPUT

neither hetero both homo homo

Resources

League of Programers, IITK Oct'12 http://cse.iitk.ac.in/users/aca/lop.html C++ Reference http://cplusplus.com/reference Code chef http://codechef.com SPOJ http://spoj.com Code forces http://codeforces.com

Vous aimerez peut-être aussi