Tải bản đầy đủ (.pdf) (508 trang)

IT training data structure practice for collegiate programming contests and education wu wang 2016 02 04

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.25 MB, 508 trang )

Computer Science

• Fundamental programming skills
• Experiments for linear lists
• Experiments for trees
• Experiments for graphs
Each chapter contains a set of problems and includes hints. The book also provides test data
for most problems as well as sources and IDs for online judgments that help with improving
programming skills.
Introducing a multi-options model and considerations of context, Data Structure Practice for
Collegiate Programming Contests and Education encourages students to think creatively
in solving programming problems. By taking readers through practical contest problems from
analysis to implementation, it provides a complete source for enhancing understanding and
polishing skills in programming.

K22004

an informa business

www.crcpress.com

6000 Broken Sound Parkway, NW
Suite 300, Boca Raton, FL 33487
711 Third Avenue
New York, NY 10017
2 Park Square, Milton Park
Abingdon, Oxon OX14 4RN, UK

ISBN: 978-1-4822-1539-7

Data Structure Practice



Based on successful experiences in many world-level contests, the book includes 204 typical
problems and detailed analyses selected from the ACM International Collegiate Programming
Contest and other major programming contests since 1990. It is divided into four sections that
focus on:

Wu
Wang

Combining knowledge with strategies, Data Structure Practice for Collegiate Programming Contests and Education presents the first comprehensive book on data structure in
programming contests. This book is designed for training collegiate programming contest
teams in the nuances of data structures and for helping college students in computer-related
majors to gain deeper understanding of data structure.

Data Structure
Practice
for Collegiate Programming
Contests and Education

Yonghui Wu and Jiande Wang

90000
9 781482 215397

w w w.crcpress.com

K22004 cvr mech.indd 1

12/9/15 9:01 AM



Data Structure
Practice
for Collegiate Programming
Contests and Education



Data Structure
Practice
for Collegiate Programming
Contests and Education

Yonghui Wu and Jiande Wang


Published with arrangement with the original publisher, Beijing Huazhang Graphics and Information Company.

CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2016 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Version Date: 20160126
International Standard Book Number-13: 978-1-4822-1540-3 (eBook - PDF)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been
made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright
holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this

form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://
www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923,
978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For
organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at

and the CRC Press Web site at



Contents
Preface..................................................................................................................................... xiii
Authors......................................................................................................................................xv

Section I  FUNDAMENTAL PROGRAMMING SKILLS
1 Practice for Simple Computing.....................................................................................3
Improving Programming Style................................................................................. 3
1.1.1 Financial Management................................................................................ 4
1.2 Multiple Test Cases.................................................................................................. 5
1.2.1Doubles........................................................................................................ 5
1.2.2 Sum of Consecutive Prime Numbers........................................................... 7
1.3 Precision of Real Numbers....................................................................................... 9
1.3.1 I Think I Need a Houseboat........................................................................ 9
1.4 Improving Time Complexity by Dichotomy...........................................................11

1.4.1Hangover................................................................................................... 12
1.4.2Humidex.....................................................................................................14
1.5Problems..................................................................................................................17
1.5.1Sum............................................................................................................17
1.5.2 Specialized Four-Digit Numbers.................................................................17
1.5.3Quicksum...................................................................................................18
1.5.4 A Contesting Decision................................................................................19
1.5.5 Dirichlet’s Theorem on Arithmetic Progressions.........................................21
1.5.6 The Circumference of the Circle................................................................ 22
1.5.7 Vertical Histogram.................................................................................... 26
1.5.8 Ugly Numbers........................................................................................... 27
1.5.9 Number Sequence...................................................................................... 28
1.1

2 Simple Simulation.......................................................................................................29
2.1

2.2
2.3

Simulation of Direct Statement.............................................................................. 29
2.1.1 Speed Limit................................................................................................ 29
2.1.2 Ride to School............................................................................................31
Simulation by Sieve Method....................................................................................33
2.2.1Self-Numbers..............................................................................................33
Construction Simulation.........................................................................................35
2.3.1Bee..............................................................................................................35

