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

TEX - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Naman Goyal
Tester:Aanya Jindal
Editorialist:Naman Goyal

DIFFICULTY:

CAKEWALK

PROBLEM:

The minimum number of transactions to be done to divide the money equally after 3 people paid (possibly) different amounts.

QUICK EXPLANATION:

Number of transactions will be equal to 1 less than the number of people who haven't paid the exact amount.

EXPLANATION:

The problem statement makes it clear that there are three friends wants to divide the money equally among themselves. Therefore, we need to find the average of the three numbers.

Since we have to make the least number of transactions. It should be clear that the maximum number of transactions that can be made is 2 which will be the case when no one of them has paid the amount equal to the average amount.

Other two cases are 0 and 1.
0 when all three of them has paid the same amount 1 when one of them has paid the amount equal to the average amount

It is necessary to take into the account about the constraints that are in the range of long long for c++, java. 0<=a,b,c<=10^18

AUTHOR'S AND TESTER'S SOLUTIONS:

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


Things to do in summer vacation

$
0
0

What should i do in summer vacation of 2 months? what are the best thing to learn in summer to improve my coding skill? i'm a begineer:-

GOODS - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Jatin Gupta
Tester:Aanya Jindal
Editorialist:Naman Goyal

DIFFICULTY:

EASY

PREQUISITES

Sorting

PROBLEM:

In problem statement, it is given that there are n(even) number of people and we have to do pairing of them in such a manner that the sum of the numbers assigned to them should be greater than k.

QUICK EXPLANATION:

After sorting the laddus, we take last and first element. If they form a valid pair, then we take the next minimum and next maximum laddus. Else, we advance the minimum and add 1 to the unsatisfied number of people.(As the minimum cant form valid pair with any other remaining person).

EXPLANATION:

We have to minimise the number of people left who can not reach the given amount(k).

We see how we can use the greedy strategy to solve the given problem.

If the sum of laddus of 2 people is greater than k, then it does not matter how much greater than k they have.

So we try to pair the person with maximum laddus with the person having minimum laddus. If they are not able to form a valid pair, then the person having minimum laddus cannot form a valid pair with anyone else. So, we add that person to the list of people who can't be in any pair.

(Let’s say we use an array as our data structure, then sorting gives an ordered arrangement of the elements in ascending manner.)

To minimize the number of people left we need to take the minimum and maximum amount of the array and then check whether their sum is greater than k or not.

If yes, then move on to the next minimum and maximum number and repeat the process until we reach the point where minimum and maximum become same or minimum become greater than maximum.

If no, then check for the next minimum number keeping the maximum number same and repeat the checking process until we reach the point where minimum and maximum become same or minimum become greater than maximum.

AUTHOR'S AND TESTER'S SOLUTIONS:

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

Submit button in google chrome gmail not working but works in internet explorer

$
0
0

What can I do to get the submit button in google chrome gmail to work when responding to emails I recieve?

Please help in SPOJ GAMES

$
0
0

Problem: http://www.spoj.com/problems/GAMES/ My solution is giving right answer for the test cases given in the question but i m still getting wrong answer. I am not able to find the bug, Any sort of Help will be appreciated, Thank You.

Code:

#include <iostream>
using namespace std;

// gcd algo
int gcd(int, int);

int main() {
    int t;
    cin >> t;
    while(t--) {
        string n; 
        cin >> n;
        string f_part;
        int count = 0,check = 0;
        for(int i = 0; i < n.length(); i++) {
            if(n[i] == '.') {
                check = 1;
                continue;
            }
            if(check == 1)
                count++;
            if(n[i] == '.') continue;
            else
                f_part += n[i];
        }
        int x = atoi(f_part.c_str());
        string deno = "1";
        while(count--) deno += '0';
        int deno_int = atoi(deno.c_str()); 
        int gcd2 = gcd(x, deno_int);
        cout << deno_int / gcd2 << endl;
    }
}

int gcd(int a, int b) {
    if(a % b == 0) return b;
    else return gcd(a, a % b);
}

[When ??]SnackDown' 18

$
0
0

@admin , Is snackdown scheduled this year ?? As I can see https://www.codechef.com/snackdown/2017/schedule Registration for snackdown'17 started on 14 April'17. But for this year It's 18 today and I am not seeing any promotions/ads/anything else for snackdown'18. I'm eagerly waiting for it. Because it will be my first snackdown. If it's not scheduled this year then please schedule it.

Also as pointed out by our fellow mate @gomu Editorials of last year's Online Elimination round are still not available. @admin Please add them as well.

ENP - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Jatin Gupta
Tester:Aanya Jindal
Editorialist:Naman Goyal

DIFFICULTY:

EASY

PREQUISITES

Basic knowledge of modulo operator

PROBLEM:

To find the length of longest palindrome that can be made by taking a subset of given string after decrypting it.

QUICK EXPLANATION:

First decrypt the string by applying inverse operation of the given encrypt function. After this, note that we can take even numbers of each character(Put that character at the beginning and end of the current answer string) and also 1 character separately that can be put in the middle.(if it exists)

EXPLANATION:

First step is to decrypt the given string:

Since we are given a pseudocode in which there is statement for encryption given as follow:
encrp+=charof((val(s[i])+x + i*d)%26);//we are given x and d

So, for decryption we have to do opposite of encryption i.e.,
decrp+=charof(value((s[i])-x-i*d)%26);

But here the problem is that id will make the modulo negative so for keeping it positive we have to add 26 to it so that the resultant lies is positive and also lies in the range of alphabets … So the new statement is :
decrp+=charof(value(((s[i])-x-i
d)%26+26)%26);//applying modular arithmetic

After decryption, it is now to find the maximum palindromic subset in the string decrypted above.

First of all it is necessary to know that a palindromic string has two parts-

  1. The characters that are double
    Example: abcba
    In this case a and b are these characters.

  2. The characters that are not double
    Example: abcba
    In this case c is that character.

Single character can at maximum occur once while we can have as many as doubles possible.

Since we have to find the subset and then arrange, so order in string does not matter.
Therefore, we will find the frequency of each alphabet in the string.

Now there can be two cases-

  1. When the frequency of particular alphabet is even: all the occurrences of that character can be used.
  2. When the frequency is odd: All but one occurrence can be used.

If we had even count for any character, we can add 1 to the answer

AUTHOR'S AND TESTER'S SOLUTIONS:

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

ADP - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Jatin Gupta& Gaurav Aggarwal
Tester:Aanya Jindal
Editorialist:Aanya Jindal

DIFFICULTY:

Easy-medium

PREREQUISITES

GCD, math

PROBLEM:

Given start and end positions on a line and the number of steps in forward and backward direction Adhiraj can take, find if the end position can be reached under the constraints.

QUICK EXPLANATION:

ax + by = c has a solution for x and y if and only if c is a multiple of gcd of a and b.
(Linear Diophantine Equation).

In our problem a = f, b = b and c = y-x. So after checking all boundary conditions if gcd of f and b divides the distance to be travelled, then Adhiraj can reach his destination.

EXPLANATION:

The problem required handling corner cases as well along with the knowledge of the gcd which will be used for main solution.

Following cases should be handled properly:

CASE 1: When c = y-x = 0
Answer will always be YES because Adhiraj has no steps to move.

In further cases we assume that c != 0

CASE 2: When f = 0 AND b = 0
Answer will always be NO because Adhiraj can’t make any step at all and thus won’t be able to reach his destination.

CASE 3: When f = 0 AND b != 0
Here Adhiraj can only move in backward direction and that too only in multiples of b.
Thus,
If c < 0 AND c%b == 0 answer will be YES
Else answer will be NO

CASE 4: When f != 0 AND b == 0
By similar reasoning as CASE 3,
If c > 0 AND c%f == 0 answer will be YES
Else answer will be NO

CASE 5: When none of the above cases follow, i.e. f, b, c != 0

When we try to solve this case, we end up with the equation,
fx - by = c,
Where, x is the number of steps in forward direction
y is the number of steps in backward direction.

It should however be noted that x and y should only take positive integer values.
(since, steps can’t be negative)

This equation is indeed a Linear Diophantine Equation and its solution says that ax + by = c has integral solutions for x and y if and only if c is a multiple of gcd of a and b.

Let’s see this intuitively using an example,
Suppose that f = 4 and b = 6. So we can easily see that to travel 2 in +X, we can take 2 steps forward and 1 step backward. So, ‘ffb’ gives +2. Now, to travel 2 in -X, we can take 1 step backward and 1 step forward. So, ‘bf’ gives -2. Now we can travel in multiples of 2. But see if you can take steps such that distance travelled is not a multiple of 2 (which is GCD of 4 and 6).

Thus,
If c%gcd(f, b) == 0 answer will be YES
Else answer will be NO

