uva 11054 - Wine trading in Gergovia
2012-09-05 19:32
302 查看
2006/2007ACMInternationalCollegiateProgrammingContest
UniversityofUlmLocalContest
city.Everydayeachinhabitantdecideshowmuchwinehewantstobuyorsell.Interestingly,demandandsupplyisalwaysthesame,sothateachinhabitantgetswhathewants.
Thereisoneproblem,however:Transportingwinefromonehousetoanotherresultsinwork.Sinceallwinesareequallygood,theinhabitantsofGergoviadon'tcarewhichpersonstheyaredoingtradewith,theyareonlyinterestedinsellingorbuyinga
specificamountofwine.Theyarecleverenoughtofigureoutawayoftradingsothattheoverallamountofworkneededfortransportsisminimized.
InthisproblemyouareaskedtoreconstructthetradingduringonedayinGergovia.Forsimplicitywewillassumethatthehousesarebuiltalongastraightlinewithequaldistancebetweenadjacenthouses.Transportingonebottleofwinefromonehouse
toanadjacenthouseresultsinoneunitofwork.
n(2≤n≤100000).Thefollowinglinecontainsnintegersai(-1000≤ai≤1000).Ifai≥0,itmeansthattheinhabitantlivingintheithhousewantstobuyaibottlesofwine,otherwise
ifai<0,hewantstosell-aibottlesofwine.Youmayassumethatthenumbersaisumupto0.
Thelasttestcaseisfollowedbyalinecontaining0.
[/code]
UniversityofUlmLocalContest
WinetradinginGergovia
Asyoumayknowfromthecomic"AsterixandtheChieftain'sShield",Gergoviaconsistsofonestreet,andeveryinhabitantofthecityisawinesalesman.Youwonderhowthiseconomyworks?Simpleenough:everyonebuyswinefromotherinhabitantsofthecity.Everydayeachinhabitantdecideshowmuchwinehewantstobuyorsell.Interestingly,demandandsupplyisalwaysthesame,sothateachinhabitantgetswhathewants.
Thereisoneproblem,however:Transportingwinefromonehousetoanotherresultsinwork.Sinceallwinesareequallygood,theinhabitantsofGergoviadon'tcarewhichpersonstheyaredoingtradewith,theyareonlyinterestedinsellingorbuyinga
specificamountofwine.Theyarecleverenoughtofigureoutawayoftradingsothattheoverallamountofworkneededfortransportsisminimized.
InthisproblemyouareaskedtoreconstructthetradingduringonedayinGergovia.Forsimplicitywewillassumethatthehousesarebuiltalongastraightlinewithequaldistancebetweenadjacenthouses.Transportingonebottleofwinefromonehouse
toanadjacenthouseresultsinoneunitofwork.
InputSpecification
Theinputconsistsofseveraltestcases.Eachtestcasestartswiththenumberofinhabitantsn(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]
相关文章推荐
- [等价转换]UVa11054 - Wine trading in Gergovia
- UVA 11054 Wine trading in Gergovia 葡萄酒交易 贪心+模拟
- Uva - 11054 - Wine trading in Gergovia
- UVa 11054/HDU 1489/POJ 2940 Wine trading in Gergovia(贪心&双向队列)
- UVA11054:Wine trading in Gergovia(Gergovia的酒交易)
- UVa 11054 - Wine trading in Gergovia
- UVA 11054 Wine trading in Gergovia
- UVA 11054 Wine trading in Gergovia(贪心)
- UVA 11054 Wine trading in Gergovia
- [贪心]UVA11054 Wine trading in Gergovia
- UVa 11054 - Wine trading in Gergovia
- UVa 11054 Wine trading in Gergovia(扫描)
- UVA - 11054 Wine trading in Gergovia 扫描法
- UVa 11054 - Wine trading in Gergovia
- uva 11054 - Wine trading in Gergovia
- UVa 11054 - Wine trading in Gergovia
- Wine trading in Gergovia(uva 11054)
- uva 11054 Wine trading in Gergovia (贪心 + 模拟)
- uva 11054 wine trading in gergovia (归纳【好吧这是我自己起的名字】)——yhx
- UVa11054 poj2940 sdut2370 Wine trading in Gergovia(贪心)