algorithm

Fastest algorithm to hop through an array

眉间皱痕 提交于 2021-02-06 08:51:54
问题 Start with an array A of positive numbers. Start at index 0. From index i, you can move to index i+x for any x <= A[i]. The goal is to find the minimum number of moves needed to get to the end of the array. Here's an example: { 2 , 4 , 1 , 2 , 3 , 2 , 4 , 2} If you always go as far as possible in each move, then this is what you get: 0 , 2 , 3 , 5 , 7 This takes 4 moves. But you can get through it faster by doing it this way 0 , 1 , 4 , 7 This only takes 3 moves. I thought about this for a

Is there a nice way to assign std::minmax(a, b) to std::tie(a, b)?

天涯浪子 提交于 2021-02-06 06:29:11
问题 std::tie(a, b) = std::minmax(a, b); I think this is intuitive code. Clean and understandable. Too bad it doesn't work as intended, as std::minmax templates for const& . If therefore the values are swapped inside the std::pair<const&, const&> than one assignement will overwrite the other value: auto[a, b] = std::make_pair(7, 5); std::tie(a, b) = std::minmax(a, b); std::cout << "a: " << a << ", b: " << b << '\n'; a: 5, b: 5 The expected output here is a: 5, b: 7 . I think this is important as

Algorithm for a geodesic sphere

南笙酒味 提交于 2021-02-06 05:39:55
问题 I have to make a sphere out of smaller uniformely distributed balls. I think the optimal way is to build a triangle-based geodesic sphere and use the vertices as the middle points of my balls. But I fail to write an algorithm generating the vertices. Answer in C++ or pseudo-code will be better. Example of a geodesic sphere: http://i.stack.imgur.com/iNQfP.png 回答1: Using the link @Muckle_ewe gave me, I was able to code the following algorithm: Outside the main() class Vector3d { // this is a

Algorithm for a geodesic sphere

风格不统一 提交于 2021-02-06 05:38:09
问题 I have to make a sphere out of smaller uniformely distributed balls. I think the optimal way is to build a triangle-based geodesic sphere and use the vertices as the middle points of my balls. But I fail to write an algorithm generating the vertices. Answer in C++ or pseudo-code will be better. Example of a geodesic sphere: http://i.stack.imgur.com/iNQfP.png 回答1: Using the link @Muckle_ewe gave me, I was able to code the following algorithm: Outside the main() class Vector3d { // this is a

Algorithm for a geodesic sphere

↘锁芯ラ 提交于 2021-02-06 05:36:06
问题 I have to make a sphere out of smaller uniformely distributed balls. I think the optimal way is to build a triangle-based geodesic sphere and use the vertices as the middle points of my balls. But I fail to write an algorithm generating the vertices. Answer in C++ or pseudo-code will be better. Example of a geodesic sphere: http://i.stack.imgur.com/iNQfP.png 回答1: Using the link @Muckle_ewe gave me, I was able to code the following algorithm: Outside the main() class Vector3d { // this is a

Closest Pair Implemetation Python

為{幸葍}努か 提交于 2021-02-06 04:52:24
问题 I am trying to implement the closest pair problem in Python using divide and conquer, everything seems to work fine except that in some input cases, there is a wrong answer. My code is as follows: def closestSplitPair(Px,Py,d): X = Px[len(Px)-1][0] Sy = [item for item in Py if item[0]>=X-d and item[0]<=X+d] best,p3,q3 = d,None,None for i in xrange(0,len(Sy)-2): for j in xrange(1,min(7,len(Sy)-1-i)): if dist(Sy[i],Sy[i+j]) < best: best = (Sy[i],Sy[i+j]) p3,q3 = Sy[i],Sy[i+j] return (p3,q3,best

Closest Pair Implemetation Python

故事扮演 提交于 2021-02-06 04:51:57
问题 I am trying to implement the closest pair problem in Python using divide and conquer, everything seems to work fine except that in some input cases, there is a wrong answer. My code is as follows: def closestSplitPair(Px,Py,d): X = Px[len(Px)-1][0] Sy = [item for item in Py if item[0]>=X-d and item[0]<=X+d] best,p3,q3 = d,None,None for i in xrange(0,len(Sy)-2): for j in xrange(1,min(7,len(Sy)-1-i)): if dist(Sy[i],Sy[i+j]) < best: best = (Sy[i],Sy[i+j]) p3,q3 = Sy[i],Sy[i+j] return (p3,q3,best

Closest Pair Implemetation Python

坚强是说给别人听的谎言 提交于 2021-02-06 04:50:57
问题 I am trying to implement the closest pair problem in Python using divide and conquer, everything seems to work fine except that in some input cases, there is a wrong answer. My code is as follows: def closestSplitPair(Px,Py,d): X = Px[len(Px)-1][0] Sy = [item for item in Py if item[0]>=X-d and item[0]<=X+d] best,p3,q3 = d,None,None for i in xrange(0,len(Sy)-2): for j in xrange(1,min(7,len(Sy)-1-i)): if dist(Sy[i],Sy[i+j]) < best: best = (Sy[i],Sy[i+j]) p3,q3 = Sy[i],Sy[i+j] return (p3,q3,best

Efficient Packing Algorithm for Irregular Polygons

我们两清 提交于 2021-02-06 03:51:32
问题 I'm looking for a packing algorithm which will reduce an irregular polygon into rectangles and right triangles. The algorithm should attempt to use as few such shapes as possible and should be relatively easy to implement (given the difficulty of the challenge). It should also prefer rectangles over triangles where possible. If possible, the answer to this question should explain the general heuristics used in the suggested algorithm. This should run in deterministic time for irregular

Efficient Packing Algorithm for Irregular Polygons

ぃ、小莉子 提交于 2021-02-06 03:50:29
问题 I'm looking for a packing algorithm which will reduce an irregular polygon into rectangles and right triangles. The algorithm should attempt to use as few such shapes as possible and should be relatively easy to implement (given the difficulty of the challenge). It should also prefer rectangles over triangles where possible. If possible, the answer to this question should explain the general heuristics used in the suggested algorithm. This should run in deterministic time for irregular