Sunteți pe pagina 1din 4

I SFAHAN U NIVERSITY OF T ECHNOLOGY

Algorithm Design
Spring 2016
Problem Set 3

Due Date: 6 May (17 Ordibehesht)

1 DYNAMIC P ROGRAMMING

Dynamic Programming (also known as dynamic optimization) is a method for solving a


complex problem by breaking it down into a collection of simpler subproblems, such that

1. Solve each of those subproblems just once

2. Store their solutions

3. Next time the same subproblem occurs, instead of recomputing its solution, one sim-
ply looks up the previously computed solution

1
Problem 1. Partition Problem:

Given an array A of n positive numbers such that:


n
P
A[i ] = 2B
i =1

Decide whether A can be partitioned into the two sets S 1 and S 2 such that:
|S
P1 | |S
P2 |
S 1 [i ] = S 2 [i ] = B
i =1 i =1

Sample Input
1 5 11 1
8 7 6 5 4

Sample Output
NO
YES

Hint: See chapter 4 of "COMPUTERS AND INTRACTABILITY" by Michael R. Garey and


David S. Johnson.

Problem 2. 2 Dimensional Knapsack:

Given a set of n objects, with each tree positive integers height, width and profit (h i , w i , p i ).
and two positive integers H and W which shows the height and width of our knapsack .
The goal is to select a subset K of this objects such that the the selected weight obeys the
knapsack bound :
P
hk ≤ H
k∈K

and
P
wk ≤ W
k∈K
P
and such that the selected profit p k is maximized.
k∈K

Sample Input
15 41 66 77
31 52 9 98
10 20 30 40
90
40

2
Sample Output
1 3

Problem 3. K-Based Numbers


Let’s consider K -based numbers, containing exactly N digits. We define a number to be valid
if its K-based notation doesn’t contain two successive zeros. For example:

• 1010230 is a valid 7-digit number;

• 1000198 is not a valid number;

• 0001235 is not a 7-digit number, it is a 4-digit number.

Given two numbers N and K, you are to calculate an amount of valid K based numbers,
containing N digits. You may assume that 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 18.

Input Format
The numbers N and K in decimal notation separated by the line break.
2
10

Output Format
90

Problem 4. Beautiful People


The most prestigious sports club in one city has exactly N members. Each of its members is
strong and beautiful. More precisely, i -th member of this club (members being numbered by
the time they entered the club) has strength Si and beauty Bi . Since this is a very prestigious
club, its members are very rich and therefore extraordinary people, so they often extremely
hate each other. Strictly speaking, i -th member of the club Mr X hates j -th member of the
club Mr Y if S i ≤ S j and B i ≥ B j or if Si ≥ S j and Bi ≤ B j (if both properties of Mr X are
greater then corresponding properties of Mr Y, he doesn’t even notice him, on the other hand,
if both of his properties are less, he respects Mr Y very much).
To celebrate a new 2003 year, the administration of the club is planning to organize a party.
However they are afraid that if two people who hate each other would simultaneouly attend
the party, after a drink or two they would start a fight. So no two people who hate each
other should be invited. On the other hand, to keep the club presti≥ at the apropriate level,
administration wants to invite as many people as possible.
Being the only one among administration who is not afraid of touching a computer, you are
to write a program which would find out whom to invite to the party.

3
Input Format
The first line of the input file contains integer N — the number of members of the club. (
2l eN ≤ 100, 000 ). Next N lines contain two numbers each — S i and B i respectively ( 1 ≤
Si , Bi ≤ 109 ).
4
1 1
1 2
2 1
2 2

Output Format
On the first line of the output file print the maximum number of the people that can be
invited to the party. On the second line output N integers — numbers of members to be
invited in arbitrary order. If several solutions exist, output any one.
2
1 4

Good Luck!

S-ar putea să vă placă și