单车道桥梁问题
如果您不熟悉这个问题,它类似于 这个。
我不是来寻求答案的,我其实已经写完了所有的代码。我刚刚发现我的解决方案并不能以最佳方式解决这个问题,因为我的解决方案一次只允许一辆车在桥上行驶。我希望我能得到一些关于如何使用 sem_wait 和 sem_post 来解决这个问题的提示。我希望让同一个方向的流量能够汇聚在一起,而不是一次一个。
我的解决方案目前看起来像这样:(
默认 sem_t 北和南 = 1 表示 1 辆车解锁)
如果 Northcar 则 sem_wait(south)、sem_wait(north)。过桥,然后是sem_post(北),sem_post(南)。这显然是错误的,因为它锁定了桥上除了桥上那辆车之外的所有车辆。我想让交通能够流动在一起。有什么想法吗?
我使用随机生成的流量,这增加了一些复杂性。
If you're unfamiliar with the problem, it's something like this.
I didn't come to ask for an answer, I have actually finished all of my coding. I have just found that my solution doesn't solve it the best way possible, because my solution only allows one car at a time on the bridge. I was hoping I could get some tips about how to go about using sem_wait and sem_post to solving this problem. I hope to allow traffic flowing the same direction to flow together and not one at a time.
My solution currently looks something like:
(default sem_t north and south = 1 for unlocked for 1 car)
IF northcar then sem_wait(south), sem_wait(north). Cross the bridge, and then sem_post(north), sem_post(south). This is obviously wrong because it's locking the bridge from all cars other than the one on it. I want to enable traffic to flow together. Any ideas?
I am using randomly generated traffic which adds a bit of complexity to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在现实生活中,您可以使用北红/南绿、北绿/南红或北红/南红的交通灯来解决这个问题,并在入口车道和出口车道上安装传感器桥的两端。
假设光线开始时为北红/南绿。汽车可以从南向北不停地行驶。当汽车从北方驶来时,它会在红灯处停下来并触发北方接近传感器。这使得光线向南偏红,但仍然向北偏红。当桥上当前的所有车辆都离开时(在离开时触发北出口传感器),指示灯会变为绿色北向。该状态一直持续到另一辆车从南方驶来并触发南方接近传感器。
考虑一下如何将其转换为代码。 (您需要使用信号量的计数属性。)
In real life you would solve this problem with a traffic light which is either red-north/green-south, green-north/red-south, or red-north/red-south, and sensors in the approach and exit lanes at both ends of the bridge.
Suppose the light starts out red-north/green-south. Cars can flow from south to north without stopping. When a car approaches from the north, it stops at the red light and triggers the north approach sensor. That makes the light go red-south, but it's still red-north as well. When all the cars currently on the bridge have left (triggering the north exit sensor on their way out) the light can change to green-north. That state persists until another car comes from the south and triggers the south approach sensor.
Think about how you'd translate that into code. (You'll need to use the counting property of semaphores.)
单车道高速公路
问题描述
一定数量的汽车正在通过单车道道路。所有汽车的速度各不相同。很容易看出,根据汽车的速度,将形成不同的组。
单车道道路不允许超车。给定汽车的速度,计算如果考虑所有可能的排列,可以形成多少个组。请参阅示例 1 以更好地理解。
打印组数除以排列数。
约束
输入
第一行包含一个
整数
N,表示车辆的数量。第二行包含 N 个空格分隔的
整数
,表示单个车辆的速度。输出
打印组数除以排列数,四舍五入到小数点后 6 位。
时限
1
示例 1
输入
3
输出
解释:
因此所有可能的排列为:
{10 20 30}
{10 30 20}
因此,这里共有 6 种排列,总组数为 11。
因此,输出为 11/6 = 1.833333
示例 2
输入
4
输出
解释:
所以这里一共有24种排列,
例如:
等等。组总数为 50。
因此,输出为 50/24 = 2.083333
Single Lane Highway
Problem Description
Certain number of cars are passing a single lane road. Speeds of all cars vary. It is easy to see, that depending on the speeds of the cars various groups will be formed.
Being a single lane road passing/overtaking is not allowed. Given speeds of cars, calculate how many groups can be formed if all possible permutations are taken into account. Refer to example1 for better understanding.
Print number of groups divided by the number of permutations.
Constraints
Input
First line contains an
integer
N, which denotes the number of vehiclesSecond line contains N space separated
integers
which denotes the speed of the individual vehicle.Output
Print number of groups divided by the number of permutations rounded upto 6 decimal places.
Time Limit
1
Example 1
Input
3
Output
Explanation:
So all possible permutations are:
{10 20 30}
{10 30 20}
So here there are total 6 permutations, and total number of groups are 11.
So, output is 11/6 = 1.833333
Example 2
Input
4
Output
Explanation:
So here there are total of 24 permutations,
For example:
So on and so forth. The total number of groups are 50.
So, the output is 50/24 = 2.083333
这是我刚刚创建的一些伪 python。基本上封锁一条路并让有限数量的汽车通过。根据您使用变量的方式,这可能是完全错误的:
This is some pseudo python I just whipped up. Basically block one way and let a limited number cars through. Depending on how you're using your variables, this might be totally wrong: