Antti Laaksonen Learning and Improving Algorithms

Total Page:16

File Type:pdf, Size:1020Kb

Antti Laaksonen Learning and Improving Algorithms Undergraduate Topics in Computer Science Antti Laaksonen Guide to Competitive Programming Learning and Improving Algorithms Through Contests Second Edition Undergraduate Topics in Computer Science Series Editor Ian Mackie, University of Sussex, Brighton, UK Advisory Editors Samson Abramsky , Department of Computer Science, University of Oxford, Oxford, UK Chris Hankin , Department of Computing, Imperial College London, London, UK Mike Hinchey , Lero – The Irish Software Research Centre, University of Limerick, Limerick, Ireland Dexter C. Kozen, Department of Computer Science, Cornell University, Ithaca, NY, USA Andrew Pitts , Department of Computer Science and Technology, University of Cambridge, Cambridge, UK Hanne Riis Nielson , Department of Applied Mathematics and Computer Science, Technical University of Denmark, Kongens Lyngby, Denmark Steven S. Skiena, Department of Computer Science, Stony Brook University, Stony Brook, NY, USA Iain Stewart , Department of Computer Science, Durham University, Durham, UK ‘Undergraduate Topics in Computer Science’ (UTiCS) delivers high-quality instructional content for undergraduates studying in all areas of computing and information science. From core foundational and theoretical material to final-year topics and applications, UTiCS books take a fresh, concise, and modern approach and are ideal for self-study or for a one- or two-semester course. The texts are all authored by established experts in their fields, reviewed by an international advisory board, and contain numerous examples and problems, many of which include fully worked solutions. The UTiCS concept relies on high-quality, concise books in softback format, and generally a maximum of 275–300 pages. For undergraduate textbooks that are likely to be longer, more expository, Springer continues to offer the highly regarded Texts in Computer Science series, to which we refer potential authors. More information about this series at http://www.springer.com/series/7592 Antti Laaksonen Guide to Competitive Programming Learning and Improving Algorithms Through Contests Second Edition 123 Antti Laaksonen Department of Computer Science University of Helsinki Helsinki, Finland ISSN 1863-7310 ISSN 2197-1781 (electronic) Undergraduate Topics in Computer Science ISBN 978-3-030-39356-4 ISBN 978-3-030-39357-1 (eBook) https://doi.org/10.1007/978-3-030-39357-1 1st edition: © Springer International Publishing AG, part of Springer Nature 2017 2nd edition: © Springer Nature Switzerland AG 2020, corrected publication 2020 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This Springer imprint is published by the registered company Springer Nature Switzerland AG The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland Preface to the Second Edition This second edition of the book contains several new sections that discuss advanced topics, such as calculating the Fourier transform, finding minimum cost flows in graphs, and using automata in string problems. I am grateful to Olli Matilainen for reading through most of the new material and giving many useful comments and suggestions. Helsinki, Finland Antti Laaksonen February 2020 v Preface to the First Edition The purpose of this book is to give you a comprehensive introduction to modern competitive programming. It is assumed that you already know the basics of pro- gramming, but previous background in algorithm design or programming contests is not necessary. Since the book covers a wide range of topics of various difficulty, it suits both for beginners and more experienced readers. Programming contests already have a quite long history. The International Collegiate Programming Contest for university students started during the 1970s, and the first International Olympiad in Informatics for secondary school students was organized in 1989. Both competitions are now established events with a large number of participants from all around the world. Today, competitive programming is more popular than ever. The Internet has played a significant role in this progress. There is now an active online community of competitive programmers, and many contests are organized every week. At the same time, the difficulty of contests is increasing. Techniques that only the very best participants mastered some years ago are now standard tools known by a large number of people. Competitive programming has its roots in the scientific study of algorithms. However, while a computer scientist writes a proof to show that their algorithm works, a competitive programmer implements their algorithm and submits it to a contest system. Then, the algorithm is tested using a set of test cases, and if it passes all of them, it is accepted. This is an essential element in competitive programming, because it provides a way to automatically get strong evidence that an algorithm works. In fact, competitive programming has proved to be an excellent way to learn algorithms, because it encourages to design algorithms that really work, instead of sketching ideas that may work or not. Another benefit of competitive programming is that contest problems require thinking. In particular, there are no spoilers in problem statements. This is actually a severe problem in many algorithms courses. You are given a nice problem to solve, but then the last sentence says, for example: “Hint: modify Dijkstra’s algorithm to solve the problem.” After reading this, there is not much thinking needed, because you already know how to solve the problem. This never happens in competitive programming. Instead, you have a full set of tools available, and you have to figure out yourself which of them to use. vii viii Preface to the First Edition Solving competitive programming problems also improves one’s programming and debugging skills. Typically, a solution is awarded points only if it correctly solves all test cases, so a successful competitive programmer has to be able to implement programs that do not have bugs. This is a valuable skill in software engineering, and it is not a coincidence that IT companies are interested in people who have background in competitive programming. It takes a long time to become a good competitive programmer, but it is also an opportunity to learn a lot. You can be sure that you will get a good general understanding of algorithms if you spend time reading the book, solving problems and taking part in contests. If you have any feedback, I would like to hear it! You can always send me a message to [email protected].fi. I am very grateful to a large number of people who have sent me feedback on draft versions of this book. This feedback has greatly improved the quality of the book. I especially thank Mikko Ervasti, Janne Junnila, Janne Kokkala, Tuukka Korhonen, Patric Östergård, and Roope Salmi for giving detailed feedback on the manuscript. I also thank Simon Rees and Wayne Wheeler for excellent collabo- ration when publishing this book with Springer. Helsinki, Finland Antti Laaksonen October 2017 Contents 1 Introduction .......................................... 1 1.1 What Is Competitive Programming? ..................... 1 1.1.1 Programming Contests ........................ 2 1.1.2 Tips for Practicing ........................... 3 1.2 About This Book .................................. 3 1.3 CSES Problem Set ................................. 5 1.4 Other Resources ................................... 7 2 Programming Techniques ................................ 9 2.1 Language Features ................................. 9 2.1.1 Input and Output ............................ 10 2.1.2 Working with Numbers ....................... 12 2.1.3 Shortening Code ............................ 14 2.2 Recursive Algorithms ............................... 15 2.2.1 Generating Subsets ........................... 16 2.2.2 Generating Permutations ....................... 17 2.2.3 Backtracking ............................... 18 2.3 Bit Manipulation ................................... 20 2.3.1 Bit Operations .............................. 21 2.3.2 Representing Sets ............................ 23 3Efficiency ............................................. 27 3.1 Time Complexity .................................. 27 3.1.1 Calculation Rules ............................ 28 3.1.2 Common Time Complexities .................... 30 3.1.3 Estimating Efficiency ......................... 31 3.1.4 Formal Definitions
Recommended publications
  • ICT and Innovation a Step Forward to a Global Society PROCEEDINGS of the 13TH ANNUAL CONFERENCE of ITAIS
    ICT and Innovation A Step Forward to a Global Society PROCEEDINGS OF THE 13TH ANNUAL CONFERENCE OF ITAIS Edited by Alessandro Zardini Francesco Virili Stefano Za © 2017 LUISS University Press - Pola Srl 978-88-6856-102-4 LUISS University Press – Pola s.r.l. Viale Pola, 12 00198 Roma Tel. 06 85225485 www.luissuniversitypress.it e-mail [email protected] Questo libro è stato stampato presso Prontostampa Via Praga 1, 24040 Verdellino Zingonia (BG) Prima edizione ottobre 2017 Table of Contents The Italian Association on Information Systems and this conference Alessandro Zardini, Francesco Virili and Stefano Za…………………………………………… “ 5 Implementing Enterprise System in Large State-owned utilities: A case study Zafar Alvi and Misbah Mehboob Awan ………………… “ 7 Pharmacist Resistance to PBM System in a Developing Country Mary Ann Barbour El Rassi and Nancy Souaiby………… “ 19 Domestic hospitality: an IT based approach Matteo Bassoli and Lucia Oggioni…………………… “ 29 What Are You Talking About? Investigating Online Information Disclosure in Italian IRCCSs Paola Briganti, Luisa Varriale, Rocco Agrifoglio, Mauro Romanelli, and Maria Ferrara…………………… “ 43 Towards a Smart Town Centre: an Integrated Approach of Real and Digital Worlds Federica Caboni, Angela Dettori, Ernestina Giudici and GianRaffaele Loddo…………………………… “ 61 The role of the mobile application in the public transport systems Celio Alberto……………………………………… “ 73 Are social media an opportunity for women entrepreneurs? A literature review Francesca Maria, Paola Demartini and Paola Paoloni………………………………….
    [Show full text]
  • A Survey on Online Judge Systems and Their Applications
    1 A Survey on Online Judge Systems and Their Applications SZYMON WASIK∗†, Poznan University of Technology and Polish Academy of Sciences MACIEJ ANTCZAK, Poznan University of Technology JAN BADURA, Poznan University of Technology ARTUR LASKOWSKI, Poznan University of Technology TOMASZ STERNAL, Poznan University of Technology Online judges are systems designed for the reliable evaluation of algorithm source code submitted by users, which is next compiled and tested in a homogeneous environment. Online judges are becoming popular in various applications. Thus, we would like to review the state of the art for these systems. We classify them according to their principal objectives into systems supporting organization of competitive programming contests, enhancing education and recruitment processes, facilitating the solving of data mining challenges, online compilers and development platforms integrated as components of other custom systems. Moreover, we introduce a formal definition of an online judge system and summarize the common evaluation methodology supported by such systems. Finally, we briefly discuss an Optil.io platform as an example of an online judge system, which has been proposed for the solving of complex optimization problems. We also analyze the competition results conducted using this platform. The competition proved that online judge systems, strengthened by crowdsourcing concepts, can be successfully applied to accurately and efficiently solve complex industrial- and science-driven challenges. CCS Concepts: •General and reference ! Evaluation; •Mathematics of computing ! Combinatorics; Discrete optimization; •Theory of computation ! Design and analysis of algorithms; •Networks ! Cloud computing; Additional Key Words and Phrases: online judge, crowdsourcing, evaluation as a service, challenge, contest ACM Reference format: Szymon Wasik, Maciej Antczak, Jan Badura, Artur Laskowski, and Tomasz Sternal.
    [Show full text]
  • Competitive Programmer's Handbook
    Competitive Programmer’s Handbook Antti Laaksonen Draft July 3, 2018 ii Contents Preface ix I Basic techniques 1 1 Introduction 3 1.1 Programming languages . .3 1.2 Input and output . .4 1.3 Working with numbers . .6 1.4 Shortening code . .8 1.5 Mathematics . 10 1.6 Contests and resources . 15 2 Time complexity 17 2.1 Calculation rules . 17 2.2 Complexity classes . 20 2.3 Estimating efficiency . 21 2.4 Maximum subarray sum . 21 3 Sorting 25 3.1 Sorting theory . 25 3.2 Sorting in C++ . 29 3.3 Binary search . 31 4 Data structures 35 4.1 Dynamic arrays . 35 4.2 Set structures . 37 4.3 Map structures . 38 4.4 Iterators and ranges . 39 4.5 Other structures . 41 4.6 Comparison to sorting . 44 5 Complete search 47 5.1 Generating subsets . 47 5.2 Generating permutations . 49 5.3 Backtracking . 50 5.4 Pruning the search . 51 5.5 Meet in the middle . 54 iii 6 Greedy algorithms 57 6.1 Coin problem . 57 6.2 Scheduling . 58 6.3 Tasks and deadlines . 60 6.4 Minimizing sums . 61 6.5 Data compression . 62 7 Dynamic programming 65 7.1 Coin problem . 65 7.2 Longest increasing subsequence . 70 7.3 Paths in a grid . 71 7.4 Knapsack problems . 72 7.5 Edit distance . 74 7.6 Counting tilings . 75 8 Amortized analysis 77 8.1 Two pointers method . 77 8.2 Nearest smaller elements . 79 8.3 Sliding window minimum . 81 9 Range queries 83 9.1 Static array queries .
    [Show full text]
  • Competitive Coding Website
    Published by : International Journal of Engineering Research & Technology (IJERT) http://www.ijert.org ISSN: 2278-0181 Vol. 10 Issue 04, April-2021 Competitive Coding Website Parth Dave, Rushikesh Deshmukh, Dipak Kumar Gautam UG Student, Dept. of Information Technology Vidyavardhini’s College of Engineering and Technology, Vasai, India Abstract— Owing to the quick development of the cutting edge data society, it is important to instruct youngsters who are liable for the fate of the Information Technology (IT). Problem solving is a serious discovering that is implemented to improve abilities and upgrade information on the IT. To improve serious programming understanding among students, we have implemented a competitive coding website. Most of the students lack the skills even to analyze a short piece of code. We have also developed a Competitive coding website, the contest management Web server. The server evaluates the submitted software using input and output data in an execution test. We deliver several planning tests for step-by-step sub-goals as partial requirements to adapt a contest for beginners. Which also provides the solution at the end. It also manages the progress of the student and his performance. We're building a Fig 1.Problem Tree teacher support system that includes features including tracking and tutoring. Many competitive coding platforms exist, including CodeChef [2], Spoj [3], CodeForces [4], and HackerEarth [5], which support over 50 programming languages and have a wide I. INTRODUCTION community of programmers who help students and Competitive programming is an analytical sport in which professionals test and develop their coding skills. Its goal is to programmers compete to solve the most algorithmic problems provide a learning, competition, and improvement in the shortest time possible.
    [Show full text]
  • Olympiads in Informatics14
    Olympiads Olympiads Olympiads in InformaticsInformatics Volume 14, 14, 2020 2020 A. ALNAHHAS, N. MOURTADA in Informatics Predicting the Performance of Contestants in Competitive Programming Using Machine Learning Techniques 3 Olympiads in Informatics 14 P.T. DO, B.T. PHAM, V.C. THAN Latest Algorithms on Particular Graph Classes 21 M. DOLINSKY, M. DOLINSKAYA The Technology of Differentiated Instruction in Text Programming in Elementary School Based on the Website dl.gsu.by 37 D.I. ESTEVEZ Consensus Algorithms for Highly Efficient, Decentralized, and Secure Blockchains 47 P. FANTOZZI, L. LAURA Recommending Tasks in Online Judges using Autoencoder Neural Networks 61 D. GINAT Operator Utilization and Abstract Conceptions 77 M. JOVANOV, E. STANKOV Introduction of “Honorable Mention” Award at the International Olympiad in Informatics 87 A. LAAKSONEN, T. TALVITIE CSES – Yet Another Online Judge 105 M. LODI Informatical Thinking 113 M. MIRZAYANOV, O. PAVLOVA, P. MAVRIN, R. MELNIKOV, A. PLOTNIKOV, 14, 2020 Volume V. PARFENOV, A. STANKEVICH Codeforces as an Educational Platform for Learning Programming in Digitalization 133 P.S. PANKOV, A.A. KENZHALIEV Pattern Recognition and Related Topics of Olympiad Tasks 143 M.S. TSVETKOVA, V.M. KIRYUKHIN Top 10 Key Skills in Olympiad in Informatics 151 REPORTS A.S. GUTIÉRREZ. Argentine Olympiad in Informatics 169 S. HALIM. Competitive Programming 4: The New Lower Bound of Programming Contests in the 2020s 177 B. KOSTADINOV, M. JOVANOV. IOI Talks: New Initiative for Publishing Presenta- tions, Events, Interviews,
    [Show full text]