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

Suggestion-Show details of test cases passing etc. for Q with no subtasks

$
0
0

Hello guys!

As we all know, that whenever we try to solve a Q on codechef which has a subtask, we get a window like this-

alt text

But its not the case for problems without sub-tasks. In those problems we get a simple WA, TLE or green tick of correct answer.

I wish to suggest to add the feature of showing details of test cases for problems with no sub-tasks too. It would be of great help in debugging if we are able to see how many test-cases we passed/failed. Examples-

  1. A correct answer on sample cases, but failing every other test case would immediately hint at misinterpretation of problem or violation of output rule.

  2. Seeing your code failing on 1-2 test cases out of 15 would hint that you're missing a corner case, while seeing code fail on 14/15 test-cases hint at program being wrongly implemented/Q being misinterpreted. This is important because on receiving a simple cross and WA, programmer is left confused if its due to misinterpreting problem, due to failing in corner case, or complete wrong implementation of program altogether.

I think many of us would appreciate this feature! But I wish to know what you guys have to say, if you guys have anything to add-on or change. Please share your views :)


SPOJ-DQUERY TLE with MO's Algorithm

$
0
0

Here's the question DQUERY. I'm using MO's Algorithm and here's my code. Tried almost everything. fast i/o,print scanf,all other optimizations.

Any help please? Thanks in advance. :)

Chef and Weird Spider Editorial

$
0
0

Problem Link:https://www.codechef.com/problems/EXOCODE6link text

Author:https://www.codechef.com/users/vivek96

DIFFICULTY:Easy

PREREQUISITES:Conditional statements and loops,Basic Programming

PROBLEM:Chef School of Witchcraft and Wizardry is known for moulding young minds into the finest wizards and witches. However that comes with a lot of practice. One of the young wizards cast a spell on a spider that makes it leap greater distances.

On observing closely the spider leaped 1 meter in its first leap. 10 meters in its second leap, 19 meters in its third leap , 28 meters in its fourth leap, 37 meters in its fifth leap and so on following a pattern.

In order to capture the spider, the young wizard must be able to tell the number of leaps the spider would take to reach a given distance.

EXPLANATION:It is clear that leap length is increased by 9 in every leap,So Now we have to count the minimum number of leaps required to a cover a distance >=N,so we count minimum no of leaps by using while(true) loop because we dont know the number of iterations exactly required to find minimum no of leaps.

Output must be in format: Case #1: (answer)

AUTHOR'S AND TESTER'S SOLUTIONS:

include<iostream>

alt text

Edit-problem link updated

SPOj LVADER solution

$
0
0

Can someone tell me how to solve this problem. The typical DP approach won't work here because of the contraints

Explain logic and code

$
0
0

Problem link-- https://www.codechef.com/ABCS2017/problems/ABCS04

right approach?

Problem description: Geek Sundaram is a world renowned Master Chef. One of his signature dishes include Idly and Vada.

Each day he prepares M Idlies and N vadas and places them in a bucket. He asks his customers to pick two items from the bucket in whatever order they desire.

After picking two items, each customer is asked to place a vada back into the bucket if they had picked two similar items or else place an idly into the bucket.

At the end of the day, Geek is left with one item in the bucket. He is keen to find out what the last item could be since he doesn't know the order in which the customers picked and replaced.

Help Geek find the last item.

Print one of the following as the output:

"IDLY" - if the last item is idly. "VADA" - if the last item is vada. "SAMBAR" - if the last item is unknown.

EXTRAN - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Kamil Dębowski
Testers:Sergey Kulik
Editorialist:Pawel Kacprzak

DIFFICULTY:

Simple

PREREQUISITES:

Sorting, basic programming

PROBLEM:

We call a sequence of integers nice if and only if they can be arranged into a sequence of distinct consecutive numbers.

The problem can be described as the following process: initially, there was a sequence $B$ consisting of $N-1$ integers, which was a nice sequence. Then, number $X$ was inserted somewhere into $B$ to form a new sequence $A$. We know that $A$ is not a nice sequence, i.e. integers in $A$ cannot be arranged into a sequence of distinct consecutive numbers. The task is to for a given sequence $B$ found the number $X$, which was inserted into $B$ to form $B$. It is guaranteed that there exists one unique answer to the problem and $N \geq 3$.

QUICK EXPLANATION:

Sort the given sequence and notice that there is sufficient to check $3$ separated cases defining $X$.

EXPLANATION:

Subtask 1

In the first subtask we know that $N$ is at most $1000$, so one possible solution is to iterate over all numbers in $A$, and for each such number check if $A$ without that number is a nice sequence.

Let's assume that we want to check that $A$ without its $i$-th element, i.e. $A[i]$ is a nice sequence. If it is, then we know that the answer to the problem is $A[i]$, because the answer is guaranteed to be unique. In order to check if $A$ without $A[i]$ is a nice sequence, we can build a new sequence $C$ which have all elements from $A$ except $A[i]$, sort $C$ in ascending order and check if all two consecutive elements of sorted $C$ are consecutive integers. Notice that since it's guaranteed that the answer exists and it's unique, then there exists $i$ such that $A$ without $A[i]$ is a good sequence and it will be found by the above process because it checks all $i$. The time complexity per a single test case is $O(N \cdot N \cdot \log N)$, because for each of $N$ choices of $i$ we perform a sort of $N-1$ elements followed by a linear check over these elements, and it is enough to get accepted. This complexity can be slightly improved to $O(N^2)$ by sorting the input sequence at the beginning.

Subtask 2

In the second subtask, $N$ can be at most $10^5$, so the method used for the first subtask is too slow here.

Let's consider a sequence $B$, i.e. the sequence into $X$ was inserted to form $A$. The main observation is since we know that $B$ was nice, then there are just $3$ possible cases to check:

  1. $X + 1$ is smaller than the minimum element of $B$
  2. $X - 1$ is larger than the maximum element of $B$
  3. $X$ is equal to one of the elements of $B$

Notice that the above $3$ cases indeed cover all possibilities. In order to prove that, let $m$ be the smallest element of $B$ and let $M$ be the largest element of $B$. We know that $X$ cannot be equal to $m-1$, because then $A$ would be a nice sequence and we know it's not. Similarly, $X$ cannot be equal to $M+1$, because then $A$ would also be a nice sequence. Now, each integer except $m-1$ and $M+1$ falls into one of the above $3$ cases, so the only remaining thing to do is to check which of these cases is true. In order to do that, at the beginning we sort $A$. Then checking cases $1$ and $2$ is very easy - just compare two smallest elements and two largest elements of $A$. To check if the third case is true, we can just iterate over sorted sequence $A$ and check if any two its consecutive elements are equal. The total time complexity of this method is $O(N \cdot \log N)$ and it's dominated by the sorting phase.

AUTHOR'S AND TESTER'S SOLUTIONS:


Setter's solution can be found here.
Tester's solution can be found here.
Editorialist's solution can be found here.

Show all pairs with given sum

$
0
0

I used brute force approach but it can not be efficient.Please tell me How to print the all pairs with given sum efficiently????

Like suppose an array with 3 element are (4, 5, 2} and given sum is 6. Then the pair with given sum is {4,2} and {2,4}

input 3
4 5 2
6

Output :
4 6
2 4

My code

CLOSEFAR - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Animesh Fatehpuria
Tester:Pushkar Mishra
Editorialist:Animesh Fatehpuria

DIFFICULTY:

Medium

PREREQUISITES:

Mo's Algorithm, Segment Trees, Lowest Common Ancestor

PROBLEM:

You are given a Tree with $N$ weighted nodes and you have to deal with two types of queries efficiently. The first query is to report the closest two values in a path between two nodes, and the second query is to report the farthest two values in a path between two nodes. There are total $Q$ queries and $N, Q \le 35000$.

QUICK EXPLANATION

Linearize the tree by doing a dfs traversal of the tree. Note that we'll do this a little differently to record the enter and exit time of each node i.e each node will appear twice in the linearized array. Now apply Mo's Algorithm to sort the queries in a desired order. While expanding or contracting the window, we can check the number of times the node has been seen in this window and correspondingly add or subtract $1$ in the particular position of the segment tree. The segment tree just needs to report the maximum difference between any two adjacent $on$ values.

EXPLANATION

Note that for subtask $1$ the solution is simple brute force.

For Subtask $2$, we can build an lca like data structure precomputing $2^i$ ancestors of each node, and the maximum/minimum values in the path from each node to this ancestor. Then we can decompose each query $(u, v)$ into $u$ to $lca(u, v)$ and $v$ to $lca(u, v)$ and compute the answer.

It is clear that type $F$ queries can be easily handled.

For Type $C$ queries, let us first look at a brute force solution.

First of all compress all the values stored at the nodes to a range of $[1,N]$.

For each query of the form $u,v$ do a dfs from node $u$ and maintain a boolean array $A$ such that $A[i] = 1$, if there exists a node having value $i$ on the path from $u$ to current node. On reaching the node $v$ during dfs, the boolean array would represent what all values are present on the path from $u$ to $v$. Now we can just iterate over the boolean array and for every consecutive $i, j$ such that $A[i]$ and $A[j]$ are both $1$, we can keep min of $(j - i)$.

We can fasten up the above solution by maintaining the boolean array $A$ using segment tree. We could use the operations set/unset an element using point update of $1/0$ and range max of difference of consecutive set elements. This can easily be done by maintaining the leftmost and rightmost set element in the range of each node.

Now, let's try to imagine that the queries are on subarrays and not tree-paths. Let us first do a dfs ordering of the given tree (rooted at any arbitrary node) such that each node comes twice in the array, once while entering the dfs and other while exiting the dfs. Now, a path from u to v can be represented as consecutive elements in this array. More on this here.

Hence, if we can implement a function to add a value to a range and a function to remove a value from a range, along with maintaining the answer of the current range, we can easily use MO's algorithm to solve the problem. To implement the add and remove functions, we could use the segment tree idea described above in the brute force approach. Hence we could use a segment tree along with MO's algorithm to solve the problem.

Final Complexity: $O(Q\sqrt{N}\log{N})$

Note : The constraints were such that some brute force solutions passed during the contest, although we had done some testing to ensure that it doesn't. We had no option but to make the TL very strict, at $3.5$ seconds, in order to cut out the brute force solutions. Note that solutions using sets might not pass, as sets and multisets have a large constant factor. We deeply regret any inconvenience caused.

AUTHOR'S SOLUTION

Setter


SALARY - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Ivan Zdomsky
Tester:Anton Lunyov
Editorialist:Anton Lunyov

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

You are given the array W[1], W[2], ..., W[N] of salaries of the workers. At each operation you can increase by 1 salaries of any N − 1 workers. Your goal is to make salaries of all the workers to be equal. What is the minimum number of operations needed for this?

QUICK EXPLANATION:

The answer is just sumW − N * minW, where sumW is the sum of all W[i] and minW is the minimum over all W[i]. See author's solution and tester's first solution as a reference.

EXPLANATION:

The operation can be treated as follows: we at first decrease salary of some worker by 1 and then increase salaries of all workers by 1. But why do we need to do the second part? Since we want all salaries to be equal the second part of the operation could be simply ignored. So we may assume that at each operation we decrease salary of some worker by 1.

Now if we have some salary greater the minimum salary then without applying operation to it we can't achieve the goal in any way - the minimum could only decreases during operations so this salary will be always not equal to the minimum one. Hence we need to apply at least W[i] − minW operations for the i-th worker. The summation of this over all i is exactly sumW − N * minW. But, clearly, applying exactly W[i] − minW operations to the i-th worker (for all i) makes all salaries to be equal to minW, which is our goal. Therefore, this number of operations is also sufficient. Hence it is the answer as stated above.

ALTERNATIVE SOLUTION:

The constraints were quite moderate. So alternatively one could model the process of applying all the operations. But the following naive implementation will get TLE: at each step we at first check whether all salaries are equal, if no, then choose the worker with the maximal salary and increase by 1 salaries of all other works. Such solution has complexity O(maxW * N * N) in the worst case that have the form W[1] = 0, W[2] = maxW, W[3] = maxW, ..., W[N] = maxW, where maxW = 10000. Since we also have like 100 tests in a file, such solution could consume more than 8 seconds on one test file. And we indeed have test files having all 100 tests of this or similar form.

In order to get AC with modeling one should at least figure it out the first step of the solution explained above: instead of increasing salaries of N − 1 workers one should decrease salary of just one worker at each operation. But even this will get TLE if it would be implemented as above. The simplest way to make such solution fast enough is to decrease by one all maximum salaries at one step (so we perform several operations at once). Then the solution will have complexity O(maxW * N) and passes the TL easily. Namely now at each step we at first check whether all salaries are equal and if no we find the maximal salary and then decrease by 1 all salaries equal to this maximum. See tester's second solutions as a reference.

AUTHOR'S AND TESTER'S SOLUTIONS:

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

RELATED PROBLEMS:

Codechef June 2012 Challenge - LECANDY
Codechef October 2012 Challenge - DRGNBOOL

ZCO 14 SUPW

$
0
0

Link- SUPW

This is my code for SUPW in python-

n = int(input())
c = list(map(int,input().strip().split()))
if n <= 3:
    print(min(c))
else:
    cost = [c[0],c[1],c[2]]
    for i in range(3,n):
        cost.append(c[i] + min(cost[i-3:i]))
    print(min(cost[n-3:]))

This is giving accepted for all test cases except for test case 2 in which it is giving Runtime Error. Can someone tell me the reason or provide a test case where it fails.

So what I have done is that-

Cost[i] = min time if we work that day.

So cost[0] = c[0],
   cost[1] = c[1],
   cost[2] = c[2]
And for the rest it is c[i] + min of the previous 3 elements.
And the answer is the min of the last 3 elements.

Size of the CodeChef Bag!!!

$
0
0

The previous bag of codechef was very small and even a laptop didn't fit into it. I am not saying that it was not good but still it was too small. In the goodies section there is a new bag(red colored). I wanted to ask that is the bag of good size?

Why is it showing -10 on my contribution in the top-contributors list?

$
0
0

Why is it showing -10 on my contribution in the top-contributors list? I saw my karma history and nobody has downvoted me but on the top contributor page, it is showing -10 karma.

How to sort an array in c++ by the second column in reverse order?

$
0
0

Suppose I have a 2-D array like this -
32 34
99 19
12 45
23 27
I want to sort it on the basis of the second column in reverse order so that it becomes -
12 45
32 34
23 27
99 19

In python I would just do -

a.sort(key = lambda x: x[1])

How to do this in c++ ?

c ++ assigning values to an array globally

$
0
0

when I declared a global array

int color[5005]

I assigned all its values -1 outside the main function

for (int i = 0; i < 5005; i++) color[i] = -1;

It gave me error.
But when i did this inside the main function it didn't.

I am new to c++, please help.

Will STL docs will be provided during INOI?

$
0
0

Will stl docs be provided during INOI and if yes then can someone give the link to that stl docs?


COT2 - Count on a tree II - help needed

How many top contributors get laddus?

$
0
0

On the goodies.codechef.com page it is given that the top contributor on discuss gets 300 laddus. How many top contributors get that. Like in the contest win, top 10 get it.

Difference between step count process and asymptotic notation

$
0
0

Please tell me - What is the difference between step count process and asymptotic notation regarding time complexity? I searched in internet but could not get the appropriate answer...... Please tell me What is the difference between step count process and asymptotic notation regarding time complexity? and How to contrast step count process and asymptotic notation regarding time complexity

Chef and Array Update Editorial

$
0
0

Problem Link:https://www.codechef.com/problems/EXOCODE4

Author:https://www.codechef.com/users/vivek96

DIFFICULTY:CakeWalk

PREREQUISITES:Basic Programming,Array

PROBLEM:Chef Purchased an array A having N Integer values,After Playing it for while,he got bored of it and decided to update value of its element.In one second he can increase value of each array element by 1.He wants each array element’s value to become greater than or equal to K.

Please Help Chef to find out the minimum amount of time it will take,for him to do so..

EXPLANATION:Since we can only increase all the elements by 1, the minimum element will take most step to reach K.So we have to find the minimum element in array A and answer will difference between K and minimum element.

One corner case is if K is less than minimum element in the array A

Time Complexity:O(N)

AUTHOR'S AND TESTER'S SOLUTIONS:

alt text

Note: We can also use Sorting(Bubble,Quick,etc) to find minimum element in Array, but this will increase time complexity

SCHEDULE - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Hasan Jaddouh
Testers:Sergey Kulik, Kamil Dębowski
Editorialist:Pawel Kacprzak

DIFFICULTY:

EASY

PREREQUISITES:

Binary search

PROBLEM:

For a binary string $A$, let's define a block as a substring of $A$ containing either only $1$'s or only $0$'s, so $111$ and $0$ are blocks, while $10$ and $0010$ are not. Moreover, let a major block be a block of $A$, such that no block in $A$ is longer that it and $L$ be the length of a major block of $A$. Notice that there can be many major blocks, but all of them have the same length.

Then the problem can be reformulated as follow: for a given binary string $A$ of length $N$ and integer $K$, we have to return the minimum $L$ (the length of a major block) that we can get by performing at most $K$ bit flips on $A$.

For example, if $A = 11100$ and $K=1$, the best we can do is to either flip the first or the second bit to get for respectively $01100$ and $10100$. In both these strings, $L$ is $2$, which is the best we can get in this case.

QUICK EXPLANATION:

First, check if getting $L=1$ is possible. If yes the problem is solved, otherwise, perform a binary search on the result checking if getting $L=M$ is possible in linear time.

EXPLANATION:

First of all, let's take a closer look at the constraints. It is said that $K \leq 10^6$, but since also $N \leq 10^6$ and there is no point of flipping a single bit more than once, we can rewrite the constraint as $K \leq N$.

Subtask 1

In the first subtask $N \leq 10$, so one can generate all possible strings we can get by performing at most $K$ flips on $A$, and compute the smallest $L$ among them. This is possible because there are at most $2^N$ such strings and computing $L$ for a single string takes $O(N)$ time. Thus the overall time complexity of solving all test cases will be $O(T \cdot 2^N \cdot N)$, which is completely fine for these constraints and $T \leq 11000$.

Subtask 2

In the second subtask we have $N \le 10^6$, so the problem has to be solved in a clever way. The crucial observation is that for any $L$ and any block of $A$ with length $M$, $\lceil M / (L+1) \rceil$ flips are necessary to convert that block into a substring without blocks of length greater than $L$, and we can perform exactly that many flips to do that.

For example, if we have a block of length $5$, $00000$, and $L=2$, we flip its bits with indices $L+1, 2 \cdot (L+1), 3 \cdot (L+1) \ldots$, so in this particular case, we flip only its third bit to get $00100$. Similarly, if we have a block of length $6$, $000000$, and $L=2$, we flip its third and sixth bits to get $001001$.

Thus it looks like we can perform a binary search for the answer and for a fixed $M$, check if we can transform all the blocks in $A$ into block of length at most $X$ using at most $K$ flips by just iterating over all blocks in $A$ and computing the sum of needed swaps for all of these blocks. Notice that binary search is correct here, because if it is not possible to use at most $K$ swaps to get $A$ with $L=X$, then it is also not possible to use at most $K$ swaps to get $A$ with $L \lt X$. This will result in $O(N \cdot \log N)$ time complexity for a single test case, but since the sum of $N$ across all test cases is at most $10^6$, this will run fast enough. However, we are not finished yet, because there is a tricky case to handle.

Notice that when we divide $A$ into blocks, the blocks alternate in such a way that after a block consisting of $0$'s there is a block consisting of $1$'s, then one consisting of $0$'s again and so on. The observation we made says that for a block of length $M$, $\lceil M / (L+1) \rceil$ flips are necessary and sufficient to convert this block into blocks of sizes at most $L$. It is true for an isolated block, however, since in some cases we are forced to flip the first or the last bit of such block, that may ruin our solution because that bit can create another block with adjacent blocks.

Let's take a closer look where this situation can happen, so in what cases we are really forced to flip either the first of the last bit of a block? As mentioned above, for a block of length $M$ and a fixed $L$, we can flip bits with indices $L+1, 2 \cdot (L+1), 3 \cdot (L+1) \ldots$. It follows that we are flipping its last bit if $M \mod (L+1) = 0$. However, if this happens and $L \gt 1$, we can always flip bits with indices $L, 2 \cdot L, 3 \cdot L, \ldots$ and we avoid flipping the first and the last bits, which caused the problem. But what happens when $L = 1$? Well, then we cannot use that trick, however, this case is easy to handle: at the beginning, we can just check if $A$ can be transformed using at most $K$ flips to get $L=1$. Notice that there are just $2$ possible strings of given length with $L=1$, either a sequence of alternating $0$'s and $1$'s starting with $0$ or the same but starting with $1$. If this is the case we are done. Otherwise, we use binary search described above searching for the answer in a range $[2, N]$. For implementation details, please refer to multiple solutions linked below.

AUTHOR'S AND TESTER'S SOLUTIONS:


Setter's solution can be found here.
First tester's solution can be found here.
Second tester's solution can be found here.
Editorialist's solution can be found here.

Viewing all 39796 articles
Browse latest View live


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