0% found this document useful (0 votes)
155 views11 pages

Problem A. Ladder: Input File Format

This document describes 8 problems involving dynamic programming. It provides the input and output formats for each problem, as well as sample test cases. The problems include finding the maximum sum climbing a ladder, the maximum number of grass cells a rabbit can visit, the number of knight's paths on a chessboard, minimum path cost for a king, maximum score in a ski slalom, minimum number of pyramids to hold a given number of cannonballs, longest path in a directed acyclic graph, and determining peaceful sets of integers.

Uploaded by

Sanskar Bhargava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views11 pages

Problem A. Ladder: Input File Format

This document describes 8 problems involving dynamic programming. It provides the input and output formats for each problem, as well as sample test cases. The problems include finding the maximum sum climbing a ladder, the maximum number of grass cells a rabbit can visit, the number of knight's paths on a chessboard, minimum path cost for a king, maximum score in a ski slalom, minimum number of pyramids to hold a given number of cannonballs, longest path in a directed acyclic graph, and determining peaceful sets of integers.

Uploaded by

Sanskar Bhargava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Saint Petersburg State University

2012-2013 Group C Problemset 5 Dynamic Programming

Problem A. Ladder

Input filename: ladder.in


Output filename: ladder.out
Time limit: 2 seconds
Memory limit: 256 Mb

The ladder has n stairs, indexed by numbers 1, 2, . . . , n from bottom to top. There is a
number written on each stair. You want to climb from the bottom (stair with index 0) to the
very top (stair with index n) of the ladder. In one step, you can step up either one or two
stairs. As you climb, you add up the numbers on the stairs you step on. You want to maximize
this sum.

Input file format


The first line contains a single integer 1 ≤ n ≤ 100. The second line contains n space-
separated integers −10 000 ≤ a1 , a2 , . . . , an ≤ 10 000 written on the stairs.

Output file format


On the first and only line print a single integer—the maximum possible climb sum on the
given ladder.

Sample tests

ladder.in ladder.out
2 3
1 2

2 1
2 -1

3 3
-1 2 1

Page 1 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem B. Rabbit

Input filename: lepus.in


Output filename: lepus.out
Time limit: 2 seconds
Memory limit: 256 Mb

The rabbit jumps along the line. There are n cells on this line, indexed by numbers from 1
to n. Some cells are swamps, and the rabbit cannot land into them. Other cells contain tasty
grass, a rabbits’ beloved snack.
The rabbit starts at the cell with index 1 and wants to get to the cell with index n. It wants
to eat as much grass along the way as possible, without landing into the swamp. The rabbit’s
design features mean that it can only jump 1, 3, and 5 cells to the right.
Determine what is the maximum number of cells with grass that a rabbit can visit.

Input file format


The first line contains a single integer 2 ≤ n ≤ 1000. The second line has a string w of
length n. ’w’ corresponds to a swamp, ’"’ corresponds to grass, and ’.’ corresponds to an
empty cell. You may assume that the first and the last cell do no contain neither grass nor
swamps.

Output file format


On the first and only line print a single integer—the maximum possible number of cells with
grass that a rabbit can visit on his path. Print −1 if it cannot reach the destination print −1.

Sample tests

ladder.in ladder.out
4 2
."".

5 0
.w"..

9 -1
.www.www.

Page 2 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem C. Knight’s move

Input filename: knight.in


Output filename: knight.out
Time limit: 1 second
Memory limit: 256 Mb

You are given a rectangular board N × M , with a chess knight in the top left corner. The
knight can only move downwards, and to the right, that’s it:

• two squares down and one to the right;

• one square down and two to the right.

Find the number of different ways the knight can take to reach the bottom-right corner of
the board.

Input file format


The first line contains two space-separated integers 1 ≤ N, M ≤ 50.

Output file format


On the first and only line print the number of different knight paths.

Sample tests

knight.in knight.out
3 2 1

31 34 293930

Page 3 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem D. Path Cost

Input filename: king2.in


Output filename: king2.out
Time limit: 1 seconds
Memory limit: 256 Mb

Every square of an 8 × 8 chessboard has a non-negative number written on it. The king
wants to move from the lower-left corner of the board to the upper right corner, but it can only
walk upwards, to the right, and diagonally to the upper right. The cost of visiting a square is
equal to the number written on it.
Move the knight from the lower-left corner of the board to the upper right corner with
minimum path cost.

Input file format


Eight lines of the input contain eight non-negative integers up to 1000 each. Lower left
corner is guaranteed to be 0.

Output file format


On the first and only line print a single integer—the minimum path cost.

Sample tests

king2.in king2.out
9 9 9 9 9 9 1 9 56
9 9 9 9 9 1 9 2
9 9 9 9 9 9 1 9
9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9
0 9 9 9 9 9 9 9

Page 4 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem E. Slalom

Input filename: slalom.in


Output filename: slalom.out
Time limit: 2 seconds
Memory limit: 256 Mb