To compute gcd, in-built function __gcd can be used or any implementation of eucledian gcd will work.

AUTHOR'S AND TESTER'S SOLUTIONS:

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


Not getting accepted in highway bypass problem.

$
0
0

I have been trying to solve the Highway Bypass problem but I am not getting AC. I am not able to figure out what is wrong with My Solution. anyone please tell me what is wrong with my logic.

How to solve road repair problem from SPOJ?

Invitation to Coders' Legacy 2018 (Rated)

$
0
0

Hello CodeChef community,

On behalf of KeyGEnCoders, the programming club and CodeChef Campus Chapter of Kalyani Government Engineeing College, I would like to invite you all to Coders' Legacy 2018. The contest will be rated for both divisions.

banner

The contest will start on Sunday 15 April at 21:30 IST and the duration will be of 3 hours. It will have an ACM style ranklist with the penalty set to 10 minutes. There will be 6 problems of varying difficulty.

The problem setting team includes @avijit_agarwal, @sarthakmanna, @mayukh45, and myself. I would also like to thank Arjun Arul from CodeChef for helping out with the problems.

Top 5 Indian and top 5 non-Indian winners will get 300 CodeChef laddus each.

Good luck to all and wish you high rating :)

UPDATE: The contest starts in 30 mins.

UPDATE 2: The contest has ended. Check out final standings and editorials. Hope everyone enjoyed the problems! The cakewalk problem turned out a little harder than we expected, we apologize for that :/ Thank you for your participation.

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

$
0
0

Please only ask a question here if you don't have sufficient karma points to post a question

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

MGCHGYM - Editorial

$
0
0

Problem Link

Practice
Contest

Difficulty

Medium-Hard

Pre-requisites

Treap, bitset, DP

Problem

Maintain two types of modifications on the given array and solve the subset-sum problem on its' subsegments.

How to get 25 points

For the beginning, let's focus on the solution to the first subtasks. The constraints are rather small, so all the modifications can be done naively, in the straightformard way. Namely, if you need to change the value of an element, you can do this in O(1) time, and when you need to reverse the subarray, straightforward O(N)-per-reverse approch will do.

Let's deal with the queries of the third kind.

The following simple DP technique will help us to determine - whether it is possible to get W as the sum of some of these numbers or not. First of all, pseudocode:

fill dp[] with zeroes dp[0] = 1 for i = L; i <= R; ++i for j = W; j >= a[i]; --j dp[j] |= dp[j - a[i]]

Now, let's have a closer look to the provided code. Suppose that we operate on the array a[]. The value of dp[k] will be equal to one if it is possible to collect the sum of k from the given integers and will be equal to zero otherwise. Initially, only dp[0] is non-zero.

Then, step by step we recalculate the values of dp[] by adding the integers from the given set one-by-one. The value of dp[j] will become equal to one during the adding of the ith element (say a[i]) if it was possible to collect a sum of (j - a[i]) before. Of course, we consider that j ≥ a[i].

The whole complexity of the described solution is O(NQ+PNW) or simply O(N(Q+PW)).

This solution will be sufficient to solve the first and the second subtasks.

How to get 50 points

At this point, we won't speed the modifications up, but will improve the execution time of the subset-sum part.

For doing this, we will have two crucial optimizations.

The first optimization

Now it's time to remember that there are only K ≥ 10 types of the element of the array a[] (aka weight-plates). During each of the subset-sum queries we process R-L+1 elements of a[] which can be optimized to O(K log (N / K)) = O(K log N) - considering the small value of K it will be good.

Let's calculate the number of weight plates of each kind. Now, consider a single kind of weight plates and suppose that there are X plates of this kind. Let's create the new summands for our subset-sum problem by grouping the weight plates:

  • The first summand should be equal to the weight of one weight plate;
  • The second summand should be equal to the weight of two weight plates;
  • The third summand should be equal to the weight of four weight plates;
  • And so on: the ith summand should be equal to the weight of 2(i-1) weight plates;

At some moment, you won't be able to create one more summand. This moment is when the amount of the weight plates you need for the new group is greater than the amount of the weight plates that are left. In this case, the last group should contain all the remaining weight plates.

Now, let's consider some examples of making the groups for the different amount of weight plates:

  • If you have 1 weight plate, there will be a single group containing this weight plate;
  • If you have 2 weight plates, there will be two groups, each containing a single weight plate;
  • If you have 3 weight plates, there will be two groups - the first containing a single weight plate and the second containing two weight plates;
  • If you have 4 weight plates, there will be three groups - the first containing a single weight plate, the second containing two weight plates and the third, again, containing a single weight plate.
  • Finally, if you have 100 weight plates, there will be 7 groups, containing 1, 2, 4, 8, 16, 32 and 37 weight plates respectively.

This way, for each kind of the weight plates, you will have no more than O(log N) summands that results in O(NQ + PWK log N) complexity.

The second optimization

Consider the version of the subset-sum DP that was shown in "How to get 25 points" part. You can note that all the elements of dp[] are either ones or zeroes and the operations on them are binary - to be more precise, only binary OR.

This enables us to use bitset, so we can speed the code up in 32 times.

Now we've got O(NQ + PWK log N / 32) solution that can additionally solve the third subtask. Such solution gets 50 points.

Getting full points

We've already achieved O(NQ + PWK log N / 32) complexity. Let's note that for the constraints of the problem the O(PWK log N / 32) part is already small enough. That means that now we process the queries of the third kind fast enough. Now we need to improve the processing of the queries of the first and the second kind.

Both these queries are actually correspond to a very standard problem - maintain the array under the following queries:

  • Reverse a subarray of the array in O(log N) time;
  • Change the value of a single array's element in O(log N) time;

This problem is easily solvable with a data structure called treap.

In our problem, there is one more query that should be maintained:

  • Find the amount of the weight plates of each kind on a given segment

For doing this, we can simply store an array of K elements in each node of the treap, where the ith element of this array will denote the number of the weight plated of the ith kind on the corresponding segments. That gives us O(K log N)-per-modification time for the modification query of the both - the first and the second kinds.

Eventually, we get the complexity of O(QK log N + PWK log N / 32). This solution passes all the subtasks and gets 100 points.

Setter's Solution

Can be found here

Tester's Solution

Can be found here

[How ??] Create Tag

$
0
0

How Do I create new tags ?? https://blog.codechef.com/2014/11/18/the-new-karma-system/ say Minimum reputation to create tags 250 karma. I have sufficient karmas. But when I tried to create tage of my username @aryanc403 It displays Unapproved tag. How do I create new tag which is approved also. Do I have to create tag and wait them for to be approved by @admin or not ?? If no then How do I approve them. And If yes then Do I need to ask @admin always to approve them.

P.S. - When I was having karma <250 . I was able to create unapproved tags. So what is difference I cannot see here.

problem with Modular exponentiation

$
0
0

Consider a code for Modular exponentiation assume we are calculating (x^n)% MOD

while(n>0)
{
    if(n&1)
    result = (result*x)%MOD;
    cout<<result<<"\n";
    x=(x*x)%MOD;

    n=n>>1;
}

suppose x = 4 and n is large say 100 and MOD is 10^9 +6

so my program has overflow and giving result in -ve number so in this program x can be very large < MOD and when we multiply it be result intermediate value is very large so how to do it
p.s i am newbie :)


Run Direction-Not able to understand

$
0
0

Can anyone explain the approach mentioned in the editorial for Run Direction in detail? I am not able to understand the greedy approach and also how can we apply binary search? I am not able to understand the intuition behind binary search on this problem. Thanks!

IOC - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Jarin Gupta
Tester:Aanya Jindal
Editorialist:Gaurav Aggarwal

DIFFICULTY:

EASY

PREREQUISITES

Sorting

PROBLEM:

N cakes are given. Cut k of them parallel to any edge to maximise the sum of perimeters of rectangles.

EXPLANATION:

We have a total of N cakes, each ith cake having a length li and breadth bi (1 <= i <= n).

For any ith cake, to divide it into 2 rectangles and maximise the perimeter, we have to cut it parallel to Xi = max(li,bi). The extra perimeter so formed will be Yi = 2 * Xi.

horizontal cut

vertical cut

So we store the extra perimeters each cake gives, sort them and pick the k maximum cakes from them to cut.

AUTHOR'S AND TESTER'S SOLUTIONS:

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

Can anyone help me with Coding Arena from TCS Codevita 2017 Contest?

problem in understanding limit for value of "a" in NUKES problem?

$
0
0

The problem NUKES, in this problem the value of "a" can range up to 1000000000 but some programmers have used integer array to store the values of a,n and k and still got accepted. you can see it in this problem. How is this possible.

AVGPR last subtask is failing :/

Viewing all 39796 articles
Browse latest View live


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