My Weblog

Blog about programming and math

SPOJ 4301. Tables

SPOJ 4301. Tables is related to sorting. Sort the list in decreasing order and count how many elements add upto to required screw ( k*s) .

import Data.List


solve::[Integer] -> String 
solve (n:k:s:xs) =  show.snd $ b     where 
	xs' = sortBy ( \ a b -> compare b a ) xs 
	b = foldl ( \(acc , cnt) x  -> if acc  < (k*s) then ( acc + x , cnt + 1 ) else ( acc , cnt ) ) (0,0) xs'


format :: [String] -> [String]
format [] = []
format (x:y:xs) = (x++" "++y) : format xs 

main = interact $ unlines. map ( solve . map read . words ) . format .  lines

Advertisements

May 25, 2011 - Posted by | Programming

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: