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

MCO16406 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

MEDIUM

PREREQUISITES:

SQRT DECOMPOSITION, LINK-CUT TREES

EXPLANATION:

For subtask $1$, we can just apply the $O(nq)$ brute force solution. For subtask $2$, since the state of VPS doesn't change for each node, we may just do a simple tree dp to compute the sum of values in each subtree to answer all queries in $O(1)$.

We'll directly solve the full problem. Let's compute the start and end time of dfs of each node. Now, we will work on the linearized tree (each node appears twice in the linearized tree). For each node, we assign a value to it which is initially $0$. Whenever we switch on the VPS of a node $u$, we increment the value of all nodes in the subtree (which is now a subsegment of the array) by $1$ and similarly if we switch it off we decrement the value of all nodes in the subtree by $1$. It is now easy to see that the nodes that will be infected if the virus starts spreading from a node $u$ is precisely the nodes in subtree of $u$ with value $0$. Thus, we only have to maintain this info in the array efficiently.

One way to do this is by sqrt decomposition. Divide the array into $\sqrt{n}$ blocks. For each block, store $add[i]$ and $cnt[i][j]$, which we will describe later. $cnt[i][j]$ will store the number of animals in the block which have the value of node equal to $j + add[i]$. With this information, each update can be done easily. The only thing that matters is adding (or subtracting) an entire block by $1$. This is where the add array comes handy. We can just increment the add array by $1$ or $-1$ instead of iterating through the whole block again. Answering each query is also the same. To find the answer for a whole block, just refer to the cnt array. Thus, this solution will work in $O(n\sqrt{n})$ time.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:


RunTime Error

$
0
0

Hey I am using Python 3 its giving me run time error, but the code works fine in IDLE.

Long challenge sept 2017

$
0
0

Admins are not replying to my query in comment section of third problem ...please anyone help me by replying.

not getting my rank in September challenge

$
0
0

I have solved a problem in the contest but my rank is not in rank list. It says "You did not solve a problem in this contest." It is not showing my rank even after a long time.

topicwise practise

$
0
0

How can I code for a specific topic( like linked list, arrays etc.) in codechef ?

"I want to ask a question" - Ask them all here!

$
0
0

Hello guys,

UPDATE- This thread is now for people who are unable to ask questions due to low-karma/losing-karma or time limit.

As you all are aware, many times there are genuine users who wish to ask a question, but dont have karma for it. They then, have to resort to means of either asking their question in other threads, or request someone to upvote them so they can ask a question.

As we have seen last month, some fake accounts were used in malpractices in discuss, and they also gained karma by "I want to ask a question" thing. Hence, now it is becoming increasingly difficult to differentiate between genuine users, and fake users.

This thread is my solution for it. Any user, with less than 3 karma who wants to ask a question is requested to ask it here, and if the question is upto standards (meaning it needs more than those "one line answers") then we (at least I ) will convert your "answer" here into a question.

In short, post your question here, and if its a good genuine question, it will get posted as a separate question by using "Convert to question" feature.

In case your question required only 1 line answers or such, we would comment it on your question.

You guys are requested to direct any such guy (who wants to ask a question) here and not "free-upvote" them, so that we can avoid any duplicate/irrelevant questions (as questions posted here would be verified first, before converting to question).

Note to @admin - We will require your active participation and help in events related to deletion of any spam content here. Also, since only admins are ones who could delete answers, it is requested that you keep an eye out on this thread.

With Regards

Vijju123

Runtime Error NZEC

Sorting In an array

$
0
0

How to sort Element in array For Example arr[] = {"Code1","Code10","Code2","Code20",code30}; output should be code1,code2,code10,code20,code30 but when i use arrays.sort in java it gives the output code1,code10,code2,code20;


CHEFPRES - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Kamran Maharov
Tester 1:Minako Kojima
Tester 2:Shiplu Hawlader
Editorialist:Pawel Kacprzak

DIFFICULTY:

MEDIUM

PREREQUISITES:

Trees, dfs

PROBLEM:

You are given a tree of N nodes and a special node B. Each node v has asocciated an unique integer, let's denote this integer by f(v). Your task is to provide an answer for each of q following queries. A single query consists of two integers, A and P. Let D(v) denotes the minimum distance between the unique path from A to v in the tree i.e. the minimum distance between A and any node on the path. The answer for a single query (A, P) is a node v for which f(v) = P and D(v) is minimum among all such nodes. If there is more than one such node, you should return the one with minimum number.

QUICK EXPLANATION:

Root a tree in B using dfs. During this dfs compute dp[v][c] := minimum node u in v's subtree for which f(u) = c or INF if there is no such node.

Using second dfs, for each v and c, compute ans[v][c] := dp[u][c] where u is the first node on the path from v to B for which dp[u][c] != INF or -1 if no such node exists

For each query (A, P), return ans[A][P].

EXPLANATION:

The method mentioned in quick explanation is straightforward, but let's take a look why it works.

Consider a single query (A, P). Let dist(u) be a distance from node u to the root of the tree, i.e. to node B. Let's assume that the result for this query is a node v. Then D(v) <= dist(A), because A is the first node of the path to v. Since we are looking for a v for which D(v) is maximal, the best thing we can do is to search for it in A's subtree, because if there exists a v in that subtree for which f(v) = P, then D(v) = dist(A) and we cannot do better. If there is no such v in A's subtree, we search (based on the same argument) for the next greatest value of D(v) in a subtree rooted in the parent of A. We continue this process unless we find v, for which f(v) = P or we reach node B. If we reach B and doesn't find a node v for which f(v) = P, the answer is -1.

In order to do that, we compute dp[v][c] := minimum node u in v's subtree for which f(u) = c or INF if there is no such node.

If implemented naively, that method has O(n^2) running time, which may pass here, but we can do better.

Using the second dfs, for each v and c, compute ans[v][c] := dp[u][c] where u is the first node on the path from v to B for which dp[u][c] != INF or -1 if no such node exists. This is similar to path compression in union-find data structure.

Using ans table, we can answer any query in O(1) time.

Time Complexity:

Time complexity is O(N * K) because for each K, we visit every edge a constant number of times during precomputational phase and we answer each query in constant time.

AUTHOR'S AND TESTER'S SOLUTIONS:

To be uploaded soon.

RELATED PROBLEMS:

To be uploaded soon.

regarding RE

$
0
0

it would be very helpful, for which test cases our code does not hold is informed to us, isn't it possible?

What is the naive way to solve (hint) the problem?

how can i solve this question in practice section ?

MTRXMOD - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Yuri Shilyaev
Tester:Hasan Jaddouh
Editorialist:Daniil Melnichenka

DIFFICULTY:

Simple

PREREQUISITES:

Absolute value equations.

PROBLEM:

Given a matrix $B$ of size $N \times N$, made by following rule: $B_{i, j} = |A_{i}-A_{j}|$. Find a lexicographically minimal sequence $A$ of $N$ numbers that could have been used to build matrix $B$ with $A_1 = 0$. Also answer $Q$ queries of changing a row and corresponding column of matrix to the new one.

QUICK EXPLANATION:

Let's restore the sequence in $O(N)$. Find a first non-zero element (let it be element number $i$) and assign it to $-B_{1, i}$. Now restore all the other elements using equations with $A_1$ and $A_i$.

The total complexity would be $O(N^2+NQ)$.

EXPLANATION:

Firstly let's maintain matrix $B$. Besides it's easy to see the following fact. Suppose we have some array $A$ that is suitable for current matrix $B$. Let's replace all elements in $A$ with the additive inverses. Obviously, the new array fits matrix $B$.

Let's do the following after every query. We know that $A_1$ is equal to 0. Let's find the smallest $i$ that $B_{1, i}$ is not zero with a simple loop. Evidently all $A_j$ ($j$ < $i$) are equal to 0. Now we should take advantage of a fact that was mentioned above and assign $A_i = -B_{1, i}$ as we want to get the lexicographically smallest answer. After that let's make a loop for all $j$ ($i$ < $j$ $\le$ $N$). Easy to note that we have a system of equations:

$|A_i - A_j| = B_{i, j}$
$|A_1 - A_j| = B_{1, j}$

where $A_1 \neq A_i$ and all the values are known except $A_j$. Now we should just solve this system of equations and find out $A_j$.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

String class

$
0
0

I am using bufferedreader instead of scanner but on the time of submission it is showing NZEC error.And this running properly on my ide.

taking input in a list in python

$
0
0

how to take an input which contains elements seperated by spaces into a list in python3.5


Unexpected behaviour of the codechef platform ..

wrong answer but right output ? Rainbow problem

$
0
0

include<stdio.h>

int func(int [],int); int n; int main() { int cas,n,a[100],yes[100],i,j; scanf("%d",&cas); if(cas<1||cas>100) goto x; for(i=0;i<cas;i+=1) { scanf("%d",&n);

for(j=0;j<n;j++) { scanf("%d",&a[j]);

} yes[i]=func(a,n-1);

} int g; for(g=0;g<cas;g++) { if(yes[g]==1) printf("yes\n"); else if(yes[g]==0) printf("no\n"); } x: return 0; }

int func(int a[],int n) { int i,j; for(i=0,j=n;i<j;i++,j--) {="" if(a[i]!="a[j])" return="" 0;="" else="" if(a[i+1]-a[i]="">1) return 0; else if(a[i]<0) return 0; else if(a[i+1]=a[i]<0) return 0; else if(a[1]<1||a[i]>7) return 0; } return 1; }

Increment/Decrement Operator

$
0
0

Programming in C: I have run the following code: .. int I = 5; printf("%d,%d,%d",I++,I,++I); ..

The output I expected was 667.After running this code it was 677. Could anyone explain this?

here`s my code in c. It`s giving me a wrong answer. could u explain why?

Header files included in bits/stdc++.h

$
0
0

I recently came across bits/stdc++.h . However, it turns out that it isn't there on OSX. Can someone please share the contents of the file with me, so that I can add this header file manually?

Viewing all 39796 articles
Browse latest View live


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