Computer Science Canada

DWITE Feb 21, Problem 2

Author:  MysticVegeta [ Tue Feb 21, 2006 8:55 pm ]
Post subject:  DWITE Feb 21, Problem 2

Quote:
DWITE Online Computer Programming Contest
February 2006
Problem 2
Floppy Disk 3 ½- inch High Density
Over the course of approximately 35 years, floppy disks have evolved from 8 inches in size down to 3 ½ inches.
Their storage capacity ranged from 80 kilobytes (KB) to 240 megabytes (MB). Most recently they are becoming
extinct, with floppy disk drives being an optional accessory with the purchase of a new computer.
One of the more commonly used floppy disk, during its time, was the 3 ½ -inch 1.44 MB capacity disk.
Your job in this problem is to put files onto the 3 ½ -inch 1.44 MB capacity disk so that there is a minimum
amount of unused space left on the floppy disk.
The input file (DATA21.txt for the first submission and DATA22.txt for the second submission) will contain
five sets of data. Each set begins with the an integer, N, the number of files available to go onto the floppy disk,
1 <= N <= 25. The next N lines, for each set, will contain S, an integer, the size, in kilobytes (KB), of each of
the files, 1 <= S <= 1440.
The output file (OUT21.txt for the first submission and OUT22.txt for the second submission) will contain five
lines of data. Each line will contain the minimum amount of unused space left on the floppy disk.
Sample Input ( 3 sets of data only)
5
1000
500
100
150
400
6
500
233
650
75
22
875
4
235
578
900
599
Sample Output
40
35
28
Sample Input Analysis
In the first set, the files with sizes 1000 and 400 use up the most storage space on the diskette.
In the second set, the files with sizes 650, 500, 233 and 22 use up the most storage space on the diskette.
In the third set, the files with sizes 599, 578 and 235 use up the most storage space.


I know this uses DP, I read tuts on dp around a month ago but I cant seem to code this one, I know it uses beginner level DP but incase if you haven't seen my posts, I am one of them Sad

Anyways, DP help will be appreciated.
Please do not tell me to make combos using nested for loops in while loops as I know I can code it using that but it takes time making all possible combos.
Thanks.

Author:  zylum [ Tue Feb 21, 2006 11:36 pm ]
Post subject: 

here's a program which uses brute force but its not fast enough. it can only handle up to 17 files but the limit is 25... this means that input with 25 files will run 2^8 times longer.

code:
var ifile, ofile : int
open : ifile, "DATA21.txt", get
open : ofile, "OUT21.txt", put

var N, size, maxsize : nat

for : 1 .. 5

    get : ifile, N
    var files : array 1 .. N of int

    for i : 1 .. N
        get : ifile, files (i)
    end for

    maxsize := 0

    for i : 0 .. 2 ** N - 1
        size := 0
        for j : 0 .. N - 1
            if ((1 shl j) & i) > 0 then
                size += files (j + 1)
            end if
        end for
        if size <= 1440 then
            maxsize := max (maxsize, size)
        end if
    end for

    put : ofile, 1440 - maxsize
end for

close (ifile)
close (ofile)

Author:  MysticVegeta [ Wed Feb 22, 2006 1:00 am ]
Post subject: 

Sadly it doesnt execute in time with 25 inputs. But I need the DP solution.. brute force is good but in a limit.

Author:  bugzpodder [ Wed Feb 22, 2006 10:43 am ]
Post subject: 

The trick here is to realize that what goes on the disk doesn't matter, as long as each file is used at most once.

code:
dp [0] = true;

for i:1..nfiles
  for decreasing j:1440..0
    if dp[j] = true and j+size[i] <=1440 then
      dp[j+size[i]]=true
    end if
  end for
end for

Author:  MysticVegeta [ Wed Feb 22, 2006 2:39 pm ]
Post subject: 

But I dont undersatnd, will it like this? the elements set to true in the dp arrays sum be the max possible under 1440? or will it be the elements set to false in the dp array?

Author:  bugzpodder [ Wed Feb 22, 2006 7:24 pm ]
Post subject: 

this generates all subset sums of the files under 1440.

Author:  Martin [ Wed Feb 22, 2006 9:50 pm ]
Post subject: 

It's like this.

We know that worst case, we can't fit anything onto the disk. So that's our base case ... d[0]:=true (the rest of the array is initialized to false)

Think of it like a distance thing ... how far away from 0 can we get as a combination of the files?


For each file we go backwards through our table - as bugz said, we don't care which files are put into the table, but that each one can be used once.

For example, suppose instead of 1440 we had 10, and our file sizes were 3, 4 and 5.

Our array to start (blanks are false) - 11 blanks
[T][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
size[1] = 3
from 10 to 0 goes to 0 (the only T element), checks to see if 0 + 3 < 10
It is, so now we have:
[T][ ][ ][T][ ][ ][ ][ ][ ][ ][ ]
The farthest right T represents the most space we can use
Next size[2]=4
Iterating, we figure out the combinations with this.
Either 0 + 4 or 3 + 4 are valid, so we end up with (setting 4 and 7 to true)
[T][ ][ ][T][T][ ][ ][T][ ][ ][ ]
next size[3]=5
7 + 5 is invalid (because it's >10), but 4 + 5 and 3 + 5 are valid and so is 0 + 5
[T][ ][ ][T][T][T][ ][T][T][T][ ]

Now we have an array representing all of the possible file sizes that can be created as a combination of the files. This means that 9 is the most space we can use, but also note that this means it's impossible to create a disk that uses size 1, 2, 6 or 10.

Author:  MysticVegeta [ Wed Feb 22, 2006 11:08 pm ]
Post subject: 

AH! Excellent description Martin Smile If you werent a mod I would give you 100 bits Smile My next question was "I dont get it, explain" I was about to post that but your post made me understand it! I am gonna go over it again and do the rest of the problems that I wasnt able to do which used DP. Thanks a lot Smile

Author:  bugzpodder [ Wed Feb 22, 2006 11:15 pm ]
Post subject: 

how about giving me 100 bits? I think I deserved it Wink

Author:  Martin [ Thu Feb 23, 2006 1:25 am ]
Post subject: 

bugzpodder wrote:
how about giving me 100 bits? I think I deserved it Wink

Alright, I'll give you 100 bits. I remember you taught me about dynamic programming in grade 11 (I think it was grade 11) with the golf club question. Smile

Author:  Martin [ Thu Feb 23, 2006 2:03 am ]
Post subject: 

MysticVegeta wrote:
AH! Excellent description Martin Smile If you werent a mod I would give you 100 bits Smile My next question was "I dont get it, explain" I was about to post that but your post made me understand it! I am gonna go over it again and do the rest of the problems that I wasnt able to do which used DP. Thanks a lot Smile


All DP is is induction.

We can solve it for N = 0, and N = 1 isn't that hard (where N = number of files)

For N = 2, we just add to our solution for N = 1. And so on. It takes a bit to wrap your head around, but it's not difficult.

Author:  MysticVegeta [ Thu Feb 23, 2006 10:18 am ]
Post subject: 

Martin wrote:
It takes a bit to wrap your head around, but it's not difficult.

Tell me about it. I tried to get the tuts so badly but got them just a little so I was able to do question just exactly like the tuts! The first one I read was about a month ago on TC, when I came across a question from SRM 286 I think. http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg

Edit: Is doing this boolean way more efficient/faster than the using a best variable?

Author:  Martin [ Thu Feb 23, 2006 8:13 pm ]
Post subject: 

Yes, and using a 'best' variable won't work most of the time.

For the above example (with 10) suppose the numbers were 7, 5 and 4.

You put 7 in. Best is now 7. You try to put 5 in. It doesn't go in, because it's less than 7 and 5+7 > 10.

Same problem with 4... 4 + 7 > 10. So you get a solution of 7, but the right answer is 5+4 = 9.

It's easy to find out what the best is after the fact (just go backwards through the array).

Author:  MysticVegeta [ Sat Feb 25, 2006 12:16 pm ]
Post subject: 

yeah setting an array true/false is great idea. and going backwards is fairly easy. There are some questions with strings that involve dp too and they are pretty hard like this one, it was used for SRM 285 DIV 2 lvl 2/DIV 1 lvl 1.

Quote:
You have a sentence written entirely in a single row. You would like to split it into several rows by replacing some of the spaces with "new row" indicators. Your goal is to minimize the width of the longest row in the resulting text ("new row" indicators do not count towards the width of a row). You may replace at most K spaces.

You will be given a String sentence and an int K. Split the sentence using the procedure described above and return the width of the longest row.

Definition

Class: SentenceSplitting
Method: split
Parameters: String, int
Returns: int
Method signature: int split(String sentence, int K)
(be sure your methodis public)


Constraints
- sentence will contain between 1 and 50 characters, inclusive.
- sentence will consist of only letters ('a'-'z', 'A'-'Z') and spaces (' ').
- Each space character in sentence will be between two letters.
- K will be between 1 and 50, inclusive.

Examples
0)


"This is a test sentence"

1

Returns: 13

You should split the sentence between the words "a" and "test".
1)