v



vi  ◾ Contents

2.4Problems................................................................................................................. 37
2.4.1 Gold Coins................................................................................................ 37
2.4.2 The 3n + 1 Problem.................................................................................... 38
2.4.3 Pascal Library............................................................................................ 39
2.4.4Calendar.....................................................................................................41
2.4.5Manager..................................................................................................... 42

3 Simple Recursion.........................................................................................................45
3.1
3.2

Calculation of Recursive Functions........................................................................ 46
Solving Problems by Recursive Algorithms..............................................................47
3.2.1 Red and Black............................................................................................ 48
3.3 Solving Recursive Datum........................................................................................51
3.3.1 Symmetric Order........................................................................................51
3.4Problems................................................................................................................. 54
3.4.1Fractal........................................................................................................ 54
3.4.2Sticks......................................................................................................... 56

SUMMARY OF SECTION I
Section II EXPERIMENTS FOR LINEAR LISTS
4 Linear Lists Accessed Directly....................................................................................65
4.1

Application of Arrays 1: Calculation of Dates..........................................................65
4.1.1Calendar.................................................................................................... 66

4.1.2 What Day Is It?.......................................................................................... 68
4.2 Application of Arrays 2: Calculation of High-Precision Numbers.......................... 72
4.2.1 Adding Reversed Numbers.........................................................................74
4.2.2 Very Easy!.................................................................................................. 77
4.3 Application of Arrays 3: Representation and Computation of Polynomials............ 80
4.3.1 Polynomial Showdown................................................................................81
4.3.2 Modular Multiplication of Polynomials..................................................... 83
4.4 Application of Arrays 4: Calculation of Numerical Matrices.................................. 86
4.4.1 Error Correction........................................................................................ 87
4.4.2 Matrix Chain Multiplication..................................................................... 89
4.5 Character Strings 1: Storage Structure of Character Strings................................... 93
4.5.1 TEX Quotes.............................................................................................. 93
4.6 Character Strings 2: Pattern Matching of Character Strings................................... 94
4.6.1 Blue Jeans.................................................................................................. 95
4.6.2Oulipo....................................................................................................... 99
4.7Problems................................................................................................................101
4.7.1 Moscow Time...........................................................................................101
4.7.2 Double Time.............................................................................................104
4.7.3 Maya Calendar.........................................................................................105
4.7.4 Time Zones...............................................................................................107
4.7.5 Polynomial Remains.................................................................................109
4.7.6 Factoring a Polynomial............................................................................. 111
4.7.7 What’s Cryptanalysis?...............................................................................112


Contents  ◾  vii

4.7.8 Run-Length Encoding..............................................................................113
4.7.9Zipper.......................................................................................................114
4.7.10 Anagram Groups...................................................................................... 115

4.7.11 English Number Translator......................................................................117
4.7.12 Message Decowding.................................................................................118
4.7.13 Common Permutation.............................................................................. 119
4.7.14 Human Gene Functions.......................................................................... 120
4.7.15Palindrome.............................................................................................. 122
4.7.16 Power Strings........................................................................................... 123
4.7.17Period...................................................................................................... 123
4.7.18 Seek the Name, Seek the Fame................................................................ 124
4.7.19 Excuses, Excuses!......................................................................................125
4.7.20Product.................................................................................................... 128
4.7.21 Expression Evaluator................................................................................ 128
4.7.22 Integer Inquiry..........................................................................................129
4.7.23 Super-Long Sums..................................................................................... 130
4.7.24Exponentiation.........................................................................................131
4.7.25 Number Base Conversion.........................................................................132
4.7.26 Super-Long Sums..................................................................................... 134
4.7.27 Simple Arithmetics.................................................................................. 134
4.7.28 ab – ba....................................................................................................... 136
4.7.29 Fibonacci Number....................................................................................137
4.7.30 How Many Fibs........................................................................................138
4.7.31Heritage....................................................................................................138

5 Applications of Linear Lists for Sequential Access....................................................141
5.1