One of the popular ski resorts in Italy hosts a slalom competition. Every participant will
go down the mountain on the skis. On every path of the path, the participant will get some
number of points. As the participant goes down, we add these points together. The participant
with the maximum number of points wins. The mountain is a triangle of integers, representing
points for parts of the path. Participants may choose whether to go left or right on every layer
as they go down to the next layer. The participants start in the highest number and may finish
in any of the lowest.
For example, the mountain can look like this:

1
4 3
5 6 7
8 9 0 9

Find the maximum number of points that one can score.

Input file format


The first line of the input file contains one positive integer 1 ≤ n ≤ 100, representing
the number of layers. The following n lines contain the description of layers, with i numbers
−100 ≤ ai,1 , ai,2 , . . . , ai,i ≤ 100 in the i-th row.

Output file format


On the first and only line print the maximum score possible.

Sample tests

slalom.in slalom.out
4 20
1
4 3
5 6 7
8 9 0 9

Page 5 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem F. Cannonballs

Input filename: balls.in


Output filename: balls.out
Time limit: 2 seconds
Memory limit: 256 Mb

Captain Vasya always keeps some cannonballs on his ship if he ever needs to fight with the
pirates. He stores the cannonballs in the pyramids. Each layer of the pyramid is an equilateral
triangle of side k filled with cannonballs. The lowest layer of the pyramid has side n, the next
layer has n − 1, . . . , and the last layer is a single cannonball, i.e. the triangle of side 1.
For example, a pyramid of size 3 consists of the following layers (from top to bottom):

X
X X

X
X X
X X X

Clearly, a single pyramid can contain only 1, 4, 10, 20, . . . cannonballs.


However, Vasya wants to take exactly m cannonballs. What is the minimum number of
pyramids he will need?

Input file format


The first line contains the number of test cases 1 ≤ T ≤ 20. The following m lines have one
number each, the number 1 ≤ mi ≤ 300 000 of cannonballs in the i-th test.

Output file format


For every test case print the minimum number of pyramids on the separate line.

Sample tests

balls.in balls.out
5 1
1 2
5 3
9 3
15 2
91

Page 6 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem G. Longest Path

Input filename: longpath.in


Output filename: longpath.out
Time limit: 2 seconds
Memory limit: 256 Mb

Find the longest path in a given directed acyclic graph.

Input file format


The first line contains two integers: the number of vertices n ≤ 10 000 and number of edges
m ≤ 100 000. The following m lines contain edges’ descriptions in the form 1 ≤ bi , ei ≤ n,
where bi is the beginning, and ei is the end of the i-th edge.
It s guaranteed that the input graph has neither cycles nor loops.

Output file format


On the first and only line print the maximum possible path length in the given graph.

Sample tests

longpath.in longpath.out
5 5 3
1 2
2 3
3 4
3 5
1 5

Page 7 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem H. Peaceful Sets

Input filename: peacefulsets.in


Output filename: peacefulsets.out
Time limit: 2 seconds
Memory limit: 256 Mb

We call a set of positive integers peaceful, if any two of its elements differ at least two times.
We call the sum of the elements of the set its strength. Find the number of peaceful sets of a
given strength.

Input file format


The first line contains a single integer 1 ≤ n ≤ 2000.

Output file format


On the first and only line print a single integer—the number of peaceful sets of strength n.

Sample tests

peacefulsets.in peacefulsets.out
2 1

5 2

Page 8 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem I. Knapsack

Input filename: knapsack.in


Output filename: knapsack.out
Time limit: 1 seconds
Memory limit: 64 Mb

You came across a buried treasure, consisting of N golden ingots with given weights. Find
the maximum total mass of gold that you can take if you only have a knapsack of capacity S.

Input file format


The first line contains two integers 1 ≤ S ≤ 10 000 and 1 ≤ N ≤ 300. The second line
contains N space-separated integers 0 ≤ w1 , w2 , . . . , wn ≤ 100 000, representing the ingots’
weights.

Output file format


On the first and only line print a single integer—the maximum possible total mass of gold
you can put in your knapsack.

Sample tests

knapsack.in knapsack.out
10 3 9
1 4 8

20 4 19
5 7 12 18

Page 9 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem J. Bad Substring

Input filename: badsubs.in


Output filename: badsubs.out
Time limit: 2 seconds
Memory limit: 256 Mb

Find the number of strings consisting of letters ’a’, ’b’, ’c’ of the given length n and
without a substring "ab".

Input file format


The first line contains a single integer 0 ≤ n ≤ 22.

Output file format


On the first and only line print the number of such strings.

Sample tests

ladder.in ladder.out
0 1

3 21

11 46368

Page 10 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming

Problem K. Josephus Problem

Input filename: joseph.in


Output filename: joseph.out
Time limit: 2 seconds
Memory limit: 64 Mb

N children are standing in the circle. They start counting clockwise. Whenever someone
gets number P , they leave the circle, the counter is reset, and counting contains from their
right neighbour.
The last man standing wins.
Find the winning number (children are indexed 1 to N , starting from the same child that
the count begins from).

Input file format


The first line contains two integers 1 ≤ N, P ≤ 106 .

Output file format


Print the winning number on the first and only line.

Sample tests

joseph.in joseph.out
3 4 2

Page 11 of 11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy