Finish 1998-12-21 00:00:00 UTC

More tweak IV

by Peter Acklam

Status: Passed
Results: 0.36% blank
CPU Time: 9.534
Score: 80.3488
Submitted at: 1998-12-18 16:53:57 UTC
Scored at: 2000-03-16 16:51:51 UTC

Current Rank: 5th
Based on: More tweak III (diff)
Basis for: More tweak V (diff)

Comments
Please login or create a profile.
Code
function indexList = binpack(songList,mediaLength)
[s,t] = sort(songList);
a = size(s,2);
b = min(a,round(4*a*mediaLength/sum(s)));
c = 90+0.1*a;
[d,e] = sort(rand(b,ceil(c/2)));
e = [e e(end:-1:1,:)];
e = e(:,1:c);
q = 0.25*(a-b);
if q > 1
r = floor(q*rand(1,c));
e = e+r(ones(1,b),:);
else
r = rand(c,1);
end
f = cumsum(s(e));
g = sum(f<=mediaLength);
[d,h] = max(f(g+(0:b:b*c-b)));
indexList = t(e(1:g(h),h));
leftOff = t(e(g(h)+1:end,h));
remTime = mediaLength-d;
onez1 = ones(g(h),1);
onez2 = ones(b-g(h),1);
newFree = remTime + songList(onez2,indexList) - songList(onez1,leftOff)';
newFree(newFree<0) = 4294967296;
[m,i] = min(newFree);
[m,j] = min(m);
if m<remTime
aux = indexList(j);
indexList(j) = leftOff(i(j));
leftOff(i(j)) = aux;
newFree = m+songList(onez2,indexList)-songList(onez1,leftOff)';
newFree(newFree<0) = 4294967296;
[m1,i] = min(newFree);
[m1,j] = min(m1);
if m1<m
aux = indexList(j);
indexList(j) = leftOff(i(j));
leftOff(i(j)) = aux;
newFree = m1+songList(onez2,indexList)-songList(onez1,leftOff)';
newFree(newFree<0) = 4294967296;
[m2,i] = min(newFree);
[m2,j] = min(m2);
if m2<m1
indexList(j) = leftOff(i(j));
end
end
end