Вы находитесь на странице: 1из 37

Algorithms

Afinitesetofinstructionsthatoperateson input(s)andresultsinoutput(s).

Whatdoweusethemfor?

Toteachmachineshowtodostuff Todotheabove,winsomecompetitionsandgetsome shirts!

ProgrammingLanguages?

Whatshouldyouuse?

Java C# Python C C++

DataStructures

Structuresthatareusedtostoredata

Why?

Unstructureddata Sunil,Hettige,102,ReidsAvenue,Cololmbo07,23 Structureddata {firstName,lastName,addressLine1,addressLine2, addressLine3,age}

WhyC++/Python?

C++

Python

Richsetoftools(data structuresand algorithms): STL(StandardTemplate Library) Fast cinandcout

Arbitraryprecision arithmetic ALOTlesscodethan othercompeting languages Easytolearnanduse

Skills?

Problemsolvingskills Someknowledgeofbasicmath Abilitytowritecodethatrunfast

Problemformat

Problemstatement

Whatyouhavetosolve Asamplesolutionthatiscommented,highlightingwhat theywant Showstheformatthattheysupplythedataandtheway theyexpectyouroutputtobe

Problemexample(mightbethereornot)

Sampleinputandoutput

Stuffyoushouldknow
ForlargeNdonotuse(N>1,000,000),

Nestedforloops/whileloops Recursivefunctionsthatcallitselfalargenumberoftimes. Trytousealinearsolution:forNnumberofinputsm*N numberofwork(instructions)willbedone DonotgoforsolutionsthatarebruteforceifNcanget sufficientlylarge,otherwiseitcanbeasolutionbutNOT recommended.

ForsmallerN

Thingstokeepinmind

Donotsubmit(thinkthatthesolutionascorrect)whenyou getthegivenoutputforthegiveninput. Donotcodetargetingtheinputsandoutputsgivenas samples. Thinkbeforeyoucode.

TheCstandarddoesnotdefinealibraryasbigasC++, developmenttimeismuchhigher,runningtimeislower becauseoflowoverhead. C++isadescendantofC,iftherewasn'tCtherewon'tbe C++.Ifyoudon'tknowC,youdon'tknowC++.

if,for,while,do...etcarethesamefromJava. Conceptsaredifferent,

OOPsupportisnotthere,butyoucanwriteOOPprogramswithit. Datatypesincludeprimitivesandstructs

Primitives

int,long,double,float Setsofprimitiveswhereordermatters, structstudent{charname[256];unsignedcharage;};

Structs

SampleCsnippet
structday{ unsignedshortyear; unsignedcharmonth; unsignedcharday; }; structstudent{ charname[256]; structdaybirthday; };

SampleCprogram
#include<stdio.h> intmain(){ inti; printf(Enteranumber\n); scanf(%d,&i); printf(Youentered%d\n,i); return0; }

Pointers
inti=90; int*iptr=&i; Youshouldalwayspointtoanaddressthatbelongstoyour process(orsharedwithyourprocess)andanaddressthat youaresurethatitisinitialized.

Pointersandarrays
Basics

intarr[100]; int*ptr_arr_first=&arr[0]; int*ptr_arr_first_another_way=arr; Aarrayofpointers,


Alittlebitadvanced

Definedlike:int*arr[N] Thedatatype:int(*aptr)[N]

Pointertomultidimentional array
intarr[N][M]; int(*aptr)[M]=arr;

intarr1[A][B][C]; int(*aptr2)[B][C]=arr1; andsoon

Pointerstopointers
char*names[3]={name1,name2,name2}; char**ptr_array_start=names; inty; for(y=0;y<3;y++){ printf("%s\n",*(ptr_array_start++)); }

Functionprototypes
//mainfunctioncanbehere,ifsomainneedsafunction //prototypeforaddtocallit //validprototypes:add(int,int);add(inta,intb); intadd(inta,intb){ returna+b; } //maincanbehere,ifsomaincancalladdwithouta //prototype

Pointerstofunctions
intadd(inta,intb);//prototype int(*add)(inta,intb);//pointertotheaboveprototype

Definitions&Headerfiles

#definekeyword Headerfilesonlyhavethestructureofaprogram,notthe runningparts

CStandardLibrary

ImplementedbytheOSmakers Providesastandardizedinterfacetoworkwithonany architectureoranyOS TheysayCisnotportable:Csourceisportable,binaries arenot!

string.h
Thestandardlibraryheaderfilewiththedefinitionsfor stringbasedfunctions

stdlib.h

Includesstandardfunctions

malloc:allocateonheap alloc:allocateonstack calloc:allocateandinitializeallelementsto0 realloc:resizeallocation atoi,atol,atof:stringtoprimitiveconvertion(toconvert theotherwayusesprintffromstdio.h)

malloc

Allocatesagivennumberofbytesfromthe heap,youshouldusefree(void*ptr)tofreethe memoryyou'veused.

C++

SupportsOOP

Inprogrammingcompetition'sperspectiveit'sbetterto keepOOPaslowasyoucan Presetsofdatastructuresandalgorithmsthatyoucan usejustafterincludingthem(map,vector,setetc) cinandcout,usageof<<and>>withanydatatype withoutcallingaspecialfunction.

StandardTemplateLibrary

Streams

SampleC++program
#include<iostream> intmain(){ inti; std::cout<<Enteranumber\n; std::cin>>i; std::cout<<Youentered<<i<<std::endl; return0; }

Usingstd
#include<iostream> usingnamespacestd; intmain(){ inti; cout<<Enteranumber\n; cin>>i; cout<<Youentered<<i<<endl; return0; }

Usingstd
#include<iostream> usingnamespacestd; intmain(){ inti; cout<<Enteranumber\n; cin>>i; cout<<Youentered<<i<<endl; return0; }

STL

Vector Map Set List Algorithms

Additionalstuff

Inputredirection freopen(PROBLEM""PROBLEM_ID".in","r",stdin); freopen(PROBLEM""PROBLEM_ID".out","w",stdout); HavingTemplatesourcefileswithalltheheadersincluded

SampleproblemI
http://code.google.com/codejam/contest/619102/dashboard#s=p0

SampleProblemII
http://code.google.com/codejam/contest/90101/dashboard#s=p0

SampleProblemIII

Problem15fromIEEExtreme2011

SampleProblemIII

Problem15fromIEEExtreme2011

Вам также может понравиться