您的位置:首页 > 运维架构

uva 714 - Copying Books

2012-10-08 20:51 246 查看
CopyingBooks
Beforetheinventionofbook-printing,itwasveryhardtomakeacopyofabook.Allthecontentshadtobere-writtenbyhandbysocalled
scribers.Thescriberhadbeengivenabookandafterseveralmonthshefinisheditscopy.Oneofthemostfamousscriberslivedinthe15thcenturyandhisnamewasXaveriusEndricusRemiusOntiusXendrianus(Xerox).Anyway,theworkwas
veryannoyingandboring.Andtheonlywaytospeeditupwastohiremorescribers.

Onceuponatime,therewasatheaterensemblethatwantedtoplayfamousAntiqueTragedies.Thescriptsoftheseplaysweredividedintomanybooksandactorsneededmorecopiesofthem,ofcourse.Sotheyhiredmanyscriberstomakecopiesofthesebooks.
Imagineyouhavembooks(numbered

)thatmayhavedifferentnumberofpages(


)andyouwanttomakeonecopyofeachofthem.Yourtaskistodividethesebooksamong
kscribes,

.Eachbookcanbeassignedtoasinglescriberonly,andeveryscribermustgetacontinuoussequenceofbooks.
Thatmeans,thereexistsanincreasingsuccessionofnumbers

suchthat
i-thscribergetsasequenceofbookswithnumbersbetweenbi-1+1and
bi.Thetimeneededtomakeacopyofallthebooksisdeterminedbythescriberwhowasassignedthemostwork.Therefore,ourgoalistominimizethemaximumnumberofpagesassignedtoasinglescriber.Yourtaskistofindthe
optimalassignment.

Input

TheinputconsistsofNcases.Thefirstlineoftheinputcontainsonlypositiveinteger
N.Thenfollowthecases.Eachcaseconsistsofexactlytwolines.Atthefirstline,therearetwointegers
mandk,

.Atthesecondline,thereareintegers


separatedbyspaces.Allthesevaluesarepositiveandlessthan10000000.

Output

Foreachcase,printexactlyoneline.Thelinemustcontaintheinputsuccession


dividedintoexactly
kpartssuchthatthemaximumsumofasinglepartshouldbeassmallaspossible.Usetheslashcharacter(`/')toseparatetheparts.Theremustbeexactlyonespacecharacterbetweenanytwosuccessivenumbersandbetweenthenumberand
theslash.

Ifthereismorethanonesolution,printtheonethatminimizestheworkassignedtothefirstscriber,thentothesecondscriberetc.Buteachscribermustbeassignedatleastonebook.

SampleInput

2
93
100200300400500600700800900
54
100100100100100


SampleOutput

100200300400500/600700/800900
100/100/100/100100
给定一组数,划分成k份,是每一份的和的最大值最小,当存在多种划分时,后面的和尽量大于前面的;

刚放完假做题奇慢YY,首先这个和一定是在这m个数中最大的到m个数[code]#include<stdio.h>
#include<string.h>
longlongn,m,a[510],b[510],ans[510],left,right,mid,t;
longlongcheck(longlongmax)
{
longlongi,num=0,pos=0;
memset(b,0,sizeof(b));
for(i=n;i>=1;i--)
{
if(pos+a[i]<=max&&i>=m-num)pos=pos+a[i];
else
{
++num;
pos=a[i];
b[i]=1;
}
}

if(num+1==m)
{
for(i=1;i<=n;i++)
ans[i]=b[i];
return1;
}
elsereturn0;
}
intmain()
{
longlongi;
scanf("%lld",&t);
while(t--)
{
left=0;right=0;
scanf("%lld%lld",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
right+=a[i];
if(a[i]>left)left=a[i];
}
memset(ans,0,sizeof(ans));
while(left<=right)
{
mid=(left+right)/2;
if(check(mid)==1)right=mid-1;
elseleft=mid+1;
}
for(i=1;i<n;i++)
if(ans[i]==1)printf("%lld/",a[i]);
elseprintf("%lld",a[i]);
printf("%lld\n",a
);
}
return0;
}

的和之间,所以可以二分查找,重点在判断某个值的划分是否成立,倒着贪心累加,注意k组划分不能有空集[/code]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: