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

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??


requesting EDITORIAL for L-R queries !!

$
0
0

can anyone post the editorial for the recent november lunchtime question L-R queries ?? It would be of great help !!! Thanks in advance !!

What are karma points?

$
0
0

I am new to this karma system, Can you explain me why is it called so? What has karma points got to do with contribution?

Haha! Finally codechef's Plagiarism test took me down incorrectly (Though it was my mistake completely)

$
0
0

Hello folks, Finally I received what we call most threatening dream of every coder(Haha!). Guess what? Yes, you are right. It is getting caught falsely for plagiarism. Now here is what happened: This guy @punee901 (https://www.codechef.com/users/punee901) has copied my code for CHEFNIL of November Long challenge 2017. These are the code:

https://www.codechef.com/viewsolution/16258278 (plagiarist)https://www.codechef.com/viewsolution/16259473 (Me)

Well, it dosen't matter whether my ratings are down or not but this type of people should be banned from CC. I agree it was completely my mistake because I kept my code public on last day of challenge in hurry and I don't feel bad if codechef decreases my ratings. After all I deserve it. But this people are making competition bad, really bad.

So purpose of this post was to make you all aware (again!) don't keep your code public on ideone. Not even by mistake. I don't want this to happen with any good, genuine coder.

Thanks.

Yadnesh Sonawane

GEEK05 - Editorial

$
0
0

Problem Link

Practice

Contest

Author:Bhuvnesh Jain

Tester:Bhuvnesh Jain

Editorialist:Bhuvnesh Jain

Difficulty

MEDIUM

Prerequisites

Sieving Techniques, Modular Arithmetic, Prefix Sums.

Problem

Find the sum of ${a}*{l} \dots {a}*{r}$, where array $a$ satisfy the follwoing property,

$\sum_{d | n} {a}_{d} = {2}^{n}$

Explanation

First of all, we see some basic properties of modular arithmetic:

  1. (a + b) % m = ((a % m) + (b % m)) % m
  2. (a - b) % m = ((a % m) - (b % m) + m) % m
  3. (a * b) % m = ((a % m) * (b % m)) % m

For the 3rd property, care must be taken while doing the multiplication, as the value may be an integer, while there may be overflow in the intermediate multiplication operation.

Now, assume we have all the values of $a_i$ calculted in efficient way, then we can answer the sums in every query naively in $O(r-l)$. To do this efficiently, we can simply precompute the prefix sums and then answer the query in $O(1)$, as :

ANS = prefix[r] - prefix[l-1], where prefix[i] = $\sum_{j=1}^{j=i} a[i]$

To calculate $a_i$ efficiently, we will use the general sieving technique. The pseudocode is given below :

for i in [1 : 100000]: a[i] = 2^i for i in [1 : 100000]: for j in [2i, 3i, ... 100000]: a[j] -= a[i]

To check why this code works, let us look at simple observations.

  1. We know initial terms are as a[1] = 2, a[1] + a[2] = 4, a[1] + a[3] = 8, a[1] + a[2] + a[4] = 16, a[1] + a[5] = 32 and so on.
  2. Thus, once we have calculated the correct answer for particular $i$, we will subtract its contribution from other $a_j$, to which it contributes to their sum. For example, we know $a[1]$ initially, so, we subtract it from $2, 3, \\dots$. Thus, we get the correct values of a[2], a[3], a[5] etc. Now, we subtract $a[2]$ from $4, 6, 8 \\dots$, and we get the correct value of 4. Thus the process continues.

The time complexity of this approach is $O(n/1 + n/2 + n/3 + n/4 + \dots + n/n)$ = $O(n \log{n})$.

Bonus Problems

  1. As the constraints are not that large, we can even do a naive brute force to calculate the values, in $O(N * sqrt(N)). See setter's solution 2 below.
  2. As constraints allowed storing the pre-computed values in an array, the above approaches were good enough. What is the number of queries were reduced, but $L, R$ were up to now ${10}^{11}$. We can still use the idea of mobius inversions and some other transforms to give the answer to each query in $O({n}^{3/4})$. Try to think about this one? You can also refer to [this video](https://www.youtube.com/watch?v=_noJI8UkTq8&t=31s) by kevinsogo for some help.

Time Complexity

$O(N * \log{N} + Q)$, where $N$ = Max value of R i.e ${10}^{5}$

Space Complexity

$O(N)$, where $N = {10}^{5}$

Solution Links

Setter's solution

Setter's solution - 2

GEEK02 - Editorial

$
0
0

Problem Link

Practice

Contest

Author:Bhuvnesh Jain

Tester:Bhuvnesh Jain

Editorialist:Bhuvnesh Jain

Difficulty

EASY

Prerequisites

Looping Techniques, Greedy Algorithms

Problem

Find the smallest number such that the sum of the digits is $N$ and it is divisible by ${10}^{N}$.

Explanation

To make a number divisible by ${10}^{N}$, we need at least $N$ zeros at the end of the number. To make the number smallest, we append exactly $N$ zeros to the end of the number. Now, we need to ensure the sum of the digits is $N$. For this, we will try to make the length of the number as small as possible to get the answer. Thus we keep on inserting 9 into the number till the sum doesn't exceed $N$. If we have any remainder left, then we keep it as the first digit (most significant one) so that the resulting number is minimised.

The approach works well for all subtasks but there are 2 corner cases:

  1. The first is that the final number may not fit into the dtata types present in C++/Java. Since, we only need to output the number, we can use strings to store the answer.
  2. The only corner case where the answer is 0 is N = 0.
  3. There are no cases where the answer doesn't exist.

Time Complexity

$O(N)$, per test case.

Space Complexity

$O(1)$ or $O(N)$, if storing the answer in the string.

Solution Links

Setter's solution

GEEK03 - Editorial

$
0
0

Problem Link

Practice

Contest

Author:Bhuvnesh Jain

Tester:Bhuvnesh Jain

Editorialist:Bhuvnesh Jain

Difficulty

CAKEWALK

Prerequisites

Looping Techniques, Sorting Algorithms

Problem

Find the number of students remaining in the class if their name is recorded everytime they enter or leave the room.

Explanation

It is easy to see that at the end of the class only those students remain in the class who name was written an odd number of times.

To check the above condition, we can do a brute force by finding the number of times each name occurs and if it occurs an odd number of times, update the answer by $1$. To compare 2 names, we can simply iterate over them character by character and also check it their lengths are same or not.

One other efficient way is to sort all the student names. Then, same names occur as a subarray, so counting the frequency will be linear in this case as compared to quadratic in the previous case.

Time Complexity

$O(X * \log{X})$, per test case, where $|X|$ = Sum of lengths of all strings.

or $O(N * N * |S|)$, per test case, where $|S|$ = Maximum length of any string in the test case.

Space Complexity

$O(X)$, where $|X|$ = Sum of lengths of all strings.

Solution Links

Setter's solution

Setter's solution - 2

GEEK01 - Editorial

$
0
0

Problem Link

Practice

Contest

Author:Bhuvnesh Jain

Tester:Bhuvnesh Jain

Editorialist:Bhuvnesh Jain

Difficulty

CAKEWALK

Prerequisites

Sorting, Looping Techniques

Problem

The median of a matrix is defined as the median of the median of all rows. Find the median of the matrix.

Explanation

To find the median, we need the numbers to be sorted in increasing order. To sort the numbers, we can use merge sort or inbuilt sort algorithm available in the languages. Once, we find the median of all rows, we can do the same trick to find the median of all rows.

To read more about sorting algorithms, refer to this link

Time Complexity

$O(N * N * \log{N})$, per test case.

Space Complexity

$O(N * N)$

Solution Links

Setter's solution


ZCO Preparatory Contest today !

$
0
0

Hello, everyone. I would like to invite you to the ZCO preparatory contest of the ICO prep series. This one is specially target for coders who are doing the ZCO tomorrow.

Problem Setter: Adhyyan Sekhsaria, Yogesh Aggarwal

It will be held on 10th November from 7:00 PM onwards.

The contest will be unrated. The contest will contain 4 problems of ZCO level. I hope you enjoy the contest. An all the best for ICPC and ZCO to everyone. :) Sorry for the short notice.

UPD:

Link to Contest:link

Link to Editorial: link

cutting receipe

Help in NUMGAME2

$
0
0

I just read the editorial for this problem and i was wondering that how do you tackle these kind of problems.

Why Alice wins when n%4 == 1 and bob wins for other cases ?

How to solve this maze problem.

$
0
0

There is a maze of size n*n. Tom is sitting at (0,0). Jerry is sitting in another cell (the position of Jerry is input). Then there are k pieces of cheese placed in k different cells (k <= 10). Some cells are blocked while some are not. Tom can move to 4 cells at any point of time (left, right, up, down one position). Tom has to collect all the pieces of cheese and then reach to Jerry’s cell. You need to print the minimum no. of steps required to do so.

I can solve this problem recursively but that is too complex , how can we do better?I can't think any DP solution as we can go in all 4 directions.

Help in POTATOES

$
0
0

I am using the sieve of sundaram to get an array of primes upto 4000 and then doing binary search on it to find the next greatest prime then the sum of X and Y and outputting the different between the prime and the sum. but it gives me wrong answer. help?

link to my code

https://www.codechef.com/viewsolution/16379802

November Lunchtime Editorials

$
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.

Even then, some of the users have shared their approaches for various problems, which are mentioned below:

SMRSTR

This problem has been discussed at here by @vipin_bhardwaj

SHUFFL

This problem has been discussed here by @aayushkapadia

L-R Queries using sqrt decomposition by @avi224

Break the array into blocks of size $\sqrt{N}$ and then use a $map$ containing frequencies of numbers in each block. For update, remove $A[l]$ from the block containing index $l($ $i.e.$ $l/block\_size)$ by decreasing its frequency and then add $y$ to it.

The key to solving the problem is to notice that we have to minimize the quadratic expression $(x-A[l])*(x-A[r])$ such that $x=A[m]$ and $l\le m \le r$. Note that the global minimum occurs at $x_0=0.5*(A[l]+A[r])$. We have to find this $A[m]$ nearest to $x_0$. This can be done using binary search (lower_bound() function in c++) on the frequency maps.

For query, just iterate from $l$ onwards and calculate normally until you find the start of a block. Iterate through every block as long as index $r$ is outside the block, and find the nearest $A[m]$. After the last block, calculate normally until index $r$ is reached.

The preprocessing part takes $O(N*log(N))$ and complexity is $O(\sqrt(N)*log(N))$ per query.

Refer to solution here

STRQUER

We still haven't got any editorial for this problem, so i again invite all of u to share ur approach if u wish to. :)

And i hope you guys would like to share your approach as always. :)

Explain the approach applied in the solution


Rating Query

$
0
0

If I don't participate in a CodeChef Monthly Challenge ( Long / Cook-Off / Lunchtime ) , ( i.e. make NO submissions ) , will my rating drop , or remain unchanged ?

November Cook-Off

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

$
0
0

Hello guys,

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

SKIING - Editorial

$
0
0

Problem Link

Practice
Contest

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

Difficulty

EASY-MEDIUM

Prerequisites

Strongly Connected Components, Bfs / Dfs

Problem

Find the minimum number of cells to select in a matrix so that we can reach every cell from it through a series of moves which is, "You can move from one cell to another only if they are adjacent (i.e. share a side) and the height of destination cell is not more than height of current cell."

Explanation

The edges in the graph will be between cells $(i, j)$ and $(x, y)$, only if they share a side. Also, the edges will be directed as the edge exists from a cell into another only if the height of destination cell is not less than the height of the current cell.

For example in the sample input 1, the edges will be as follows:

Now given a directed graph, we want to select a set of cells so that all cells are reachable from it. The first observation is 2 decompose the graph into Strongly connected components, as there may be cycles in the graph. This is because all the vertices in a cycle can be visited by starting from any vertex.

Now, we have a directed graph in which there are no cycles. (It is also called a DAG). In this graph, we claim that we chose only vertices with indegree $0$ to be in our set as the possible candidate solution.

Reason : Let us say there is an edge $P$ to $Q$ in the DAG. Let us denote the set of vertices reachable from $Q$ to be $S$. Then all vertices in $S$ are also reachable from $P$. But $P$ is not reachable from $Q$. So continuing this process, we will see that only vertices having indegree $0$ are not reachable from any other vertex but they as a set cover all the vertices reachable from it.

The DAG of input sample 1 is give below:

In the above figure, $1$ represents the set ${1}$, $2$ represents the set ${2,3}$, $3$ represents the set ${4,7}$ and $4$ represents the set ${5,6,8,9}$.

Thus, the algorithm is as below:

  1. Construct the directed graph from the grid. Complexity is $O(N * M)$. Also, the number of edges in the graph is bounded by $(8 * N * M)$ as each cell can have at most 4 neighbors and it can have edge in both directions.
  2. Decompose the graph into one with no cycles. You can use Tarjan's algorithm or Kosaraju algorithm for this step.
  3. Find the number of vertices with indegree $0$ in DAG.

Author's Solution

The solution counts the number of components having same heights, such that all adjacent components are less (strictly) than it.

Proof: Each such component consisting of all cells of same height can be represented by 1 cell only. Now, each component can either have all adjacent components less tha it or not. If all cells adjacent to it are smaller, the those can be reached from the current cell, so they are ot counted towards the answer. If we repeat this process, for each component, we are bound to find only those cells which will contribute to the answer. Also, if we assume some cell that shouldn't be in answer was counted by above process, the it would contradict the fact that all adjacent components are lesser than it.

The above process can be simply implemented by a dfs call from every vertex and check if all components adjacent to it are smaller than it or not. For details, refer to author's solution.

Time Complexity

$O(N * M)$, per test case.

Space Complexity

$O(N * M)$

Solution Links

Setter's solution
Tester's solution
Editorialist's solution

Incorrect statement of problem FACTSUM

Viewing all 39796 articles
Browse latest View live


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