您的位置:首页 > 编程语言 > Go语言

uva 11054 - Wine trading in Gergovia

2012-09-05 19:32 302 查看
2006/2007ACMInternationalCollegiateProgrammingContest

UniversityofUlmLocalContest

WinetradinginGergovia

Asyoumayknowfromthecomic"AsterixandtheChieftain'sShield",Gergoviaconsistsofonestreet,andeveryinhabitantofthecityisawinesalesman.Youwonderhowthiseconomyworks?Simpleenough:everyonebuyswinefromotherinhabitantsofthe
city.Everydayeachinhabitantdecideshowmuchwinehewantstobuyorsell.Interestingly,demandandsupplyisalwaysthesame,sothateachinhabitantgetswhathewants.

Thereisoneproblem,however:Transportingwinefromonehousetoanotherresultsinwork.Sinceallwinesareequallygood,theinhabitantsofGergoviadon'tcarewhichpersonstheyaredoingtradewith,theyareonlyinterestedinsellingorbuyinga
specificamountofwine.Theyarecleverenoughtofigureoutawayoftradingsothattheoverallamountofworkneededfortransportsisminimized.

InthisproblemyouareaskedtoreconstructthetradingduringonedayinGergovia.Forsimplicitywewillassumethatthehousesarebuiltalongastraightlinewithequaldistancebetweenadjacenthouses.Transportingonebottleofwinefromonehouse
toanadjacenthouseresultsinoneunitofwork.

InputSpecification

Theinputconsistsofseveraltestcases.Eachtestcasestartswiththenumberofinhabitants
n(2≤n≤100000).Thefollowinglinecontainsnintegersai(-1000≤ai≤1000).Ifai≥0,itmeansthattheinhabitantlivingintheithhousewantstobuyaibottlesofwine,otherwise
ifai<0,hewantstosell-aibottlesofwine.Youmayassumethatthenumbersaisumupto0.

Thelasttestcaseisfollowedbyalinecontaining0.

OutputSpecification

Foreachtestcaseprinttheminimumamountofworkunitsneededsothateveryinhabitanthashisdemandfulfilled.Youmayassumethatthisnumberfitsintoasigned64-bitinteger(inC/C++youcanusethedatatype"longlong",inJAVAthedatatype"long").

SampleInput

5
5-41-31
6
-1000-1000-1000100010001000
0

SampleOutput

9
9000
不知道第几次看到了,学校oj比赛了出现了很多次了,很明显的按位贪心

#include<stdio.h>
longlonga[100010],s,n,i,j;
intmain()
{
while(scanf("%lld",&n),n)
{
s=0;
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(i=2;i<=n;i++)
{
if(a[i-1]>=0){s=s+a[i-1];a[i]+=a[i-1];}
else{s=s-a[i-1];a[i]=a[i]+a[i-1];}
}
printf("%lld\n",s);
}
return0;
}

[/code]

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