提出的算法以下谜题!算法

2023-09-11 03:22:57 作者:去吹吹夏日晚风吧,也许会遇见浪漫,今日QQ乐园小编为您设计一

有n个汽油排成一圈铺位。每个双层从静止一定距离隔开。你选择出行的一些模式,它需要汽油1升涵盖1公里距离。你不能无限地画出汽油的任何数额从每个铺位每个铺位都有一些有限的汽油而已。但是你知道,升汽油中的所有铺位的总和等于所覆盖的距离。

There are n petrol bunks arranged in circle. Each bunk is separated from the rest by a certain distance. You choose some mode of travel which needs 1litre of petrol to cover 1km distance. You can't infinitely draw any amount of petrol from each bunk as each bunk has some limited petrol only. But you know that the sum of litres of petrol in all the bunks is equal to the distance to be covered.

即让P1,P2,...光合速率为n铺位安排循环。 d1为p1和p2之间的距离,d2为p2和p3之间的距离。 dn是PN和p1.Now找出从哪里旅行可以开始这种上下铺之间的距离,你的旅行方式从来没有耗尽燃料。

ie let P1, P2, ... Pn be n bunks arranged circularly. d1 is distance between p1 and p2, d2 is distance between p2 and p3. dn is distance between pn and p1.Now find out the bunk from where the travel can be started such that your mode of travel never runs out of fuel.

推荐答案

让我们选择一个垃圾的算法,我们知道错了,看看它为什么是错的......

Let's choose a junk algorithm that we know is wrong to see why it is wrong...

符号...

当前点:(气体在当前点加仑,使下一个点所需加仑) - >剩余的气体(加仑)

Current Point: (gallons of gas at Current Point, gallons required to make next point)-> Remaining Gas (gallons)

在多一点的数学形式:

P [I]:(G(P [I])中,d(P [I + 1])) - >的总和(克(P [I]) - D(P [I + 1]))从i = 1到当前点-1

P[i]: (g(P[i]), d(P[i+1])) -> sum of (g(P[i]) - d(P[i+1])) from i=1 to current point-1

(现在的坏算法...)

(And now for the bad algorithm...)

P1: (2,2) -> 0 (at P2)
P2: (5,3) -> 2 (at P3)
P3: (2,4) -> 0 (at P4)
P4: (2,5) -> -3 (ran out of gas 3 miles short of P5)

为了使其为P5,我们必须有气体的三个额外加仑由我们使它到P3的时间,和为了有3个额外加仑在P3中,我们需要有3个额外加仑在P1:

In order to make it to P5, we have to have three extra gallons of gas by the time we make it to P3, and in order to have 3 extra gallons at P3, we need to have 3 extra gallons at P1:

??? -> +3 (at P1)
P1: (2,2) -> 0+3 = 3 (at P2)
P2: (5,3) -> 2+3 = 5 (at P3)
P3: (2,4) -> 0+3 = 3 (at P4)
P4: (2,5) -> -3 +3 = 0 (made it to P5)

诀窍,因此要找到最糟糕的部分 - 在这里你没有给予足够的天然气穿越他们。我们的知道的我们无法从P4,P3,P2,P1或启动。我们必须从某个地方开始较早,存够气循坏部分做出来。

The trick, therefore, is to find the very worst sections -- where you are not given enough gas to traverse them. We know we can't start from P4, P3, P2, or P1. We have to start somewhere earlier and save up enough gas to make it through the bad section.

目前无疑是圈内多重利空的部分,使这个有点复杂,但它实际上是很容易弄清楚如何做到这一点。

There will no doubt be multiple bad sections within the circle, making this somewhat complicated, but it's actually quite easy to figure out how to do this.

这是的可能的跟在最糟糕的舒展在圈内接下来的几个点可以在拉伸后走,但前提是他们不会更改您的天然气储量。 (例如,最坏的舒展后点给你2个加仑的汽油,并让你去2加仑距离的下一个点。)

It's possible that the next few points following the very worst stretch in the circle could be traveled after the stretch, but only if they make no changes to your gas reserves. (e.g. the point after the worst stretch gives you 2 gallons of gas and makes you go 2 gallons of distance to the next point.)

在某些情况下,但是,最糟糕的部分必须覆盖过去。那是因为你开始在该节之前,你需要尽可能多的天然气储存起来成为可能,而最糟糕的拉伸之后的下一个点可能给你气的最后一点,你需要的,这意味着你需要遍历它之前服用在最坏的舒展。虽然有可能存在多个解决方案,这个问题的简单的事实是,穿越最糟糕的部分是最后的总是的解决方案。下面是一些code:

In some cases, however, the worst section MUST be covered last. That's because before you start on that section, you need as much gas saved up as possible, and the next point after the worst stretch might give you the very last bit of gas that you need, which means you need to traverse it prior to taking on the worst stretch. Although there may exist multiple solutions, the simple fact of the matter is that traversing the worst section last is ALWAYS a solution. Here's some code:

class point_ {
int gasGiven_;
int distanceToNextPoint_;

public:
int gasGiven() {return gasGiven_;}
int distanceToNextPoint {return distanceToNextPoint_;}
}

class Circle_ {
public:
numberOfPoints;
point_ *P;
}

在main():

In main():

int indexWorstSection=0;
int numberPointsWorstSection=0;
int worstSum=0;
int currentSum=0;
int i=0; 
int startingPoint =0;

// construct the circle, set *P to malloc of numberOfPoints point_'s, fill in all data

while (i<(Circle.numberOfPoints-1) || currentSum<0)
   {
   currentSum += Circle.P[i].gasGiven() - Circle.P[i].distanceToNextPoint();

   if (currentSum < worstSum) { worstSum = currentSum; indexWorstSection=i-numberPointsWorstSection; startingPoint=i;}

   if (currentSum>0) { currentSum=0; } 
     else { numberPointsWorstSection++; }

   if (i==(Circle.numberOfPoints-1)) { i=0; }
     else { i++; }
   }

if (indexWorstSection<0) indexWorstSection=Circle.numberOfPoints+indexWorstSection;

你之所以不能让一个for循环,因为最糟糕的部分可能是,例如,从i =(Circle.numberOfPoints -2)到i = 3。如果currentSum在零,它的必须的继续回到了数组的开始。

The reason why you can't make it a for-loop is because the worst section might be, for example, from i=(Circle.numberOfPoints -2) to i=3. If the currentSum is under zero, it must continue back at the start of the array.

没有试过code和没有做任何严重的编程将近十年。很抱歉,如果它有一些缺陷。你会毫无疑问,要清理这个了一下。

Haven't tried the code and haven't done any serious programming in almost a decade. Sorry if it has some bugs. You will no doubt have to clean this up a bit.