Application of Sequence Lists................................................................................142
5.1.1Children...................................................................................................142
5.1.2 The Dole Queue........................................................................................143
5.2 Application of Stacks.............................................................................................145
5.2.1Rails..........................................................................................................145

5.2.2 Boolean Expressions.................................................................................. 151
5.3 Application of Queues...........................................................................................154
5.3.1 A Stack or a Queue?..................................................................................154
5.3.2 Team Queue.............................................................................................156
5.3.3 Printer Queue...........................................................................................161
5.4Problems................................................................................................................163
5.4.1 Roman Roulette........................................................................................163
5.4.2M*A*S*H..................................................................................................164
5.4.3Joseph.......................................................................................................165
5.4.4 City Skyline..............................................................................................166
5.4.5 Anagrams by Stack...................................................................................168

6 Generalized List Using Indexes.................................................................................171
6.1

Solving Problems Using Dictionaries.....................................................................171
6.1.1References.................................................................................................172
6.1.2Babelfish...................................................................................................176


viii  ◾ Contents

6.2

Solving Problems Using a Hash Table and the Hash Method................................179
6.2.110-20-30...................................................................................................179
6.3Problems................................................................................................................185
6.3.1 Spell Checker............................................................................................185
6.3.2 Snowflake Snow Snowflakes.....................................................................188
6.3.3Equations..................................................................................................188


7 Sort of Linear Lists....................................................................................................191
7.1

Using Sort Function in STL..................................................................................191
7.1.1 Hardwood Species....................................................................................191
7.1.2 Who’s in the Middle?................................................................................194
7.1.3 ACM Rank Table......................................................................................195
7.2 Using Sort Algorithms...........................................................................................197
7.2.1 Flip Sort....................................................................................................197
7.2.2Ultra-Quicksort........................................................................................199
7.3Problems................................................................................................................201
7.3.1Ananagrams..............................................................................................201
7.3.2 Grandpa Is Famous.................................................................................. 202
7.3.3 Word Amalgamation............................................................................... 203
7.3.4 Questions and Answers............................................................................ 205
7.3.5 Find the Clones........................................................................................ 206
7.3.6487-3279.................................................................................................. 207
7.3.7 Holiday Hotel.......................................................................................... 209
7.3.8 Train Swapping.........................................................................................210
7.3.9 Unix ls......................................................................................................211
7.3.10 Children’s Game.......................................................................................213
7.3.11 DNA Sorting............................................................................................214
7.3.12 Exact Sum.................................................................................................215
7.3.13Shellsort....................................................................................................216
7.3.14 Tell Me the Frequencies!...........................................................................219
7.3.15 Anagrams (II)...........................................................................................219
7.3.16Flooded!....................................................................................................221
7.3.17 Football Sort............................................................................................ 222
7.3.18Trees........................................................................................................ 225


SUMMARY OF SECTION II
Section III EXPERIMENTS FOR TREES
8 Programming by Tree Structure...............................................................................231
8.1
8.2
8.3

Solving Hierarchical Problems by Tree Traversal...................................................231
8.1.1 Nearest Common Ancestor...................................................................... 232
8.1.2 Hire and Fire........................................................................................... 236
Union–Find Sets Supported by Tree Structure......................................................241
8.2.1 Find Them, Catch Them.......................................................................... 243
8.2.2 Cube Stacking.......................................................................................... 246
Calculation of Sum of Weights of Subtrees by Binary Indexed Trees.................... 248
8.3.1 Apple Tree.................................................................................................250


Contents  ◾  ix

8.4Problems............................................................................................................... 254
8.4.1Friends..................................................................................................... 254
8.4.2 Wireless Network......................................................................................255
8.4.3War...........................................................................................................257
8.4.4 Ubiquitous Religions................................................................................259
8.4.5 Network Connections.............................................................................. 260
8.4.6 Building Bridges.......................................................................................261
8.4.7 Family Tree.............................................................................................. 264
8.4.8 Directory Listing......................................................................................267
8.4.9 Closest Common Ancestors..................................................................... 268

8.4.10 Who’s the Boss?....................................................................................... 269
8.4.11 Disk Tree................................................................................................. 272
8.4.12 Marbles on a Tree.................................................................................... 273
8.4.13 This Sentence Is False................................................................................275

9 Applications of Binary Trees.....................................................................................281
9.1

Converting Ordered Trees to Binary Trees............................................................281
9.1.1 Tree Grafting........................................................................................... 282
9.2 Paths of Binary Trees............................................................................................ 285
9.2.1 Binary Tree.............................................................................................. 285
9.3 Traversal of Binary Trees...................................................................................... 287
9.3.1 Tree Recovery.......................................................................................... 288
9.4Problems................................................................................................................291
9.4.1 Tree Summing..........................................................................................291
9.4.2 Trees Made to Order................................................................................ 292

10 Applications of Classical Trees..................................................................................295

10.1 Binary Search Trees.............................................................................................. 295
10.1.1BST......................................................................................................... 296
10.1.2 Falling Leaves.......................................................................................... 297
10.2 Binary Heaps.........................................................................................................301
10.2.1 Windows Message Queue........................................................................ 303
10.2.2 Binary Search Heap Construction........................................................... 306
10.2.3 Decode the Tree....................................................................................... 309
10.3 Huffman Trees......................................................................................................311
10.3.1 Fence Repair.............................................................................................312
10.4Problems................................................................................................................314

10.4.1 Cartesian Tree...........................................................................................314
10.4.2Argus........................................................................................................316
10.4.3 Black Box..................................................................................................317
10.4.4Heap.........................................................................................................319
10.4.5 How Many Trees?.................................................................................... 320
10.4.6 The Number of the Same BST................................................................. 322
10.4.7 The Kth BST............................................................................................325
10.4.8 The Prufer Code...................................................................................... 330
10.4.9 Code the Tree...........................................................................................331

SUMMARY OF SECTION III


x  ◾ Contents

Section IV EXPERIMENTS FOR GRAPHS
11 Applications of Graph Traversal................................................................................337

11.1 BFS Algorithm......................................................................................................337
11.1.1 Prime Path...............................................................................................338
11.2 DFS Algorithm..................................................................................................... 342
11.2.1 House of Santa Claus............................................................................. 342
11.3 Topological Sort................................................................................................... 344
11.3.1 Following Orders.....................................................................................345
11.3.2 Sorting It All Out................................................................................... 348
11.4 Connectivity of Undirected Graphs.......................................................................352
11.4.1 Knights of the Round Table....................................................................356
11.5Problems............................................................................................................... 362
11.5.1 Ordering Tasks....................................................................................... 362
11.5.2Spreadsheet............................................................................................. 363

11.5.3 Genealogical Tree....................................................................................365
11.5.4 Rare Order............................................................................................. 366
11.5.5 Pushing Boxes.........................................................................................367
11.5.6 Basic Wall Maze......................................................................................373
11.5.7Firetruck..................................................................................................375
11.5.8 Dungeon Master.................................................................................... 377
11.5.9 A Knight’s Journey..................................................................................379
11.5.10 Children of the Candy Corn...................................................................381
11.5.11 Curling 2.0............................................................................................. 383
11.5.12 Shredding Company.............................................................................. 387
11.5.13 Be Wary of Roses................................................................................... 390
11.5.14 Monitoring the Amazon..........................................................................393
11.5.15 Graph Connectivity................................................................................ 394
11.5.16 The Net...................................................................................................395
11.5.17 The Warehouse....................................................................................... 397

12 Algorithms of Minimum Spanning Trees.................................................................405
12.1 Kruskal Algorithm............................................................................................... 405
12.1.1 Constructing Roads............................................................................... 406
12.2 Prim Algorithm.................................................................................................... 408
12.2.1Agri-Net................................................................................................. 409
12.3Problems................................................................................................................412
12.3.1Network..................................................................................................412
12.3.2 Truck History..........................................................................................413
12.3.3 Slim Span................................................................................................414
12.3.4 The Unique MST....................................................................................419
12.3.5Highways............................................................................................... 420

13 Algorithms of Best Paths...........................................................................................423
13.1 Warshall Algorithm and Floyd–Warshall Algorithm............................................ 423

13.1.1Frogger................................................................................................... 424
13.1.2Arbitrage................................................................................................ 427


Contents  ◾  xi

13.2 Dijkstra’s Algorithm............................................................................................. 430
13.2.1Toll..........................................................................................................431
13.3 Bellman–Ford Algorithm..................................................................................... 434
13.3.1 Minimum Transport Cost.......................................................................435
13.4 Shortest Path Faster Algorithm (SPFA Algorithm)............................................... 439
13.4.1 Longest Paths......................................................................................... 440
13.5Problems............................................................................................................... 443
13.5.1 Knight Moves......................................................................................... 443
13.5.2 Big Christmas Tree................................................................................. 444
13.5.3 Stockbroker Grapevine........................................................................... 446
13.5.4 Domino Effect....................................................................................... 448
13.5.5 106 miles to Chicago...............................................................................452
13.5.6AntiFloyd................................................................................................454

14 Algorithms of Bipartite Graphs and Flow Networks.................................................457

14.1 Maximum Matching in Bipartite Graphs..............................................................457
14.1.1Conference..............................................................................................458
14.2 Flow Networks..................................................................................................... 460
14.2.1 Power Network........................................................................................461
14.2.2Trash...................................................................................................... 467
14.3Problems................................................................................................................470
14.3.1 A Plug for UNIX.....................................................................................470
14.3.2 Machine Schedule...................................................................................471

14.3.3 Selecting Courses....................................................................................473
14.3.4 Software Allocation.................................................................................474
14.3.5 Crime Wave.............................................................................................475
14.3.6Pigs......................................................................................................... 477
14.3.7 Drainage Ditches.....................................................................................479
14.3.8 Mysterious Mountain............................................................................. 480

SUMMARY OF SECTION IV
Bibliography.......................................................................................................................489
Index..................................................................................................................................491



Preface
Since the 1990s, the ACM International Collegiate Programming Contest (ACM-ICPC) has
become a worldwide programming contest. Every year, more than 10,000 students and more than
1,000 universities participate in local contests, preliminary contests, and regional contests all over
the world. In the meantime, programming contests’ problems from all over the world can be gotten, analyzed, and solved by us. These contest problems can be used not only for programming
contest training, but also for education.
In our opinion, not only a programming contestant’s ability, but also a computer student’s
ability is based on his or her programming knowledge system and programming strategies for
solving problems. The programming knowledge system can be summarized as a famous formula:
algorithms + data structures = programs. It is also the foundation for the knowledge system of
computer science and engineering. Strategies solving problems are strategies for data modeling and
algorithm design. When data models and algorithms for problems are not standard, what strategies we should take to solve these problems?
Based on the ACM-ICPC, we published a series of books, not only for systematic programming contest training, but also for better polishing computer students’ programming skill, using
programming contests’ problems: “Data Structure Experiment: For Collegiate Programming
Contest and Education,” “Algorithm Design Experiment: For Collegiate Programming Contest
and Education,” and “Programming Strategies Solving Problems” in Mainland China. And the
traditional Chinese versions for “Data Structure Experiment: For Collegiate Programming Contest

and Education” and “Programming Strategies Solving Problems” were also published in Taiwan.
“Data Structure Practice: For Collegiate Programming Contests and Education” is the English
version for “Data Structure Experiment: For Collegiate Programming Contest and Education.”
There are 4 sections, 14 chapters, and 200 programming contest problems in this book. Section I,
“Fundamental Programming Skills,” focuses on experiments and practices for simple computing, simple simulation, and simple recursion, for students just learning programming languages.
Section II, “Experiments for Linear Lists,” Section III, “Experiments for Trees,” and Section IV,
“Experiments for Graphs,” focus on experiments and practices for data structure.
Characteristics of the book are as follows:


1. The book’s outlines are based on the outlines of data structures. Programming contest problems and their analyses and solutions are used as experiments. For each chapter, there is a
“Problems” section to let students solve programming contests’ problems, and hints for these
problems are also shown.
2.Problems in the book are all selected from the ACM-ICPC regional and world finals programming contests, universities’ local contests, and online contests, and from 1990 to now.

xiii


xiv  ◾ Preface



3. Not only analyses and solutions or hints to problems are shown, but also test data for most of
problems are provided. Sources and IDs for online judges for these problems are also given.
They can help readers better and more easily polish their programming skills.

Therefore, the book can be used not only as an experiment book, but also for systematic programming contests’ training.
We appreciate Professors Steven Skiena, Rezaul Chowdhury, C. Jinshong Hwang, Ziliang
Zong, Hongchi Shi, and Rudolf Fleischer. They provided us platforms in which English is the
native language that improved our manuscript.

We appreciate our students Julaiti Alafate, Zheyun Yao, and Hao Zhang. They finished programs in the book.
The work is supported by the China Scholarship Council.
Online judge systems for problems in this book are as follows:
Online Judge Systems

Abbreviations

Website

Peking University Online Judge
System

POJ

/>
Zhejiang University Online Judge
System

ZOJ

/>
UVA Online Judge System

UVA

/> />
Ural Online Judge System

Ural


/>
SGU Online Judge System

SGU

/>
If you discover anything you believe to be an error, please contact us through Yonghui Wu’s
email: Your help is appreciated.
Yonghui Wu
Jiande Wang


Authors
Yonghui Wu is associate professor at Fudan University. He acted as the coach of Fudan University
Programming Contest teams from 2001 to 2011. Under his guidance, Fudan University qualified for Association for Computing Machinery International Collegiate Programming Contest
(ACM-ICPC) World Finals every year and won three medals (bronze medal in 2002, silver medal
in 2005, and bronze medal in 2010). Since 2012, he has published a series of books for programming contests and education. Since 2013, he has given lectures in Oman, Taiwan, and the United
States for programming contest training. He is the chair of the ICPC Asia Programming Contest
1st Training Committee now.
Jiande Wang is a senior high school teacher and a famous coach for the Olympiad in Informatics
in China. He has published 24 books for programming contests since the 1990s. Under his guidance, his students have won seven gold medals, three silver medals, and two bronze medals in the
International Olympiad in Informatics for China.

xv



FUNDAMENTAL
PROGRAMMING SKILLS


I

Programming language is an introductory course of data structures and algorithms. This course
enables students to program by programming languages. Programming languages, data structures, and algorithm designs are skills that computer students must polish. Therefore, polishing
fundamental programming skills is the first section for this book. There are three chapters in
Section I covering
1.Computing
2.Simulation
3.Recursion
These three chapters are not only a review of programming languages, but also an introductory
course on data structure.



Chapter 1

Practice for Simple Computing
The pattern of a programming contest problem is input–process–output. A problem for simple
computing is a problem whose process is simple. For such a problem, we should only consider
optimizing the process and dealing with input and output correctly. The goals of Chapter 1 are
as follows:
1.Students master C/C++ or Java programming language.
2.Students become familiar with online judge systems and programming environments.
3. Students begin to learn how to transfer a practical problem into a computing process, implement the computing process by a program, and debug the program to pass all test cases.
“God is in the details.” In Chapter 1, problems are relatively simple. We should notice formats
of input and output, precision, and time complexity. Therefore, the following topics will be discussed in this chapter:
1.Programming style
2.Multiple test cases
3.Precision of real numbers
4.Improving time complexity by dichotomy

Normally, a complex problem consists of several subproblems for simple computing. “Even the
longest journey begins with a single step.” Polishing programming skills should begin with solving
simple computing problems.

1.1 Improving Programming Style
A program’s writing style is not only for its visual sense, but also for examining the program and
debugging its errors. A program’s style also shows whether its programming idea is clear. It is hard
to say which kind of programming style is good, but there are some rules for programing style.
They are discussed in the following experiments.
3


4  ◾  Data Structure Practice: For Collegiate Programming Contests and Education

1.1.1  Financial Management
Larry graduated this year and finally has a job. He’s making a lot of money, but somehow never
seems to have enough. Larry has decided that he needs to get a hold of his financial portfolio and
solve his financial problems. The first step is to figure out what’s been going on with his money.
Larry has his bank account statements and wants to see how much money he has. Help Larry by
writing a program to take his closing balance from each of the past 12 months and calculate his
average account balance.
Input
The input will be 12 lines. Each line will contain the closing balance of his bank account for a
particular month. Each number will be positive and displayed to the penny. No dollar sign will
be included.
Output
The output will be a single number, the average (mean) of the closing balances for the 12 months.
It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by
the end of the line. There will be no other spaces or characters in the output.
Sample Input


Sample Output

100.00

$1581.42

489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75
Source: ACM Mid-Atlantic United States 2001.
IDs for online judges: POJ 1004, ZOJ 1048, UVA 2362.

Analysis
The problem’s pattern, input–process–output, is very simple: First, the income of 12 months
a[0 .. 11] is input by a for statement for(i = 0; i < 12; i++), and the total income


Practice for Simple Computing  ◾  5

11


sum =


∑ a[i ]
i =0

is calculated. Then the average monthly income avg = sum/12 is calculated. Finally, avg is output
in accordance with the problem’s output format.
Program
#include<iostream>                      // Preprocessor Directive
using namespace std;                     // Using C++ Standard Library
int main()                              // Main function
{
      double avg, sum=0.0, a[12]={0};      // Real variable avg and sum,
and real array a
      int i;                              // Integer variable i
      for(i=0;i<12;i++){        // Input the income of 12 months a[0..11]
and summation
                cin>>a[i];
                sum+=a[i];
            }
      avg=sum/12;                      // Calculate the average monthly
income
      printf("$%.2f",avg);             // Output the average monthly
income
      return 0;
   }

From the above program, we can get the following:
First, the input and output of the program must meet formats for input and output. In this

problem, each input number will be positive and displayed to the penny, and the output will be
rounded to the nearest penny, preceded immediately by a dollar sign and followed by an end of
the line. If the program doesn’t meet formats for input and output, it will be judged as the wrong
answer.
Second, a program should be readable. The style of a program should be serration based on a
logical level.
Finally, program annotations should be given.

1.2  Multiple Test Cases
The financial management problem (Section 1.1.1) has only one test case. In order to guarantee the
correctness of a program, for most problems there are multiple test cases. In some circumstances,
the number of test cases is given; in other circumstances, the number of test cases isn’t given, but
the mark of the input end is given.

1.2.1 Doubles
As part of an arithmetic competency program, your students will be given randomly generated
lists of 2–15 unique positive integers and asked to determine how many items in each list are twice


6  ◾  Data Structure Practice: For Collegiate Programming Contests and Education

some other item in the same list. You will need a program to help you with the grading. This
program should be able to scan the lists and output the correct answer for each one. For example,
given the list


1 4 3  2  9  7 18 22

your program should answer 3, as 2 is twice 1, 4 is twice 2, and 18 is twice 9.
Input

The input file will consist of one or more lists of numbers. There will be one list of numbers per
line. Each list will contain from 2 to 15 unique positive integers. No integer will be larger than 99.
Each line will be terminated with the integer 0, which is not considered part of the list. A line with
the single number –1 will mark the end of the file. The example input below shows three separate
lists. Some lists may not contain any doubles.
Output
The output will consist of one line per input list, containing a count of the items that are double
some other item.
Sample Input

Sample Output

1 4 3 2 9 7 18 22 0

3

2 4 8 10 0

2

7 5 11 13 1 3 0

0

–1
Source: ACM Mid-Central United States 2003.
IDs for online judges: POJ 1552, ZOJ 1760, UVA 2787.

Analysis
There are multiple test cases for the problem. Therefore, a loop statement is used to deal with multiple test cases. The loop enumerates every test case. –1 marks the end of the input. Therefore, –1

