Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need some ideas (not solutions) on how to go about solving the following problem.
So there's a guy who need to get to the phones on the top right hand corner of the building. It is guaranteed they will be at that location. We have to find the shortest path he can take to get there. See the image for an example.
The first thing that came to mind was using Dijkstra's algorithm. However, I have been told that it is unnecessary and that there are simpler alternatives.
On another note, if it is a last resort option, I wouldn't mind using Dijkstra's algorithm if someone can guide me through it. I have not much prior knowledge of graph theory, although I'm competent in the language I use (C++).
Explanation:
He takes the escalator from the left side of the ground floor to the right side of the first floor (14 seconds);
He sprints across the first floor from the right side to the left (5 seconds);
He takes the escalator from the left side of the first floor to the right side of the second floor (13 seconds);
He takes the escalator from the right side of the second floor to the left side of the top floor (11 seconds);
He runs from the left side to the right side of the top floor (5 seconds) to claim the phone!
The total time from the front doors to the sales desk is 14+5+13+11+5=48 seconds.
The input will be specified in the following format:
Each line will each contain three integers l f r separated by single spaces, where l represents the number of seconds required to travel from the left-hand side of the current floor to the right-hand side of the floor above, f the number of seconds to run from one side of the floor to the other, and r the number of seconds to travel from the right-hand side of the floor to the left-hand side of the floor above.
Example input:
14 10 15
13 5 22
13 7 11
5
This is a DP question.
f[n][left] = min{f[n-1][left] + time[go_from_left_to_right_on_floor_n-1] + time[escalator_from_right_side_up],
f[n-1][right] + time[escalator_from_right_side_up]}
f[n][right] = min{f[n-1][right] + time[go_from_right_to_left_on_floor_n-1] + time[escalator_from_left_side_up],
f[n-1][left] + time[escalator_from_left_side_up]}
If you want to go to third floor, left side, you can either start from the second floor, left side, go to right side, and take the escalator; or start from the second floor, right side, take the escalator. Choose the way that you use the minimum of time, and keep doing this use a loop(or recursion :) )
The solution should be O(n)
If the problem stays as small as the one you posted, I guess choyukchow's answer will work.
But if the problem gets considerably big, you may want to use heuristics. Example here.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I would appreciate it if someone can assist with a code to calculate the "specific number of heatwaves days where relative humidity > 66% and < 33%".
(whereas, a heatwave event is defined as one in which temperatures exceeded the 90th percentile of the daily mean temperature for at least three consecutive days, respectively).
Ok well here is a solution
# temperature percentile
cdo timpctl,90 infile -timmin infile -timmax t2m.nc t2m_pcen90.nc
# mask the temperature
cdo ge t2m.nc t2m_pcen90.nc mask.nc
# Need to make sure we have three consecutive days
cdo --timestat_date last runmean,3 mask.nc mask3.nc
cdo gec,1 mask3.nc heatwave_T.nc
# Now filter for dry heatwaves, assuming RH is %, change X if fraction
cdo lec,33 rh.nc rhdry.nc
cdo mul heatwave_T.nc rhdry.nc heatwave_dry.nc
# and same for wet
cdo gec,66 rh.nc rhwet.nc
cdo mul heatwave_T.nc rhwet.nc heatwave_wet.nc
Each file should have a 1 in it for each location/time when you are in a heatwave according to your definition. Of course the metadata is appropriate for T2m not the index, use NCO to change that if required. I have several video guides that would help with this question, the key one being the one on masking (it doesn't include the running mean part though). Note also that the RH criterion is applied ONLY on the day (no running mean) but that is how you write the definition in your question. Duplicate the running mean part if needed.
ps: In general it is good to show that you have attempted a solution yourself, before asking, SO guidelines are that questions are of a debugging nature, or can be a request for a one-liner, but not coding requests like "write me a code that does X or Y" - I think that is why you were getting downvoted.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Over view
I am trying to create a game that would need a pseudo-random function of the following characteristics:
Each player would be allotted a variable (of type float) for their money, the products (of type int*) they own and would want to sell and another variable to show what place they are in.
Each player would have the right to sell any of their products they own to any other player; and in addition the game involves a computer player which represents the other citizen population of that place who would want to buy the products or sell certain products.
Each player would have a different place value and each place would have different different cost for each products and the "transport cost" between different places also differs as a function of time.
A brief data structure of a player's profile would is as follows:
typedef struct PLAYER{
char *name;
int place;
int *products;
int noOfProducts;
float money;
PLAYER();
PLAYER(&ply);
.
.
.
}*ptrPLAYER;
Now I need a pseudo-random function that sets the rate of each product, rate of transport from one place to the other, and the function should also determine what products should the "citizens" player buy or sell and at what rate. The values provided by this function should follow a particular trend for some time and change its trend completely, but the variance should not be high. characteristics of the pseudo function:
Should be the function of the previous values generated and time.
Should show a gradual change following some trend up to some point.
The question:
The random function should have the following properties:
The variance should be low.
If we define the function as int *randomFnc( int previousResult[]) the function should follow some trend being very difficult to crack and at the same time, it should not reach a saturation point where beyond that point, the randomFnc(.) provides the same result.
The random function should generate a set of values (for example 100 values) which are directly dependent upon the previously generated values in some unique way.
You can model your process. As a starting point you can choose autoregressive model of type AR(1):
y_t = a_0 + a_1 * y_(t-1) + e_t (1)
where y is your variable of interest, e_t is gaussian white noise with mean 0 and variance sigma_e_t^2 and a0, a1 are constants.
should be the function of the previous values generated and time.
You can generate number of N of white noise assuming you will need N values of y. Then as you can see, value of y in time t is directly dependent on previous value y_(t-1) and you can always switch between two based on your array of e_t and relation (1).
the variance should be low.
The unconditional variance of y_t is
var(y_t) = sigma_e_t^2 / ( 1 - a_1^2) if a_1^2 < 1
so you can make it as small as you would like ( in range [sigma_e_t^2, +inf)) .
This process may looks like
You can add trend to make it y_t = a_0 + a_1 * t + a_2 * y_(t-1) + e_t and it will looks something like
More details are needed to be added in your question in order to advise you how to adjust this process further being subjected to other restrictions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having a hard time understanding the minimax or maximin problem from this wikipidea. The thing I can't understand is, what does the problem want? Does it want the shortest path from one node to another? If not this, than what? what is minimum-weight or maximum weight? a clarification with example would be very much helpful.what I exactly want is that what is minimum of maximum weight? I don't understand relation between minimum and maximum.
Explanation through an example: (derived from Wikipedia example)
The minimax path between Maldon and Feering is in red.
Here, the maximum between all the edges is 9.
max(8,9,7,8,9) = 9
There is no possible path where the maximum of all the edges is less than 9.
Note that this is not the shortest path, the shortest path would be the direct path between the two, with a cost of 10, but 10 > 9, so that would not be the minimax path.
The problem is to find a path where the minimum edge on the path is as large is possible.
So, if you have a path P = e1, ..., ek, where ei is the edges weights, let f(P) be min(e1, ..., ek). You should find a path P* so that f(P*) is as high as possible.
Another possible interpretation of the problem: find a maximal weight W such that if you remove every edge with weight < W from the graph, there still will exist a path from source to target. In this case W = f(P*).
I have been scouring the internet for quite some time now, trying to find a simple, intuitive, and fast way to approximate a 2nd degree polynomial using 5 data points.
I am using VC++ 2008.
I have come across many libraries, such as cminipack, cmpfit, lmfit, etc... but none of them seem very intuitive and I have had a hard time implementing the code.
Ultimately I have a set of discrete values put in a 1D array, and I am trying to find the 'virtual max point' by curve fitting the data and then finding the max point of that data at a non-integer value (where an integer value would be the highest accuracy just looking at the array).
Anyway, if someone has done something similar to this, and can point me to the package they used, and maybe a simple implementation of the package, that would be great!
I am happy to provide some test data and graphs to show you what kind of stuff I'm working with, but I feel my request is pretty straightforward. Thank you so much.
EDIT: Here is the code I wrote which works!
http://pastebin.com/tUvKmGPn
change size to change how many inputs are used
0 0
1 1
2 4
4 16
7 49
a: 1 b: 0 c: 0
Press any key to continue . . .
Thanks for the help!
Assuming that you want to fit a standard parabola of the form
y = ax^2 + bx + c
to your 5 data points, then all you will need is to solve a 3 x 3 matrix equation. Take a look at this example http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html - it works through the same problem you seem to be describing (only using more data points). If you have a basic grasp of calculus and are able to invert a 3x3 matrix (or something nicer numerically - which I am guessing you do given you refer specifically to SVD in your question title) then this example will clarify what you need to do.
Look at this Wikipedia page on Poynomial Regression
For a school project, I have a simple program, which compares 20x20 photos. I put 20 photos, and then i put 21th photo, which is compared to existing 20, and pops up the answer, which photo i did insert (or which one is most similar). The problem is, my teacher wanted me to use nearest neighbour algorithm, so i am counting distance from every photo. I got everything working, but the thing is, if photos are too similar, i got the problem with saying which one is closer to my one. For example i get these distances with 2 different photos (well, they are ALMOST the same):
0 distance: 1353.07982026191
1 distance: 1353.07982026191
It is 15 digits already, and i am using double type. I was reading that long double is the same. Is there any "easy" way to store numbers with more than 15 digits and do math on them?
I count distance using Euclidean distance
I just need to be more precise, or thats limit i probably wont pass here, and i should talk to my teacher i cant compare such similar photos?
I think you need this: gmplib.org
There's a guide how to install this library on this site too.
And here's article about floats: http://gmplib.org/manual/C_002b_002b-Interface-Floats.html#C_002b_002b-Interface-Floats
Maybe you could use an algebraic approach.
Let us assume that you are trying to calcuate if vector x is closer to a or b. What you need to calculate is the sign of
d2(x, a) - d2(x, b)
Which becomes (I'll omit some passages for brevity)
and then
Which only contains differences between values which should be very similar. Summing over such small values should yield a better precision than working on the aggregate.