Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-17-2006, 06:57 PM   #1 (permalink)
3UNKNOWN3
Registered User
 
Join Date: Mar 2006
Posts: 1
3UNKNOWN3 is on a distinguished road
Problem

Okay, so I was at a programming competition today and my team was programming in QBasic (I know.). Anyway, we went through the first 4 problems (there were 5 in all) much faster than everyone else, but the last one just stumped us and we lost. I've done some more coding today, using C++ and FreeBasic and I couldn't get a working solution. I would like help with the solution in C++ (C is fine too). The problem read as follows:
The stores in a mall are offering free coupons. Each store's coupon has a specific dollar value. The dollar values are different for different stores. Customers are free to pick up one coupon from each store except that no one is allowed to pick up coupons from two adjacent stores. Assume there are 10 stores arranged in a single row and that you want to pick up coupons with as large a total dollar value as possible.
Write a program which inputs 10 positive integers corresponding to the coupon dollar value in the same order as the stores in the mall, and the outputs the chosen dollar values needed to get the maximal total, together with the total.

For example,
If the input is: 5 7 3 1 4
The output should be: 5 3 4 12

If the input is: 8 1 2 6 3
The output should be: 8 6 14

Thanks for any help. I REALLY want to know how you would go about doing this.
3UNKNOWN3 is offline   Reply With Quote
Old 03-18-2006, 02:38 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Well, you've got two sub problems here. The first is to generate permutations of the input list with certain restrictions. The second is to find a maximal value for the sum of a valid permutation.

One way to do it in C/C++ would be to represent each solution to the problem as a binary number with ones indicating the coupons a user took. So if I took the coupons from stores 1,3,5,7,9 the solution would look like '1010101010' . You can then write a function to convert a bit vector like this to sum using the input numbers.

Now that yoou have a representation for the solutions, you'll need an array to store them in. You could safely make this array global and have a function which adds solutions to the array, and another function that calulates the maximum value by iterating over this array, converting the bitvectors to integers representing the total value of the coupons they represent.

So that just leaves finding the permutations. Since the problem says you can't get coupons from any adajacent store you can write a recursive function that takes an index value and a partial solution, that iterates over all legal stores further down the line from itelf, and calls itself on those stores. (I know that was confusing so maybe a bit of code would explain it more clearly)
Code:
/* hop from store to store */
void find_next_valid_store(int index, int solution_vector) {
     for (int i=index+1;i<10;i++) {
          /*find the next non-adjacent store from index */
	  int valid_store =  i + 1; 
          /*add this storeplus any previous stores to our list as a possible solution */
	  int new_solution = update_bitvector(solution_vector, valid_store);  
	  add_solution( new_solution);
	  find_next_valid_store(valid_store, new_solution);
     }
}
/* all valid solutions could be found by starting at each store and then running find_next_valid */
for(int i=0; i < 10; i++) {
         find_next_valid_store( i, make_bitvector_starting_at( i ) );
}
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
parse error problem in game.. can't seem to find problem solution. slashdot Standard C, C++ 5 08-03-2005 09:15 PM
JSP code problem j.gohel Java 7 04-15-2005 03:07 PM
Hashing problem jodders Standard C, C++ 1 02-09-2005 02:51 PM
Problem Assignment (Urgent help req.) Boltress Standard C, C++ 0 01-12-2005 08:59 AM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 05:44 PM


All times are GMT -8. The time now is 12:44 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting