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 2 years ago.
Improve this question
I am trying to solve problem Uva-10128 (Queue) on UVa Online Judge. I am not able find a way to approach this problem. I searched on internet and found that most of people has solved this problem by precalulating using DP.
DP[1][1][1] = 1;
for(N = 2; N <= 13; N++)
for(P = 1; P <= N; P++)
for(R = 1; R <= N; R++)
DP[N][P][R] = DP[N-1][P][R]*(N-2) + DP[N-1][P-1][R] + DP[N-1][P][R-1];
Above code snippet is taken from https://github.com/morris821028/UVa/blob/master/volume101/10128%20-%20Queue.cpp.
Can someone please explain formula used in above code.
Thanks
When you calculate DP[N][P][R] you look at the position of the smallest person in the queue. Because he is the smallest, he can't block anybody. But he will get blocked if he doesn't stand at either end of the queue.
If he is the first person in the queue he is seen from the beginning of the line. So if we remove him, the queue contains N-1 people and you can only see P-1 people from the beginning, but still R people from the end. Therefore there are DP[N-1][P-1][R] combinations.
If he is in the middle, then by removing him we still can see P and R people. And since there are N-2 positions in the middle, there are DP[N-1][P][R] * (N-2) combinations.
And if he is the last person in the queue we get DP[N-1][P][R-1] combinations. The reasoning is identically to the first case.
So the total number of combinations for DP[N][P][R] is the sum of all three cases.
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 3 years ago.
Improve this question
I am doing competitive programming. I faced a problem with a question in which we need to find the number of ways to climb A stairs with 1,2 and 3 steps at a time. I solved this part using dynamic programming. The question has an additional condition too. That is we can jump 3 stairs only B times. I have attached my code. How do I solve this new part?
A = int(input())
B= int(input())
f = [1,2]
for i in range(3,A+1):
if B>0:
if i==3:
f.append(f[i-1]+f[i-2]+1)
else:
f.append(f[i-1]+f[i-2]+f[i-3])
B-=1
else:
f.append(f[i-1]+f[i-2])
print(f[-1])
Test case:
A=4
B=1
Output expected=7
I got 6
This can be solved using dynamic-programming. Below is breakdown of the process.
Let f(n,k) denote the number of ways of climbing n stairs with 1, 2 or 3 stairs at a time, but using at most k 3-steps.
Given n and k, we have three possibilities:
First step is a 1-step. We can climb remaining in f(n-1,k) ways.
First step is a 2-step. We can climb remaining in f(n-2,k) ways.
First step is a 3-step. We can climb remaining in f(n-3,k-1) ways.
So, we obtain the recurrence f(n,k) = f(n-1,k) + f(n-2,k) + f(n-3,k-1) for all n>=3.
The base cases will be f(0,j) = f(1,j) = 1 and f(2,j) = 2 for all 0<=j<=k.
Below is the corresponding code in C++:
int f(int n, int k) {
if(n<2) return 1;
if(n==2) return 2;
// dp is a 2D array of n+1 rows and k+1 cols
// dp[i][j] stores the result f(i,j)
vector<vector<int> > dp(n+1, vector<int>(k+1,0));
for(int j=0; j<=k; j++){
dp[0][j] = dp[1][j] = 1;
dp[2][j] = 2;
}
for(int i=3; i<=n; i++){
for(int j=0; j<=k; j++){
dp[i][j] = dp[i-1][j] + dp[i-2][j];
if(j>0) {
dp[i][j] += dp[i-3][j-1];
}
}
}
return dp[n][k];
}
In this case you need to add a dimension to the state description: "given how many steps you need to do and how many triple jumps you are allowed to do, how many different ways there are to climb the staircase?"
You cannot use just a single array for the problem, you need a 2D matrix with number of steps on one dimension and number of triple jumps in the other.
The the decomposition in subproblems is simple: when you do a 1/2 jump you just decrease the first index accordingly by either 1 or 2, when you do a triple jump (if allowed) you decrease the first by three and the second by one.
Written in the recursive/memoized version the code becomes (Python):
def ways(A, B):
if A < 3:
return max(A, 1)
try:
return cache[A, B]
except KeyError:
n = ways(A-1, B) + ways(A-2, B)
if A > 2 and B > 0:
n += ways(A-3, B-1)
cache[A, B] = n
return n
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 4 years ago.
Improve this question
this is how I can find sum of array.
p[i] - array of random integers, size 1000
sum = 0;
for (int j = 1; j < p.length; j++ )
{
sum = sum + p[j];
}
my question is how can I use multiple threads to perform it faster?
Simply:
int sum = Arrays.stream(p).parallel().sum();
This question already has answers here:
Big O, how do you calculate/approximate it?
(24 answers)
Closed 4 years ago.
I have been asked to explaine about Big O notation and to calculate Big o Notation for an algorithm. I'm done with the defining part but I'm still wondering how I can calculate it. Can someone help me to calculate the Big O for the below given code?
new = int (input("enter number" ))
if new <= 10000:
comm=new*2/100
print (comm)
else :
comm= new*5/100
print (comm)
Since there is no loop, it is O(1).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I just learned Python two days ago so this is probably very bad. Since i want to get better at optimization and organization, is there anything specifically bad here or that i could improve. Wasted lines of code, things done in a more memory intensive way than it could be and so on. Thanks a lot for any input and I'm looking forward to learn much more.
from random import*
b = 10
a = randint(1,b)
point = 1
x = 1
while x < 2:
print("Guess a number between 1 and ", b)
svar = int (input())
if svar == a:
b+=5
point= point+point
a = randint (1,b)
print("You have ", point, "points!")
elif svar < a:
print("Higher")
else:
print("Lower")
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
SO is warning me my question is likely to be closed, I hope they're wrong :)
My question: let you have a correlation matrix; you would like correlations which are next to 1 and -1 go towards 1, while those next to 0 stay there.
The simplest way is to use absolute values, e.g. if Rho is you correlation matrix then you will use abs(Rho).
Is there any way which is theoretically more correct than the one above?
As an example: what if I use Normal p.d.f. instead of absolute value?
Adjusted Rho = N(Rho, mu = 0, sigma = stdev(Rho))
where N is the Normal p.d.f. function.
Have you any better way?
What are strengths and weaknesses of each method?
Thanks,
Try this.
x <- runif(min = -1, max = 1, n = 100)
tr <- (x - min(x))/diff(range(x))
plot(x)
points(tr, col = "red")
You could also use a logit link function that guarantees the values to be between 0 and 1. But given that you're limited to values between -1 and 1, you would get only values in the range of ~[0.3, 1].