Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all 39796 articles
Browse latest View live

LRQUER Subtask 2.

$
0
0

Are there no max-tests in LRQUER 2nd subtask? The most naive $O(NQ)$ solutions passed on it. See this, for example.

@admin@vijju123


Further schedule of CCDSAP??

$
0
0

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:

  1. Will there be a scholarship for Jan 21 exam date?
  2. If Yes, what will be the selection criteria? December long challenge or January Long challenge or Both?
  3. And, will the previously selected candidate will be eligible to be selected again?

Waiting for your reply. :)

@admin@vijju123

Help in L-R Queries

$
0
0

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.

Discrepancy in Nov long

Discuss your Approach November Lunchtime

$
0
0

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. :)

ZCO 2018 discussion

$
0
0

Personally I think the question were a little harder than previous years. I managed to finish in 2hrs.
First problem was prefix sums+ maths
Second was greedy + implementation

PERMEXIS - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

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:

setter
tester

Whats the next procedure after selection for Chennai Onsite? Where to confirm our participation and where to make payment ?

$
0
0

Please post any updates you get here.


Help in debugging code in Python

$
0
0

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]

$
0
0

Problem Statement:

Link

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.

  1. '(' --> Count = 1
  2. '(' --> Count = 2
  3. solution[0] = 'a'
  4. stack <-- 'minus' symbol
  5. solution1 = 'b'
  6. Encounter ')', count = 1, push stack top item into soln.
  7. solution2 = stack(top item)
  8. encounter symbol 'multiplication' <-- push to stack
  9. '(' --> count = 2
  10. steps 3 to 5 repeated somewhat [the division sign: c/d was pushed to stack]
  11. encountered ')' remove top item from stack '/' and push to solution[5] & count = 1
  12. 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.
  13. 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

$
0
0

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 :)

Payment Issue

$
0
0

Sadly, I am facing a dilemma.

I made a big mistake and the payment for my team is still not done. I was busy with my majors and I tried doing the payment today and the SBI account didn't update the beneficiary and long story short I still have not done the payment.

What I didn't consider was tomorrow was the last Saturday and NEFT will go through only on Monday.

What should I do now? I have mailed AmritaPuri and is there any chance that I will get a extension till Monday?

(I know this is a big *** up but any help will be highly appreciated)?

ONCHESS - Editorial

$
0
0

Problem Link

Practice
Contest

Setter:Hasan Jaddouh
Tester:Amr Mahmoud
Editorialist:Bhuvnesh Jain

Difficulty

EASY

Prerequisites

Looping Techniques

Problem

Find a suitable match for every person depending on some constraints mentioned in the statement.

Explanation

The solution is simply doing what the problem statement says. Just maintain a "wait" variable for all players, which when set to $1$ means that the player is still waiting for his chance to pair up with someone, and $0$ meaning that the person is already paired up.

Thus, for every player, we start from the first person and proceed in increasing order of their times of arrival and break as soon as we get some matching person whose "wait" variable was set to "1".

To check if any person matches with someone else, we can simply use "if" statements to check the conditions mentioned in the statement.

  1. The first condition can be checked as follows : $y <= x \\text{ and } x <= z$, meaning that $x$ lies in the range $[y, z]$
  2. The second and third condition can be simply checked by comparing the 2 values i.e. time and "rated/unrated" criteria.
  3. For last condition, we first check to which category it belongs i.e. $\\{ \\text{random, black, white} \\}$. Then as per the category, we do the corresponding check for the other player i.e. "random" matches with "random", "black" with "white" and "white" with "black".

Time Complexity

$O(N^2)$, per test case.

Space Complexity

$O(N)$

Solution Links

Setter's solution
Editorialist's solution

ADJLEAF - editorial

$
0
0

Problem Link

Practice
Contest

Setter:Hasan Jaddouh
Tester:Amr Mahmoud
Editorialist:Hasan Jaddouh

Difficulty

HARD

Prerequisites

Dynamic programming on trees, combinatorics

Problem

Given a tree, for each node i make it the root of the tree then count the number of subsets of leaves that it's possible to be visited next to each other when doing dfs from the root (ignoring nodes that are not leaves)

Explanation

solution for a single root

Let's root the tree at node 1 and count the number of good subsets of leaves, we will use dynamic programming technique.

$dp_{i,0}$ means the number of good subsets in subtree of node i excluding empty subset and subset that consists of all leaves in the subtree (this is to avoid repetition in counting)

$dp_{i,1}$ means the number of good subsets in subtree of node i that also appear as prefix in one of the dfs sequences (let's call it good prefix subset), again excluding empty subset and subset that consists of all leaves in the subtree

Now suppose we have already calculated dp values of children of node i, how can we use them to calculate the dp values for node i itself?

first thing we notice that all those subsets of leaves that their LCA is a decedent of node i are already counted in one of children of node i so in the end we only add do values of the children to node i, so we only care about subsets of leaves that their LCA is node i

note that when we do dfs from node i we can select the order of visiting the children, but once we visited one child of node i we can't start visiting another child of node i before we visit all leaves in subtree of the first child, so for a subset of leaves to be good then for each child of node i either all leaves in its subtree are included in the subset or none of them are included, except for at most two children because they can be a prefix and suffix in the sequence of visiting the good subset.

From what is said above we can have some insight about how to calculate the dp values, suppose C is the number of children of node i, we have the following cases of good subsets

  • for each non-empty subset S of children of node i, if we take all leaves that are in subtree of one of the children in S then it will be good subset of leaves, this also can be "good prefix subset" (we defined this above) so we add the count of this case to both $dp_{i,0}$ and $dp_{i,1}$. the number of subsets of children of node i is $2^{C}$ we should subtract 2 because we don't want to include the empty subset and we don't want to include the full subset (remember we excluded it in our definition of $dp_{i,0}$ and $dp_{i,1}$ ).

  • for each child $u$ of node i and a subset S of other children, if we take all leaves that are in subtree of one of the children in subset S in addition to some but not all leaves in subtree of u such that those leaves make good prefix subset then all those leaves will form a good subset of leaves and it also can be a prefix so we add the count of this case to both $dp_{i,0}$ and $dp_{i,1}$. to count the number of subsets of this case we should add values of $dp_{u,1}$ where u is child of i and then multiply it by $2^{C-1} -1$, why C-1 instead of C? because we can't include u itself, why -1 in the end? because we don't want to include the empty subset of children otherwise this will make the resulting subset of leaves have descendant of node i as their LCA and lead to repetition of counting.

  • for each pair of different children u, v of node i and a subset S of other children, if we take all leaves that are in subtree of one of the children in S and some but not all leaves in subtree of u such that those leaves make good prefix subset and some but not all leaves in subtree of v such that those leaves make good prefix subset then all those leaves will form a good subset of leaves, but they can't be good prefix subset so we only add this count to $dp_{i,0}$, to count the number of subsets in this case we should add all values of $dp_{v,1} * dp_{u,1}$ where u and v are different children of node i after that we multiply the result by $2^{C-2}$ because this is the number of subsets of other children of node i.

in the end the answer is $dp_{1,0} + 1$, we add 1 to include the subset of all leaves again.

This solution only requires a single dfs to calculate all dp values so its complexity is O(n)

solution for all roots

Let's start by rooting the tree at arbitrary node (say node 1) and apply the previous solution on it. Now consider a node that is not a root, it have a number of children (say C) and one parent, we calculated the dp values of this node from its C children, what would have been different if this node was the root? simply all its children are still the same but the parent will become a child of this node making this node have C + 1 children, so all we have to do is to calculate for each node what would have the dp values of its parent be if this parent was a child instead, and then calculate the dp values of all nodes again taking into account the new values from the parent, how to this exactly?

first of all, we do dfs from the the node we rooted the tree at (say node 1) and calculate dp values for all subtrees exactly the same in "solution for a single root" paragraph, after that we should do another dfs from the same root, but this time we should allow dfs function to have 2 more parameters that we will use for passing the dp values of the parent to its children, inside the dfs function first we should calculate again the dp values of the current node including the dp values that we got from the parameters, after that we will get the final answer for this node,note that calculations are done exactly the same way we did in first paragraph, after that we should calculate the parameters to pass to the children of current node, note that those parameters will be different for each child, and it's also done in same way but only considering only dp values of all other children in addition to the dp values of the parent.

using this method we only need to do two dfs traversal on the tree, thus the whole complexity is O(n)

Time Complexity

$O(N)$, per test case.

Solution Links

Setter's solution

Why is there no solution visible for ICO 2018 Preparatory Series ?


Is there an easier method to solve this?

How to solve ADJLEAF ?

Solving SEACO with normal segment tree

ITERATIVE method instead of RECURSIVE one to solve SPOJ GSS1 question

$
0
0

I'm trying to solve the spoj GSS1 using an iterative implementation of segment trees instead of a recursive approach. I have successfully built the segment tree but I'm finding it difficult to code the query() method using an iterative approach! Can anyone help in implementing the code using an iterative approach?Link to my code

Company Hiring

$
0
0

Found this question on web

An array consists of number of employees required for each month. ‘Hire’ is the cost of hiring an employee and ‘serv’ is the cost of removing an employee and ‘sal’ is the cost of retaining an employee for a month.Cost for each month will be (no_hires * hire + no_fired * serv + no_retain * sal) Given, the requirement for any month will strictly lie between 1 and 41. What is the minimum possible total cost for the ‘n’ months.

Link:https://stackoverflow.com/questions/47032975/solution-company-hiring

How to start ,i think there are many possibilities here ,How to approach??

Viewing all 39796 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>