Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:21:17+00:00 2026-05-31T14:21:17+00:00

In preparation for the upcoming Google Code Jam, I’ve started working on some problems.

  • 0

In preparation for the upcoming Google Code Jam, I’ve started working on some problems. Here’s one of the practice problems I’ve tried:

http://code.google.com/codejam/contest/32016/dashboard#s=p0

And here’s the gist of my current Haskell solution:

{-
- Problem URL: http://code.google.com/codejam/contest/32016/dashboard#s=p0
-
- solve takes as input a list of strings for a particular case
- and returns a string representation of its solution.
-}

import Data.List    

solve :: [String] -> String
solve input = show $ minimum options
    where (l1:l2:l3:_) = input
          n = read l1 :: Int
          v1 = parseVector l2 n
          v2 = parseVector l3 n
          pairs = [(a,b) | a <- permutations v1, b <- permutations v2]
          options = map scalar pairs

parseVector :: String -> Int -> [Int]
parseVector line n = map read $ take n $ (words line) :: [Int]

scalar :: ([Int],[Int]) -> Int
scalar (v1,v2) = sum $ zipWith (*) v1 v2

This works with the sample input provided, but dies on the small input to an out of memory error. Increasing the max heap size appears to do nothing but let it run indefinitely.

My main question is how to go about optimizing this so that it will actually return a solution, other than passing in the -O flag to ghc, which I’ve already done.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:21:19+00:00Added an answer on May 31, 2026 at 2:21 pm

    The solution I came up with:

    {-
    - Problem URL: http://code.google.com/codejam/contest/32016/dashboard#s=p0
    -
    - solve takes as input a list of strings for a particular case
    - and returns a string representation of its solution.
    -}
    
    import Data.List  
    
    solve :: [String] -> String
    solve input = show result
        where (l1:l2:l3:_) = input
              n = read l1 :: Int
              v1 = parseVector l2 n
              v2 = parseVector l3 n
              result = scalar (sort v1) (reverse $ sort v2)
    
    parseVector :: String -> Int -> [Integer]
    parseVector line n = map read $ take n $ (words line) :: [Integer]
    
    scalar :: [Integer] -> [Integer] -> Integer
    scalar v1 v2 = sum $ zipWith (*) v1 v2 :: Integer
    

    This seems to give the right answer and is orders of magnitude faster. The trick I guess is to not take the word “permutation” at face value when reading problems like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code: // WAP to implement student result preparation. #include stdafx.h #include iostream
In preparation of a future application design, I've started some investigations on CQRS-style applications
I'm working on some test preparation material and stuck on this problem. Show a
I am writing some android code in preparation for a graphics intensive app I
I'm doing some simple tests (in preparation for a larger project) to call an
In preparation for my move to a production server later, I'm using this code
How should one structure validation, preparation and arrangement (etc) of data before dealing with
I am working of a file preparation software to enable translators work easily and
I have been adapting our code in preparation of moving our code to the
I've done quite a bit of research and preparation with my code to try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.