is the end condition of the loop. In the loop statement, there are two steps:
1.A loop inputs a test case into array a and accumulates the number of elements n in the test
case. 0 marks the end of the test case.
2.A double loop enumerates all pairs of a[i] and a[j] (0 <= i < n – 1, i + 1 <= j < n) in the test
case and determines whether (a[i]*2 == a[j] || a[j]*2 == a[i]) holds.
Program
#include <iostream>                    // Preprocessor Directive
using namespace std;                    // Using C++ standard library
int main()                             //Main function
{
     int i, j, n, count, a[20];                //  Integer variables i,
j, n, count and array a


Practice for Simple Computing  ◾  7

     cin>>a[0];                          // Input the first element
     while(a[0]!=-1)                    // If it is not the end of input,
input a new test case
     {  n=1;                            // Input array a
         for( ; ; n++)
         {
           cin>>a[n];
           if (a[n]==0) break;
          }
          count=0;        // Determine how many items in each list are
twice some other item
          for (i=0; i          {
            for (j=i+1; j

            {
              if (a[i]*2==a[j] || a[j]*2==a[i])    // Accumulation
              count++;
              }
            }
            cout<            cin>>a[0];                      // Input the first element of
next test case
     }
     return 0;
   }

In this problem, the number of test cases and the size of a test case are unknown. Normally, a
double-loop statement is used for the program structure: the outer loop is used to enumerate every
test case, and the inner loop is used to deal with a test case.
In some problems, if the size of the test data is larger, all the test cases are dealt with by the
same method, and the result area is known, its time complexity can be improved by an offline
method. First, all solutions within the specified range are calculated and stored in a constant array.
Then the program deals with the constant array directly for each test case. It can avoid duplication
of computing.

1.2.2  Sum of Consecutive Prime Numbers
Some positive integers can be represented by a sum of one or more consecutive prime numbers.
How many such representations does a given positive integer have? For example, the integer 53
has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations:
2 + 3 + 5 + 7 + 11 + 13, 11 + 13 + 17, and 41. The integer 3 has only one representation, which is
3. The integer 20 has no such representations. Note that summands must be consecutive prime
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. Your
mission is to write a program that reports the number of representations for the given positive
integer.

Input
The input is a sequence of positive integers, each in a separate line. The integers are between 2 and
10,000, inclusive. The end of the input is indicated by a zero.


8  ◾  Data Structure Practice: For Collegiate Programming Contests and Education

Output
The output should be composed of lines each corresponding to an input line, except the last zero.
An output line includes the number of representations for the input integer as the sum of one or
more consecutive prime numbers. No other characters should be inserted in the output.
Sample Input

Sample Output

2

1

3

1

17

2

41

3


20

0

666

0

12

1

53

2

0
Source: ACM Japan 2005.
IDs for online judges: POJ 2739, UVA 3399.

Analysis
Because the program needs to deal with consecutive prime numbers for each test case, and the
upper limit of prime numbers is 10,000, the offline method can be used to solve the problem.
First, all prime numbers less than 10,001 are obtained and stored in array prime[1 .. total] in
ascending order.
Then we deal with the test cases one by one:
Suppose the input number is n; the sum of consecutive prime numbers is cnt; the number of
representations for cnt == n is ans.
A double loop is used to get the number of representations for n:

◾◾ The outer loop i: for(int i  =  0; n  >=  prime[i]; i++) enumerates all possible minimum
prime[i].
◾◾ The inner loop j: for(int j = i; j < total && cnt < n; j++), cnt += prime[j], is to calculate the
sum of consecutive prime numbers. If cnt ≥ n, then the loop ends, and if cnt == n, then the
number of representations is ans++.
When the outer loop ends, ans is the solution to the test case.
Program
#include <iostream>                    // Preprocessor Directive
using namespace std;                   // Using C++ Standard Library
const int maxp = 2000, n = 10000;      // Set the size of prime array and
the upper limit of  prime numbers
int prime[maxp], total = 0;              // Initialization


×