Download presentation

Presentation is loading. Please wait.

Published byCatalina Gedman Modified over 3 years ago

1
Programming Interest Group Tutorial One Introduction: Get Familiar with Your Weapon 1

2
What is ACM? ACM: Association for Computing Machinery http://www.acm.org/ the worlds largest educational and scientific computing society ACM ICPC ACM International Collegiate Programming Contest http://en.wikipedia.org/wiki/ACM_International_Co llegiate_Programming_Contest http://en.wikipedia.org/wiki/ACM_International_Co llegiate_Programming_Contest 2

3
ACM ICPC ICPC is a two-tiered competition among teams of students representing institutions of higher education. Teams compete in Regional Contests, from which top scoring teams advance to the ACM-ICPC World Finals. Each team has three students, sharing one computer, given a number of programming problems Coordination and teamwork are essential 3

4
Online Judge (OJ) When you finish a problem, submit the source code to the online judge You can use C, C++, Java The online judge will compile your source code, and check with unknown number of secret test cases Feedback from the judge Accepted (AC) – congratulations! Presentation Error (PE) – Your program outputs are correct, but are not presented in the specified format. Check for spaces, left/right justification, line feeds, etc. Wrong Answer (WA) – Your program returned an incorrect answer to one or more of the judges secret test cases Compile Error (CE) – The judges compiler cannot compile your source code 4

5
Online Judge (cont.) Runtime Error (RE) – Your program failed during execution due to a segmentation fault, floating point exception, or others. Time Limit Exceeded (TL) – Your program took too much time on at least one of the test cases. Try to improve the efficiency of your solution! Memory Limit Exceeded (ML) – Your program tried to use more memory than the judges settings. 5

6
Available OJs There are many famous online judges Valladolid OJ (http://acm.uva.es/p)http://acm.uva.es/p Ural OJ (http://acm.timus.ru)http://acm.timus.ru Saratov OJ (http://acm.sgu.ru)http://acm.sgu.ru ZJU OJ (http://acm.zju.edu.cn)http://acm.zju.edu.cn ZJUT OJ (http://acm.zjut.edu.cn)http://acm.zjut.edu.cn Official ACM Live Archive (http://cii-judge.baylor.edu/)http://cii-judge.baylor.edu/ Peking University Online Judge (http://acm.pku.edu.cn/JudgeOnline/)http://acm.pku.edu.cn/JudgeOnline/ Programming Challenges (http://www.programming- challenges.com) 6

7
What do we use? Programming Challenges Steven S. Skiena and Miguel Revilla, Programming Challenges, the Programming Contest Training Manual, Springer, 2003. http://www.programming- challenges.com/pg.php?page=index http://www.programming- challenges.com/pg.php?page=index Our main training reference, available from library 7

8
Suggested Books Art of Programming Contest (free online) http://online- judge.uva.es/p/Art_of_Programming_Contest_SE_for_uva.pdf http://online- judge.uva.es/p/Art_of_Programming_Contest_SE_for_uva.pdf Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms, 2nd Edition, The MIT Press, 2001. Robert Sedgewick, Bundle of Algorithms in Java, Third Edition (Parts 1-5), 3rd Edition, Addison-Wesley Professional, 2003. (There is also a C++ version). Donald E. Knuth,The Art of Computer Programming, Volume 1, 2, 3. Bill Gates said that in order to be "a really good programmer" one must read its first volume and solve all of its problems before sending his company a resume. 8

9
Programming Languages Machine Language Or called Machine code, understandable directly by CPU Different CPU has different Instruction Set A program is just a sequence of instructions that are executed by a CPU. E.g.: adding registers 1 and 2 and placing the result in register 6 is encoded as: 000000 00001 00010 00110 00000 100000 9

10
Machine Language Shall we understand the so cold binary code? CPU designer Writing an assembler Reverse-engineering http://www.reverse-engineering.net/ A book: Hacker Disassembling Uncovered by Kris KasperskyKris Kaspersky 10

11
Assembly Language The same as machine language. But the command numbers are replaced by letter sequences, understandable by CS students. Assembler: transform the assembly language into machine code E.g.: movl $1, %eax movl $0, %ebx int $0x80 11

12
Assembly Language Shall we learn assembly language today? Write an Operating System Write a compiler Optimize the performance of a piece of program MMX, SSE, SSE2, etc. Help us to understand how computer works! A free book: Programming from the Ground Up, by Jonathan Bartlett 12

13
High-level Language Allow us to describe the program in a more natural language. A comparison of different programming languages: http://en.wikipedia.org/wiki/Comparison_of_progra mming_languages http://en.wikipedia.org/wiki/Comparison_of_progra mming_languages Compilation vs. Interpretation Interpreters are generally slower to run, but more flexible than compilers. 13

14
C Quoted from wikipedia: The C programming language (often, just "C") is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. Dennis Ritchie K&R C In 1978, Dennis Ritchie and Brian Kernighan published the first edition of The C Programming Language. This book, known to C programmers as "K&R," served for many years as an informal specification of the language. The version of C that it describes is commonly referred to as "K&R C."Dennis RitchieBrian KernighanThe C Programming Language 14

15
C ANSI C and ISO C The first standard for C was published by ANSI. After a long and arduous process, the standard was completed in 1989 and ratified as ANSI X3.159-1989 "Programming Language C." This version of the language is often referred to as ANSI C, or sometimes C89. In 1990, the ANSI C standard (with a few minor modifications) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990. This version is sometimes called C90. Therefore, the terms "C89" and "C90" refer to essentially the same language. ANSI C is now supported by almost all the widely used compilers. 15

16
C C99 After the ANSI standardization process, the C language specification remained relatively static for some time, whereas C++ continued to evolve. A new C standard is finally published as ISO 9899:1999 in 1999. This standard is commonly referred to as "C99." It was adopted as an ANSI standard in March 2000. Some new features: Inline functions Variables can be declared anywhere Variable-length arrays Support for one-line comments beginning with // 16

17
C Books C Programming Language, 2 nd Edition, by Brian W. Kernighan and Dennis M. Ritchie. Expert C Programming: deep C secrets, by Peter van der Linden. The Standard C Library, by P. J. Plauger. 17

18
C++ Stroustrup began work on C with Classes in 1979. In 1983, the name of the language was changed from C with Classes to C++. In 1985, the first edition of The C++ Programming Language was released. As the C++ language evolved, a standard library also evolved with it, which finally leaded to Standard Template Library (STL). After years of work, a joint ANSI-ISO committee standardized C++ in 1998 (ISO/IEC 14882:1998). The 1998 C++ standard consists of two parts: the core language and the C++ standard library; the latter includes most of the Standard Template Library and a slightly modified version of the C standard library. A newer version of C++ standard was published in 2003. 18

19
C++ C++ can be thought of as comprising three parts The low-level language, largely from C Object-Oriented programming: to define our own data types and to organize large-scale programs and systems Standard library: to provide a set of useful data structures and algorithms 19

20
C++ Templates and Generic programming Generic programming involves writing code in a way that is independent of any particular type. Templates are the foundation of generic programming. int compare(const string &v1, const string &v2) { if (v1 < v2) return -1; if(v2 < v1) return 1; return 0; } int compare(const double &v1, const double &v2) { if (v1 < v2) return -1; if(v2 < v1) return 1; return 0; } 20

21
C++ The following is a template version of compare: Template int compare(const T &v1, const T &v2) { if (v1 < v2) return -1; if(v2 < v1) return 1; return 0; } 21

22
C++ Books C++ Primer C++ Primer Plus Thinking in C++ The C++ Programming Language, by Bjarne Stroustrup The C++ Standard Library: A Tutorial and Reference, by Nicolai M. Josuttis 22

23
Java Java was started as a project called "Oak" by James Gosling in June 1991. The first public implementation was Java 1.0 in 1995. It made the promise of "Write Once, Run Anywhere" (WORA), with free runtimes on popular platforms. Java remains a proprietary de facto standard that is controlled through the Java Community Process (http://www.jcp.org/en/home/index). 23

24
Java Object-Oriented programming Platform independence It means that programs written in the Java language must run similarly on diverse hardware. One should be able to write a program once and run it anywhere. Java language code Java bytecode Jave bytecode is then run on a virtual machine (VM), a program written in native code on the host hardware that interprets and executes generic Java bytecode. To improve the performance, Just-in-time (JIT) compilation has been introduced, by translating bytecode into native machine code at runtime. 24

25
Java Libraries Core libraries: Data structures XML parsing libraries Security Internationalization and localization Integration libraries: The Java Database Connectivity (JDBC) API for database access Java Naming and Directory Interface (JNDI) for lookup and discovery RMI and CORBA for distributed application development User interface libraries: AWT (Abstract Windowing Toolkit ) Swing APIs for audio capture, processing, and playback Lots of extensions: Java EE (J2EE), Java ME (J2ME) JMF (Java Media Framework) JAI (Java Advanced Imaging) Java 3D, JOGL (Java OpenGL) 25

26
Others Python Rumor: advocated by Google "Python has been an important part of Google since the beginning, and remains so as the system grows and evolved. Today dozens of Google engineers use Python, and we're looking for more people with skills in this language" -- Peter Norvig, Director of Search Quality at Google I am the author of the Python programming language. …In December 2005 I joined Google in Mountain View, CA. -- Guido van Rossum, Creator of Python C# Developed by Microsoft (to combat Java) 26

27
Standard Input/Output In ACM contest, each program must read the test data from the standard input and print the results to the standard output For C language, use scanf() and printf() For C++, use cin and cout scanf() and printf() are also supported For Java, refer to http://www.programming- challenges.com/pg.php?page=javainfohttp://www.programming- challenges.com/pg.php?page=javainfo Programs are not allowed to open files or to execute certain system calls 27

28
Not convenient for debugging? #include int main () { freopen(FILE_NAME_FOR_INPUT,r,stdin); freopen(FILE_NAME_FOR OUTPUT,w,stdout); Rest of the codes… return 0; } While sending your code to online judges, remember to remove the two lines with freopen. 28

29
Simple Coding Avoid the usage of the ++ or -- operators inside expressions or function calls Avoid expressions of the form *p++ Avoid pointer arithmetic. Instead of (p+5) use p[5]. Never code like : return (x*y)+Func(t)/(1-s); but like : temp = func(t); RetVal = (x*y) + temp/(1-s); return RetVal; 29

30
Naming Dont use small and similar names for your variables. Use descriptive names. Dont use names like {i,j,k} for loop control variables. Use {I,K,M}. It is very easy to mistake a j for an i when you read code or copy, paste & change code, 30

31
Internal force Data Structures Commonly seen in almost every programming projects A treasure from tens of years of experiences Summarized by computer scientists as textbooks A core subject for every computer science/computer engineering department 31

32
Internal force Algorithms To solve real problems efficiently Categories: Sorting Searching Graph algorithms Scientific computing: matrix, number-theoretic, computational geometry, etc. … 32

33
Internal force Mathematics Everything finally goes back to mathematics! Number theory Geometry Combinatorics Graph theory … 33

34
A Good Team three factors crucial for being a good programming team Knowledge of standard algorithms and the ability to find an appropriate algorithm for every problem in the set; Ability to code an algorithm into a working program; Having a strategy of cooperation with your teammates 34

35
Quickly identify problem types In programming contests, you will be dealing with a set of problems, not only one problem. Problem types Mathematics (number theory, big integer, etc) Sorting Searching Simulation String processing Dynamic programming (DP) Graph Computational geometry Ad Hoc (i.e., no standard categories) 35

36
Analyze your algorithm Proof of algorithm correctness Time/Space complexity analysis for non recursive algorithms For recursive algorithms, the knowledge of computing recurrence relations and analyze them: iterative method, substitution method, recursion tree method and finally, Master Theorem Given the maximum input bound (usually given in problem description), can my algorithm, with the complexity that I can compute, pass the time limit given in the programming contest? 36

37
Some rules of thumb Biggest built in data structure "long long" is 2^63-1: 9*10^18 (up to 18 digits) If you have k nested loops running about n iterations each, the program has O(n k ) complexity The best times for sorting n elements are O(nlog n) DP algorithms which involves filling in a matrix usually in O(n^3) In contest, most of the time O(n log n) algorithms will be sufficient 37

38
Testing your code You wont get any credit by partially solving the problem. You need to be able to design good test cases Sample input-output given in problem description is by default too trivial to measure your code's correctness Before submission, you may want to design some tricky test cases first, test them in your own machine, and ensure your code is able to solve them correctly 38

39
Guidelines in designing good test cases Must include sample input, the most trivial one Must include boundary cases, what is the maximum n,x,y, or other input variables, try varying their values to test for out of bound errors For multiple input test case, try using two identical test case consecutively. Both must output the same result. This is to check whether you forgot to initialize some variables Increase the size of input. Sometimes your program works for small input size, but behave wrongly when input size increases. Tricky test cases, analyze the problem description and identify parts that are tricky, test them to your code. Dont assume input will always nicely formatted if the problem description didnt say so. Try inserting white spaces (space, tabs) in your input, check whether your code is able to read in the values correctly Finally, do random test cases, try random input and check your code's correctness 39

40
Producing Winning Solution Write down a game plan for what you're going to do in a contest round Read through all the problems first, don't directly attempt one problem since you may missed easier problem. Order the problems: shortest job first, in terms of your effort Sketch the algorithms, complexity, the numbers, data structures, tricky details. Brainstorm other possible algorithms Do the Math! (space & time complexity) Code it of course, as fast as possible, and it must be correct Try to break the algorithm - use special (degenerate?) test cases. 40

41
Coding a problem Only coding after you finalize your algorithm. Create test data for tricky cases. Code the input routine and test it (write extra output routines to show data). Code the output routine and test it. Write data structures needed. Stepwise refinement: write comments outlining the program logic. Fill in code and debug one section at a time. Get it working & verify correctness (use trivial test cases). Try to break the code - use special cases for code correctness. 41

42
Tips & tricks for contests Brute force when you can, Brute force algorithm tends to be the easiest to implement. KISS: Simple is smart! (Keep It Simple, Stupid !!! / Keep It Short & Simple). Hint: focus on limits (specified in problem statement). Waste memory when it makes your life easier (trade memory space for speed). Don't delete your extra debugging output, comment it out. Optimize progressively, and only as much as needed. Keep all working versions! 42

43
Tips & tricks for contests (Cont.) Code to debug: a. white space is good, b. use meaningful variable names, c. don't reuse variables, (we are not doing software engineering here) d. stepwise refinement, e. Comment before code. Avoid pointers if you can. Avoid dynamic memory like the plague: statically allocate everything. Try not to use floating point; if you have to, put tolerances in everywhere (never test equality) 43

44
Some Google interview questions Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7. Find the intersection of 2 sorted integer arrays. What if one of them is huge? What if one of them is so huge, it can't fit in memory. How do you minimize the number of disk seeks? Given a string A, how do you find all the repeated substrings with minimum size of 2? Given N computers networked together, with each computer storing N integers, describe a procedure for finding the median of all of the numbers. Assume that a computer can only hold O(N) integers (i.e. no computer can store all N^2 integers). Also assume that there exists a computer on the network without integers, that we can use to interface with the computers storing the integers. 44

45
Example: A+B Problem http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem Code=1001 Time Limit:1 second Memory Limit:32768 KB Description: Calculate a + b Input: The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. Output: For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. Sample Input: 1 5 Sample Output: 6 45

46
Example Solution /* C code */ #include stdio.h int main() { int a, b; while (scanf(%d %d, &a, &b) != EOF) { printf(%d\n, a+b); } return 0; } /* Java code */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int a = in.nextInt(); int b = in.nextInt(); System.out.println(a + b); } 46

47
Fundamental Problems http://acm.zjut.edu.cn 1167 1166 1174 1175 1176 1177 1178 1179 1181 1185 1190 1191 1187 1204 1208 1205 1044 47

48
Fundamental Problems http://acm.uva.es http://acm.uva.es/p/v1/100.html http://acm.uva.es/p/v101/10189.html http://acm.uva.es/p/v101/10137.html http://acm.uva.es/p/v7/706.html http://acm.uva.es/p/v102/10267.html http://acm.uva.es/p/v100/10033.html http://acm.uva.es/p/v101/10196.html http://acm.uva.es/p/v101/10142.html 48

49
3n+1 Problem The 3n+1 problem http://acm.uva.es/p/v1/100.html This is an outstanding unsolved problems in number theory, called 3n+1 conjecture. Start with an integer n; If n is even, n=n/2; else n = 3n+1; Repeat this process, terminating when n=1. Its conjectured that this algorithm will terminate at n=1 for every integer n. E.g.: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 The cycle length for 22 is 16. 49

50
3n+1 problem Write a function to calculate the cycle length for integer i int cycle_length(int i) { int length = 1; while (i != 1) { if( i % 2 == 0) i = i>>1; else i = i<<1 + i + 1; length++; } return length; } Write a function to calculate the maximum cycle for integers between and including i and j void solve(int i, int j) { int t, m, n; int length, maxlength; if( i <= j) { m = i; n = j; } else { m = j; n = i; } maxlength = 1; for(t = m; t <= n; t++) { length = cycle_length(t); if (length > maxlength) maxlength = length; } printf("%d %d %d\n", i, j, maxlength); } 50

51
The trip http://acm.uva.es/p/v101/10137.html A number of students are members of a club that travels annually to exotic locations. The group agrees in advance to share expenses equally, but it is not practical to have them share every expense as it occurs. So individuals in the group pay for particular things, like meals, hotels, taxi rides, plane tickets, etc. After the trip, each student's expenses are tallied and money is exchanged so that the net cost to each is the same, to within one cent. In the past, this money exchange has been tedious and time consuming. Your job is to compute, from a list of expenses, the minimum amount of money that must change hands in order to equalize (within a cent) all the students' costs. 51

52
The trip (Cont.) The Input Standard input will contain the information for several trips. There are no more than 1000 students and no student spent more than $10,000.00. A single line containing 0 follows the information for the last trip. The Output For each trip, output a line stating the total amount of money, in dollars and cents, that must be exchanged to equalize the students' costs. Sample Input 3 10.00 20.00 30.00 0 Output: $10.00 Sample Input 4 15.00 15.01 3.00 3.01 0 Output: $11.99 52

53
The trip (Cont.) Its important to analyze the problem first. After you find the optimal way which can achieve the minimum amount of money that must change hands, writing the program will be very easy. Whats the difficulty of this problem? We need to equalize the costs as much as possible. That means: if A pays the most, and A pays P A ; if B pays the least, and B pays P B, We need to have P B <= P A <= P B +1 53

54
The trip (Cont.) Assume there are n students. We can calculate the total money. If the total money is a multiple of n, then we can equalize the costs. The problem is easy to solve. Example 1: 10.00, 20.00, 30.00 Total = 60.00, average = 20.00 The first student receives 10.00 from the third student The second student doesnt need to do anything. The answer will be $10.00 54

55
The trip (Cont.) If the total money is not a multiple of n Let start from an example: 1.10, 2.10, 3.10, 10.00 Total = 16.30 = 4x4.07 + 0.02 The final cost should be 4.07, 4.07, 4.08, 4.08 To minimize the amount of money change: 1.10 4.07; 2.10 4.07; 3.10 4.08; 10.00 4.08 The answer will be $5.92 Question: do we need to sort the cost? 55

Similar presentations

OK

Getting familiar with ACM Contest. What is ACM ACM: Association for Computing Machinery o o the world’s largest.

Getting familiar with ACM Contest. What is ACM ACM: Association for Computing Machinery o o the world’s largest.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on save mother earth in french Ppt on leverages thesaurus Ppt on coalition government in israel Ppt online downloader from youtube Ppt on current account deficit united Ppt on e movie ticket booking system Seven segment display ppt online Ppt on asian continent videos Ppt on barack obama leadership skills Ppt on holographic technology software