题目重述
题目解析
这个题目有点绕,不是很好理解,关键在于看示例,注意在你决定停止运行后,天上的人不用再管了,他们下来不花钱(不需要runningCost)
面向示例编程就好
这里引用一下@zhe的"有才"评论
题目太费解。我把它重新描述一下,为"最佳跑路时机"。 迪士尼把它的摩天轮游乐场卖给你了,约好明天付
款,今天你先试运行一下。但你其实没有那么多钱,今天必须跑路。 摩天轮不能停,从早上开门就必须一直按
固定的速率旋转。 电力公司不信任你,每旋转一格,电力公司就实时从你的账中划走一笔电费runningCost。
你有未卜先知的能力,知道每个时刻,达到的顾客数目有多少。而且这些顾客都是死忠,坐不上摩天轮就不
走。 每个顾客在坐上去的同时,扫码转账给你boardingCost。 问:你什么时候拉闸跑路,钱最多?天上的顾
客等救援机构来了再说。。。
解题思路
直接面向示例模拟就好,循环中每一步我们用时刻rotateCnt计录就行,用playedPeo记录已经玩过的人数之和,用一个curTolPeo记录当前已经到达但还没有玩过的人,循环的终止条件是已经到达最后的customers而且curTolPeo为0.(代表已经尝试过所有可能的更大值)......注意curTolPeo不是每一次都要增加的d
在循环中的每一层判断curTolPeo等待人数是否超过4,如果超过:
curTolPeo-=4;
playedPeo+=4;
否则 :curTolPeo置零;
playedPeo+=curTolPeo;
curTolPeo=0;
代码如下:
int curTolPeo=0,maxProfit=0;
int playedPeo=0,ans;
for(int rotateCnt=1;rotateCnt<=customers.size()||curTolPeo!=0;rotateCnt++){
if(rotateCnt<=customers.size()) curTolPeo +=customers[rotateCnt-1];
if(curTolPeo>4){
curTolPeo-=4;
playedPeo+=4;
}else{
playedPeo+=curTolPeo;
curTolPeo=0;
}
int curTolProfit=playedPeo*boardingCost - rotateCnt*runningCost;
if(curTolProfit>maxProfit) {ans=rotateCnt; maxProfit=curTolProfit;}
}
return maxProfit>0?ans:-1;
}