"TheOnlyWord"

37

Returns: 11

2)


"One veeeeeeeeeeeeeeeeeeery long word"

2

Returns: 22

3)


"Each tournament round is an elimination round"

3

Returns: 15

Author:  bugzpodder [ Sat Feb 25, 2006 8:46 pm ]
Post subject: 

K is at most 50. So this is greedy (try K = 1, 2, ..., 50)... and greedily take the longest word...

Author:  MysticVegeta [ Sat Feb 25, 2006 10:01 pm ]
Post subject: 

Is greedy a hard concept to grasp? cause I dotn wanna be wasting time on learning it not knowing it is too much a wide concept for my puny little brain.

Author:  bugzpodder [ Sun Feb 26, 2006 8:29 am ]
Post subject: 

Not at all. let me do an example. In a class Miss Thompson offered you and other people in your class candies. What you do in a greedy situation istake all the candies and not share it with any of your classmates.

Author:  MysticVegeta [ Sun Feb 26, 2006 12:00 pm ]
Post subject: 

Greedy is mean but helpful Very Happy But what exactly "is" to take all in that problem?

Also, does it require knowing a prerequiste concept of something?

Author:  bugzpodder [ Sun Feb 26, 2006 6:07 pm ]
Post subject: 

Fix a width, say w.

Basically you are to determine if u can break the setence up with at most K spaces such that each line has width w or less.

So u keep on taking words (greedily) until the next word would exceed the width, and break the line. count how many breaks you've used to ensure it doesnt exceed K. Finally do that for all possible width and take the least one (start from the smallest, the first width you find would be the answer).

Author:  MysticVegeta [ Mon Feb 27, 2006 2:32 pm ]
Post subject: 

ok, I am gonna go find some tuts on greedy.

Author:  Martin [ Mon Feb 27, 2006 7:46 pm ]
Post subject: 

This is one of those cases where you have to realize what information is and isn't important.

We need to know the length of the words, but not the actual words. Also, we know that there are no double spaces between words, so we can safely load it all into an array of ints representing the length of each word (if we have n ints, then there are n - 1 spaces).

Now, we set numLines = max (K, n-1), because we obviously can't split more spaces than we have (for example, a sentence 'hello world' with K = 5 is valid, but we can really only make 1 split).

So now we have a list of numbers. Keeping in mind that a space represents a single character (so the line 'Hello World' is length 5 + 1 + 5 = 11), we now are going to generate the possible lengths of each line.

So let's say that our word lengths are 7 5 2 11 3 5 and K = 2 (meaning we will have 3 lines).

Now we just try all logical combinations (ie. 7 + 1 + 5 is a valid line length, but 7 + 1 + 2 is not), remembering to add 1 for spaces in a line, and you're in business.

Author:  mckhatri1 [ Wed Jul 05, 2006 8:36 am ]
Post subject: 

Martin wrote:
It's like this.

We know that worst case, we can't fit anything onto the disk. So that's our base case ... d[0]:=true (the rest of the array is initialized to false)

Think of it like a distance thing ... how far away from 0 can we get as a combination of the files?


For each file we go backwards through our table - as bugz said, we don't care which files are put into the table, but that each one can be used once.

For example, suppose instead of 1440 we had 10, and our file sizes were 3, 4 and 5.

Our array to start (blanks are false) - 11 blanks
[T][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
size[1] = 3
from 10 to 0 goes to 0 (the only T element), checks to see if 0 + 3 < 10
It is, so now we have:
[T][ ][ ][T][ ][ ][ ][ ][ ][ ][ ]
The farthest right T represents the most space we can use
Next size[2]=4
Iterating, we figure out the combinations with this.
Either 0 + 4 or 3 + 4 are valid, so we end up with (setting 4 and 7 to true)
[T][ ][ ][T][T][ ][ ][T][ ][ ][ ]
next size[3]=5
7 + 5 is invalid (because it's >10), but 4 + 5 and 3 + 5 are valid and so is 0 + 5
[T][ ][ ][T][T][T][ ][T][T][T][ ]

Now we have an array representing all of the possible file sizes that can be created as a combination of the files. This means that 9 is the most space we can use, but also note that this means it's impossible to create a disk that uses size 1, 2, 6 or 10.


How do you know how many array spaces to start with?

Author:  MysticVegeta [ Sat Jul 08, 2006 11:49 am ]
Post subject: 

well you know you have to reach the sum closest 1440. so the array length would be 1441. Since dp[0] would always be true so that it can used as a previous state meaning the previous reached sum. Kinds hard to explain but you would get it if you read the entire post by him


: