No Such Blog or Diary
ICPC by Haskell
引き続きICPC2005 Regional, Tokyo の問題Cを Haskell で.サイコロの全パタン生成が一番面倒かも.全盛生後の比較の高速化のために色名を数字に置き換える部分は少々手抜きかも.
-- Problem C in ACM/ICPC 2005 ASIA Regional Tokyo
-- 2005/11/04 Brute Force (24^3)
import Control.Monad
import Debug.Trace
import List
main = getProblems >>= mapM_ (putStrLn.show.solve)
getProblems =
do
[n] <- getNums
if n==0 then return []
else do
xs <- replicateM n getEntry
liftM (xs:) getProblems
where
getEntry = liftM words getLine
getNums = liftM (map read.words) getLine
solve::[[String]]->Int
solve [d] = 0
solve ds = let
tab = zip (nub.sort.concat $ ds) [1..]
rep x = case (lookup x tab) of Just i -> i
ds' = map (map rep) ds
in foldl1 min.map cost.map (head ds':).prod.map genAll.tail $ ds'
cost = sum . map cost'. trans
cost' xs = length xs - longest (sort xs)
longest (h:ts) = foldl1 max.map snd $ scanl (\(x, c) y -> if y == x then (x, c+1) else (y, 1)) (h,1) ts
trans s] = map (\x -> [) xs
trans (xs:xss) = zipWith (:) xs $ trans xss
prod s] = map (\x->[) xs
prod (xs:xss) = concat.map (\x -> map (x:) (prod xss)) $ xs
-- generate all dice equivalent to the die by rotation
genAll = concat . map rots2 . rots1
-- rotations around an axis (the front surface is fixed)
rots2 [x1,x2,x3,x4,x5,x6] = [[x1,x2,x3,x4,x5,x6],
[x1,x3,x5,x2,x4,x6],
[x1,x5,x4,x3,x2,x6],
[x1,x4,x2,x5,x3,x6]]
-- rotations to move each surface to the front
rots1 [x1,x2,x3,x4,x5,x6] = [[x1,x2,x3,x4,x5,x6],
[x2,x6,x3,x4,x1,x5],
[x3,x2,x6,x1,x5,x4],
[x4,x2,x1,x6,x5,x3],
[x5,x1,x3,x4,x6,x2],
[x6,x5,x3,x4,x2,x1]]
- Comments: 0
- TrackBack (Close): -
forall A
Haskell で forall A. A -> A 系の型を作ってみる.まずもっとも単純に.
let x = x
t を型変数として x :: t で,undef 以外の何者でもない気がする.
んで,次.
let f x = x
これで f :: t->t . id 関数なような.
続いて
let y f = f (y f)
これで y :: (t->t)->t . fixpoint 関数とうか Y コンビネータなような.
ついでに,
let g y z = if True then y else z
とすると g :: t->t->t になる.意味のある関数ではないが... さて,これ以降はどうなるのだろうか?
ついでなので,forall a,b,... . a -> b -> ... も作ろうとすると
let f x y = f x y
とかで引数の数を増やせばいくらでもいける.意味はないけど.意味のあるものってどれくらいあるんだろう? undef, id, fixpoint 以外に意味のあるのがあるか?
- Comments: 0
- TrackBack (Close): -
ふと思ったこと@DT上P244
- 2005-11-12 (Sat)
- 一般
live を逆さまにすると evil だなぁ.逆さまになる英単語って意識したことなかったので新鮮な感じだ.
- Comments: 0
- TrackBack (Close): -
ICPC by Haskell
ICPC2005 Regional, Tokyo の問題Bを Haskell で.queue を使ったシミュレーションだけど面倒だから一ターンごとにリスト生成...
-- Problem B in ACM/ICPC 2005 ASIA Regional Tokyo
-- 2005/11/04 Brute Force
import Control.Monad
import Debug.Trace
main = getProblems >>= mapM_ (putStrLn.show.solve)
getProblems =
do
[m, c, n] <- getNums
if (n==0 && m==0 && c==0) then return []
else do
xs <- replicateM n getEntry
liftM ((m,c,n,xs):) getProblems
where
getEntry = getNums >> getNums
getNums = liftM (map read.words) getLine
-- it's better to make each entry of ds the pair of it and its length
solve (m,c,n,xs) = sl xs 0 (take m $ repeat [])
where
sl [] t _ = t
sl ys t ds = let
(hs, ys') = unzip $ map (\x->(head x, tail x)) ys
(t', ds') = sl' ds hs
in sl (filter (not.(==[])) ys') (t'+t) ds'
sl' ds = foldl searchOne (0,ds)
searchOne (t, ds) x = let
p = length $ takeWhile (not.or.map (==x)) ds
ds' =if p<m then take p ds++[filter (not.(==x)) (ds!!p)]++drop (p+1) ds
else ds
in insertOne (t+p+1) ds' x
insertOne t ds x =
if length (head ds) < c then (t+1, (x:head ds):tail ds)
else let
p = length $ takeWhile ((==c).length) ds
ds' = if p<m then take p ds++[x:(ds!!p)]++drop (p+1) ds
else ds
p' = length $ takeWhile ((==c).length) ds'
hds = head ds
tds = tail ds
q = p'-1
tds' =if p'<m then take q tds++[last hds:(tds!!q)]++drop (q+1) tds
else tds
in (t+p+p'+p+5, (x:init hds):tds')
- Comments: 0
- TrackBack (Close): -
希望が見えたか?
- 2005-11-10 (Thu)
- 一般
Microsoft から来たメールによると,どうやら theSpokeアカウント を使わずに「Visual Studio 2005 Professional」を手に入れられるようになるらしい.いろいろと文句や問い合わせが行った結果なのかどうかしらんけどありがたいかも.
- Comments: 0
- TrackBack (Close): -
どうにかならんかなぁ
- 2005-11-09 (Wed)
- 一般
ASCII24の記事によると『Visual Studio .NET the Spoke Premium 2003』を購入、登録した学生を対象に『Visual Studio 2005 Professional Edition』を追加費用なしでダウンロード提供してくれるらしい.詳しい情報を得ようとだいぶ前に登録したきりの"the Spoke”のサイトへ行ってみるが... 3ヶ月間ログインしないと登録が抹消されますって言われて登録消えているらしい orz. もう一回登録しなおしてもいいものなのかどうか...
- Comments: 0
- TrackBack (Close): -