What is SIGFPE Runtime Error and how can it occur?
SIGFPE Runtime Error
NOV Lunchtime Unofficial Editorial (SHUFFL)
Problem : SHUFFL
Have you ever tried to solve Joshephus Problem. The problem is here : josephus. In joshephus problem we are basically removing 1 people each time by some rules indicated in the problem statement. Similarly for our problem we are removing 2 people each time again by rules depicted in the problem statement. There is a nice recursive solution for josephus problem which is here or you can look recursive equation in wiki page itself. Basically in josephus problem f(n) is written in terms of f(n-1). While in our problem we have write f(n) in terms of f(n-2). You can see the function getIndices() for knowing recursive equation : https://www.codechef.com/viewsolution/16372240
Main page bug
On the main page it shows December Lunchtime as upcoming contest, but the link (correctly) leads to December Challenge.
November Cook-Off rating calculation
Disjoint Set Data Structure recursive find_parent gets wrong answer
I was trying to solve "Almost Union Find problem" from Kattis. I used disjoint set to solve this problem. My problem is, I got wrong answer when I used the following recursive find_parent function in my code -
int find_p(int x){
if(par[x]!=x)
par[x] = find_p(par[x]);
return par[x];
}
But, the following find_parent function gets AC.
int find_p(int x){
int root = x;
while(par[root]!=root)
root = par[root];
int cn = x;
while(cn!=root)
{
int curp = par[cn];
par[cn] = root;
cn = curp;
}
return root;
}
Don't they do the same thing?
Here's my complete code,
GEEK04 - Editorial
Problem Link
Author:Bhuvnesh Jain
Tester:Bhuvnesh Jain
Editorialist:Bhuvnesh Jain
Difficulty
MEDIUM
Prerequisites
Recursion, Bitmasks, Dynamic Programming.
Problem
Find the minimum time taken to cross the river by $N$ people if we can allow almost 2 people to use a boat such that the rowing time is equal to the minimum of the rowing time of both the people. Note that, slowing rowing time is given by larger integers.
Explanation
First of all, any type of greedy algorithm will only pass the first subtask and not any other. As the constraints are small and greedy solution will not work, we will try to come up with a dynamic programming solution.
Let us assume we denote the side where all the persons are initially standing by left and their destination, i.e. where they want to reach by right.
The first observation is that we will always try to send 2 people in the boat. This is because the slower person rowing time will then not contribute to the answer. The next observation is that once few people have crossed the river and reached "right", the boat will be brought to the left side by the person whose rowing time is the minimum among the persons at the right side. Next is that we can represent each person by a "bit-mask". The "left" and "right" bitmask denote that if the ${i}^{th}$ bit is one, then the ${i}^{th}$ person is present there else he is not present. The last observation is that the person can be present either in the "left" side or "right" side, not both ways. Thus, we only care about "left" side masks. The "right" mask can be calculated from the "left" ones, by setting the bits to 1 which are not present in "left".
With all these observations, we try to build a recursive solution as follows: (in the code, "turn" means, whether we send people from right to left or reverse)
//call with recurse((2^n)-1, 0)
void recurse(LEFTMASK, turn):
if (LEFTMASK == 0):
return 0 //we transferred all the people
RIGHTMASK = ((2^n)-1) ^ LEFTMASK
if (turn == 1): //we want to transfer people to the left
min_row = infinity, person = -1
for i in [0 .. n-1]:
if (RIGHTMASK & (2^i)):
if (min_row > row[i]):
min_row = row[i]
person = i
return row[person] + recurse(LEFTMASK ^ (2^person), turn ^ 1)
else: //we want to transfer people to the right
if (number of set bits in LEFTMASK == 1):
//only 1 person is left)
for i in [0 .. n-1]:
if (LEFTMASK & (2^i)):
return row[i] + recurse(left ^ (2^i), turn ^ 1)
else:
//try every pair of people by sending them to right)
ans = infinity
for i in [0 .. n-1]:
for j in [i+1 .. n-1]:
if ((LEFTMASK & (2^i)) && (LEFTMASK & (2^j)):
val = max(row[i], row[j])
val += recurse(LEFTMASK^(2^i)^(2^j), turn ^ 1)
ans = min(ans, val)
return ans
To optimise the above code for the full score, we can use dynamic programming so that each state is visited only once. This is also known as "top-down DP" or "recursion with memoization".
Time Complexity
$O(2^N * N * N)$
Space Complexity
$O(2^N)$
Solution Links
Further schedule of CCDSAP??
I wish to know, as I'm sure many other users, about the further schedule of scholarships for CCDSAP for Jan 21 exam date.
My questions are:
- Will there be a scholarship for Jan 21 exam date?
- If Yes, what will be the selection criteria? December long challenge or January Long challenge or Both?
- And, will the previously selected candidate will be eligible to be selected again?
Waiting for your reply. :)
Help in L-R Queries
Why is this solution failing in first subtask even though I used long where it is needed. After that I used long for all the variables where they won't even overflow as they were given in constraint as 1<=Ai, Y<=10e9. The same solution with every data type set as long is passing here. Please explain.
Discuss your Approach November Lunchtime
Hello Guys...
This time I'm not posting any editorial because of an unavoidable reason (not being able to solve any problem complete :( ), but i still invite you all to share your approach for any of the four problems. This post has been marked community wiki, so you all will be able to make any changes (or write your own approach) on the post itself.
Sorry for inconvenience of missing editorials (will be back for next long challenge, hopefully)
And i hope you guys would like to share your approach as always. :)
LRQUER Subtask 2.
NOV LUNCH_TIME SMRSTR Unofficial editorial
problem: SMRSTR
Here we are given an array D and an array X
we have to calculate :
for i = 1..N:
X = floor(X / D[i])
for every value of Xi
The above statement can be rewritten as :
X = ( ( (X / D[1]) / D[2] )/ D[3] ) ... ) or
X = ( X /(D[1]*D[2]*D[3]*D[4]...D[N]) )
we can calculate :
p = (D[1]*D[2]*D[3]*D[4]...D[N])
as D[i] <= 10^9
so the value of p will overflow even for long long
Here we have to notice that Xi <= 10^9, so while calculating p we can check if it's value is going bigger than 10^9 or not
if yes than answer will be 0 as (X <= 10^9)
otherwise we can easily compute Xi/p
for this we can have a flag which will represent above two states
Therefore for every X we can compute the result in O(1)
and whole program will take O(Q)
If you didn't get it u can check my solution here
Help in 2nd problem of lunchtime
Can anyone tell me where this solution for 2nd problem is going wrong? https://www.codechef.com/viewsolution/16374117 p.s.- Its my friend's code and he asked me to debug it after contest but i failed too
My approach for SMRSTR(November Lunchtime)
Problem:- SMRSTR
It says that we have to optimize the code snippet given in the problem. We can observe that for every value in the X array we do the same thing by dividing it repeatedly by all the elements in the D array i.e we just divide every value in the X array by the product of all elements in the D array. So, we can proceed by finding this product only once. But according to the constraints the product results in a very big number. But we can observe that if product is greater than the largest element in X array, then answer becomes zero for all of them. So, we stop calculating the product when it crosses the largest element in X array. Since, largest element in the X array satisfies our constraints, we can ensure that our product doesn't overflow. If the product does not exceed the largest element of X array we simply find it and divide it with all the elements of the X array and print the result. This gives us a runtime of O(n) which is enough for this question.
If there is a better approach, please do comment it. Here is a link to my solution:- Click Here
If anyone did solve the second problem L-R queries, please explain me. Thanks.
Discrepancy in Nov long
@admin,@vijju123 rating of cookoff is updated from the ratings which we have got before plagarism check but shouldn't we actually get it from the new ratings after plagiarism checking.please check into the issue because rank predictor also shows we should get more in Nov long according to present ranklist if in Nov long http://codechef-rating-predictor.7e14.starter-us-west-2.openshiftapps.com/contest/NOV17/all
Is the Green tick for solved questions a bug?
Many times on the individual question page a green tick appears near the submit tab even though I never attempted it. Is it a bug?
PERMEXIS - Editorial
PROBLEM LINK:
Author and Editorialist:Lalit Kundu
Tester:Kamil Debowski
DIFFICULTY:
simple
PREREQUISITES:
logic, basic programming, sorting
PROBLEM:
You are given a list of integers $A_1, A_2, ..., A_N$. You have to rearrange them to get $B_1, B_2, ..., B_N$. Also, the conditions that $|B_i - B_{i + 1}| \le 1$ should be satisfied $\forall i < N$. You have to output if it's possible to do so.
QUICK EXPLANATION:
======================
If in sorted array $A$, any two adjacent elements differ by more than $1$, answer is NO
, otherwise YES
.
EXPLANATION:
================
Claim : If in sorted array $A$, any two adjacent elements differ by more than $1$, answer is NO
, otherwise YES
.
Proof: Let's assume there are two elements $l$ and $r$ in array $A$, where $r - l > 1$ and no other element lies between $l$ and $r$. We have to show that there doesn't exist a permutation of $A$ where adjacent elements don't differ by more than $1$, no matter what the other elements of array $A$ are.
We assume there exists such a permutation. Then, there are two cases:
- $l$ occurs before $r$ in this permutation. Since each adjacent element differs by atmost $1$, we can observe that we have to go from element $l$ to $r$ and in each step we can either remain at same value or increase by one or decrease by one, with the condition that new value should be present in array $A$. The only way to reach $r$ is through $r-1$(since $l$ occurs before $r$ in array), which can't be achieved since there is no such value in the array $A$.
- $r$ occurs before $l$ in this permutation. Since each adjacent element differs by atmost $1$, we can observe that we have to go from element $r$ to $l$ and in each step we can either remain at same value or increase by one or decrease by one, with the condition that new value should be present in array $A$. The only way to reach $l$ is through $l + 1$(since $r$ occurs before $l$ in array), which can't be achieved since there is no such value in the array $A$.
Pseudo code:
A = []
scan(T)
for test = 0 to T - 1:
ans = "YES"
scan(N)
for i = 0 to N - 1:
scan(A[i])
sort(A)
for i = 0 to N - 2:
if A[i + 1] - A[i] > 1:
ans = "NO"
print ans
COMPLEXITY:
================
$O(N \text{log} N)$, since we sort array $A$.
AUTHOR'S, TESTER'S SOLUTIONS:
Whats the next procedure after selection for Chennai Onsite? Where to confirm our participation and where to make payment ?
Please post any updates you get here.
Help in debugging code in Python
Hello folks,
I have implemented segment tree in python for this code.
It is running in some cases and for others abort is called in build function ( I checked that by debugging in Pycharm but I couldn't understand why code is misbehaving)
Any help would be appreciated :)
ONP - Editorial [Transform the Expression]
Problem Statement:
Difficulty: Cakewalk <--> Easy
Prerequisites: Concept of Stacks, Reverse Polish Notation
The Problem:
The problem finally boils down to:
- You are given an algebraic expression
- Operations over one-character variables
- All expressions are closed by '(' and ')' brackets.
- Keeping track of secondary expressions between one opening '(' and its corresponding closing ')'
- Thus tertiary expressions in between secondary brackets and so on..
- Use of basic mathematical operation symbols (+ - / * ^ %)
Explanation:
All variables used in the problem are of length=1. It is assured that expressions are of type:simple. Implies that there will be expressions only of the type a+b and not a+b+c.
Now, maintain a count. For your input expression to terminate, you have to come across a reverse bracket id est ')'. In other words, for every ( there has to be a ). Thus you can start by taking in a character which you are sure is '('. Maintain a count and increment it for every '(' that you find along the way and while this count is more than zero, you must take in and process the expression. Now, a '(' can be followed by either another ( or some variable, say 'x'. Let's say you have gone through one or more of these '(' and have now come to a variable. You shall then push this variable into a solution array of length [405].
After a variable, you will get an operator symbol. You have to sideline it temporarily for one step. In other words you will put it into a stack and pull it out after you take the next variable into consideration. Suppose the next variable was 'y' {initial was 'x', right?}. Let your expression be x#y where # is some operation symbol. After taking ing 'x' you expect a symbol and you push it into a stack. Then you expect a character y. You push the character into the solution array. And then comes the symbol. The brackets are to be completely ignored for our answer.
Now take a different case. Exempli gratia, ((a-b)***(c/d)). Now how will you cope with the multiplication sign which has no variables but two expressions to follow? This is why, a symbol must necessarily be pushed to the solution array only after encountering the ')' closing bracket. Now take this specific case according to how our algorithm will process it.
- '(' --> Count = 1
- '(' --> Count = 2
- solution[0] = 'a'
- stack <-- 'minus' symbol
- solution1 = 'b'
- Encounter ')', count = 1, push stack top item into soln.
- solution2 = stack(top item)
- encounter symbol 'multiplication' <-- push to stack
- '(' --> count = 2
- steps 3 to 5 repeated somewhat [the division sign: c/d was pushed to stack]
- encountered ')' remove top item from stack '/' and push to solution[5] & count = 1
- encountered another ')': now remember the multiplication symbol that we pushed.. Now take that out from stack as it is the topmost item in our stack currently.
- count becomes 0. terminate.
Let's see how our solution array looks like. ab-cd/*
General Algorithm:
- initialize count=0;
- input first character (which you know is a opening bracket '(' for sure)
count ++
while count is greater than 0
do:
- if next char is '(' count ++
- else if next char is {+ - / .. etc} then put into stack
- else if next char is ')' count -- and then pull out topmost item from stack and push to array.
- (now only case left is variable alphabet): else directly push char to solution.
Solution
Editorialist solution can be foundHERE
Editorial By@aalok_sathe
doubt in calculating expectations
i had recently learned expectation in math and trying to solve : https://www.codechef.com/problems/RRPLAYER
i read the editorial and able to understand it but i want to know where my approach is lacking? my approach :
` $E(x)$ = $((1 + E(x-1))*(1/n) + (2 + E(x-1))*(1/n^2) + ...........)*n$
i thought that if we can take any song from list in one attempt(prob : 1/n) and then have to choose (n-1) different songs + (if i choose that song in two attempt $(1/n^2)$ and then choose n-1 left songs $E(x-1)) + ........$ and finally i can choose any of the songs from the list as my current song(song that i have choosing 1 time+ 2 times+....) so, multiply my answer by n!
can anyone please explain where i am going wrong!
if possible please give some questions to practice :)