Quantcast
Viewing all 39796 articles
Browse latest View live

getting wrong answer in KMXOR checked all the cases given in the editorial


Invitation to ProCon Junior 2018

"Code to fight, Code to survive, Code to win!"

Hello Everyone!

I would like to invite you all to participate in ProCon Junior to be held on CodeChef. It will begin at 19:30 IST on August 11, 2018.

The problems have been made by me (prakhar17252), Madhav Sainanee (madhav_1999) and Tanmay Bansal (tanmay28).

You will be given 7 problems to solve in 2.5 hours. The competition is open for all and rated for Div-2.

Top school students studying between classes VI-XII (or equivalent) in an Indian school will then be invited to the onsite finals to be held at Esya'18, the Technical Fest of IIIT-Delhi.

School Students, register here for onsite finals: bit.ly/PRCNJR18

Prizes worth Rs. 20000 for the top rankers in the onsite finals.

This is my first time hosting a contest, and that too a rated one! Hope you all like the problems!

Hope everyone has high ratings!

Good Luck!

Happy Coding!

what is the solution for time limit exceeded

My solution is showing time limit exceeded. What all can I do to solve this problem?

FLIPCOIN Editorial ?

Python input error

When i try to run a code in python on codechef it gives EOF error.

But the same thing if run with custom input it runs perfectly.

Add networkx in python

If possible, and if it is NOT unfair to other participants, Can codechef consider adding networkx library in python? It is very helpful library in manipulating graphs. Thanks.

Procon junior chef and pizza

How to solve “chef and pizza“ problem from procon junior?

penalty in short contest

how exactly is penalty calculated in short contests?


Why does this code not cause a SIGSEGV?

Why does this code not cause a segmentation fault when it is supplied with input 1 1000?

#include <iostream>
#define MAX 1000

using namespace std;

long long dp[MAX] = {-1};
long long func(int n)
{
    if(dp[n] > 0)
    {
        return dp[n];
    }
    else {
        return dp[n] = (n * n) + func(n - 2);
    }
}
int main()
{
    dp[0] = 0;
    dp[1] = 1;
    dp[2] = 4;
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        cout << func(n) << endl;
    }
    return 0;
}

Internal Server Error in LTIME62 Forced Particles question

@admin I have been trying to submit solution for this problem for the past one day, but every time I'm getting internal server error on the submission result page!

repeating of staement

#include <stdio.h>

int main(void) { float bal,total; int amt; printf("enter the amount"); scanf("%d%f",&amt,&bal); if(amt%5==0 && amt<bal) { total=bal-amt; printf("the balance is=%f\n",total); } else { printf("you have entered the wrong text%f\n",bal); } return 0; }

why enter the amount statement is repeating with the output of result

QM10P5A editorial

PROBLEM LINK:

Practice
Contest

Author:Chandan Boruah
Tester:Taranpreet Singh
Editorialist:TaranPreet Singh

DIFFICULTY:

Simple

PREREQUISITES:

Greedy

PROBLEM:

Given N balls, each ball colored either red or blue, we have to find maximum number of green balls we can make. A Green ball can be made by merging consecutive red and blue ball.

QUICK EXPLANATION

  • Final composition of balls would never have consecutive balls are red and blue, since we can always merge them to obtain one more green ball.
  • So, merging balls greedily would generate maximum number of green balls.

EXPLANATION

All we need to do is to merge red and blue ball whenever we find a pair of such balls. All we need to prove now is that this approach is optimal.

Consider example: RBRB

Here, if we choose to merge second and third ball, our final compostion would look something like this RGB. Since we cannot move Green ball from in-between, we cannot merge the remaining red and blue ball. This proves that we should perform the merge operation as early as possible, otherwise Green ball might block future merge operation.

Instead of pair (2, 3), had we chosen (1, 2), we can still merge (3, 4) getting 2 green balls in the end, thus, being the optimal approach for merging.

Complexity:
Time Complexity is O(N).

AUTHOR'S SOLUTIONS:

using System;
using System.Collections.Generic;
class some
{
    public static void Main()
    {
        int tests=int.Parse(Console.ReadLine());
        for(int k=0;k<tests;k++)
        {
            int t=int.Parse(Console.ReadLine());
            string s=Console.ReadLine();
            int max=0;
            int count=0;
            for(int i=0;i<t-1;)
            {
                if(s[i]!=s[i+1]){count++;i+=2;}
    else i++;
            }
            max=count;
            count=0;
            for(int i=1;i<t;)
            {
                if(s[i]!=s[i-1]){count++;i+=2;}
    else i++;
            }
            max=Math.Max(count,max);
            Console.WriteLine(max);
        }
    }
}

QM10P5 editorial

PROBLEM LINK:

Practice
Contest

Author:Chandan Boruah
Tester:Taranpreet Singh
Editorialist:TaranPreet Singh

DIFFICULTY:

Easy

PREREQUISITES:

Slope of a line, Observation.

PROBLEM:

Given $N$ lines represented by two points, we need to find maximum number of lines which can pass through a single point, without superimposing any other line. we can move any line but not rotate it.

QUICK EXPLANATION

  • Represent lines as pair $(m, c)$ where line can be given as $y = mx+c$, called line slope form. We can now see that we can change the $c$ for any line, but cannot modify $m$.
  • Lines with Same value of slope ($m$) are parallel to each other, given ($c_{1} \neq c_{2}$).
  • No two parallel lines can pass through same point without superimposing each other.
  • Our problem reduces to finding different values of slopes from the given set of lines.

EXPLANATION

The quick Explanation says it all. :-)

We can calculate slope of a line as $(y_2 - y_1)/(x_2-x_1)$, add them to a set and count the number of distinct values of slope in set.

BUT BUT, what about REs we were getting in contest??

This was because of some lines having $x_1 = x_2$, test case specially added by devil tester :D, which caused arithmetic Division by zero error.

We need to handle vertical (and probably horizontal too, if we wish) lines separately. Maybe having two boolean values horizontal and vertical would do fine. Each boolean value indicate whether input set contains a vertical (or horizontal) line.

If true, would each of them contribute 1 to final answer.

Complexity:
Time Complexity is $O(N)$.

AUTHOR'S SOLUTIONS:

using System;
using System.Collections.Generic;
class some
{
    public static void Main()
    {
        int t=int.Parse(Console.ReadLine());
        while((t--)>0)
        {
            int n=int.Parse(Console.ReadLine());
            int[]x1=new int[n];
            int[]y1=new int[n];
            int[]x2=new int[n];
            int[]y2=new int[n];
            for(int i=0;i<n;i++)
            {
                string[]ss=Console.ReadLine().Split();
                x1[i]=int.Parse(ss[0]);
                y1[i]=int.Parse(ss[1]);
                x2[i]=int.Parse(ss[2]);
                y2[i]=int.Parse(ss[3]);
            }
            SortedSet<double>slopes=new SortedSet<double>();
            for(int i=0;i<n;i++)
            {
                double slope=0;
                if(y1[i]==y2[i])
                {
                    slope=int.MaxValue;
                }
                else
                    slope=(x1[i]-x2[i])*1.0/(y1[i]-y2[i]);
                if(!slopes.Contains(slope))
                {
                    slopes.Add(slope);
                }
            }
            Console.WriteLine(slopes.Count);
        }
    }
}

QM10P5B editorial

PROBLEM LINK:

Practice
Contest

Author:Chandan Boruah
Tester:Taranpreet Singh
Editorialist:TaranPreet Singh

DIFFICULTY:

Easy

PREREQUISITES:

Slope of a line, Observation.

PROBLEM:

Given $N$ lines represented by two points, we need to find maximum number of lines which can pass through a single point, without superimposing any other line. we can move any line but not rotate it.

QUICK EXPLANATION

  • Represent lines as pair $(m, c)$ where line can be given as $y = mx+c$, called line slope form. We can now see that we can change the $c$ for any line, but cannot modify $m$.
  • Lines with Same value of slope ($m$) are parallel to each other, given ($c_{1} \neq c_{2}$).
  • No two parallel lines can pass through same point without superimposing each other.
  • Our problem reduces to finding different values of slopes from the given set of lines.

EXPLANATION

The quick Explanation says it all. :-)

We can calculate slope of a line as $(y_2 - y_1)/(x_2-x_1)$, add them to a set and count the number of distinct values of slope in set.

BUT BUT, what about REs we were getting in contest??

This was because of some lines having $x_1 = x_2$, test case specially added by devil tester :D, which caused arithmetic Division by zero error.

We need to handle vertical (and probably horizontal too, if we wish) lines separately. Maybe having two boolean values horizontal and vertical would do fine. Each boolean value indicate whether input set contains a vertical (or horizontal) line.

If true, would each of them contribute 1 to final answer.

Complexity:
Time Complexity is $O(N)$.

AUTHOR'S SOLUTIONS:

using System;
using System.Collections.Generic;
class some
{
    public static void Main()
    {
        int t=int.Parse(Console.ReadLine());
        while((t--)>0)
        {
            int n=int.Parse(Console.ReadLine());
            int[]x1=new int[n];
            int[]y1=new int[n];
            int[]x2=new int[n];
            int[]y2=new int[n];
            for(int i=0;i<n;i++)
            {
                string[]ss=Console.ReadLine().Split();
                x1[i]=int.Parse(ss[0]);
                y1[i]=int.Parse(ss[1]);
                x2[i]=int.Parse(ss[2]);
                y2[i]=int.Parse(ss[3]);
            }
            SortedSet<double>slopes=new SortedSet<double>();
            for(int i=0;i<n;i++)
            {
                double slope=0;
                if(y1[i]==y2[i])
                {
                    slope=int.MaxValue;
                }
                else
                    slope=(x1[i]-x2[i])*1.0/(y1[i]-y2[i]);
                if(!slopes.Contains(slope))
                {
                    slopes.Add(slope);
                }
            }
            Console.WriteLine(slopes.Count);
        }
    }
}

A Learning Module for Beginners (Currently in Development, Feedback requested)

Hi fellow programmers,

We are planning to create a course for beginners. Beginners often come to the site and got overwhelmed by so many problems present on the site. Many of them often ask where to start from. So, we thought of creating a course module for them. The module will be a set of tutorials and a set of practice contests on CodeChef.

For this module, we will identify the key set of topics that a beginner should solve in order to reach a certain level. For each topic, we will have some 5 to 10 or more problems on that topic that she should solve. I have collected some set of problems for following topics (in increasing order of the difficulty). Please feel free to give any relevant feedback, suggest some problems that you think can be added or removed. Any suggestions regarding would be most welcome.

Note that currently, this module is not ready. It's still work in progress. I have marked this post a community wiki. Please feel free to edit it.

Module #0: What's competitive programming?

Competitive Programming is a mind sport where participants solve problems by writing code. The problems in question are typically logical, mathematical or algorithmic in nature. Participants must write code according to given specifications which typically includes a pre-specified time and memory limits within which a program must successfully complete execution.

Two of the most prominent competitive programming competitions are the ACM International Collegiate Programming Competition (ICPC) and the International Olympiad of Informatics (IOI) which are for univeristy and high school students respectively. The ICPC is one of the oldest, largest, hardest and most prestigious programming competitions in the world, and it is widely considered as the "Olympics" of programming.

Some other prominent programming competitons are

  • Google Code Jam
  • Facebook Hackercup
  • Topcoder Open (TCO) Algorithm
  • Yandex Algorithm

Apart from the above, regular programming competitions are conducted on various platforms across the intetnet. Some of these are

  • Codechef
  • Codeforces
  • Hackerrank
  • Topcoder
  • Hackerearth
  • Atcoder
  • CSAcademy

Getting started with CodeChef platform

Module #1: Learning A Programming Language

Check https://www.codechef.com/ioi/basics, the section "Language Constructs - C++ - 1" for basic knowledge of C++. If you want to learn basics of other languages like Java, Python, you can refer to (need to provide more links).

Just get it done :) Starting questions to do

  • FLOW001 (add two numbers)
  • FLOW002 (find reminder of a number when divided by other number)
  • TEST (one of the starting problem to solve on CodeChef)
  • INTEST (for testing whether your input/output routines are fast enough)

Basic knowledge of if/else conditions

  • FLOW010 (basic knowledge of if/else conditions)
  • FLOW014 (another if/else knowledge question)
  • LADDU (very nice implementation problem to solve)

Dealing with numbers

  • FLOW002 (find remainder of a number a % b)
  • FLOW004 (find sum of first and last digit of a number)
  • FLOW006 (find sum of digits of a number)
  • FLOW007 (given a number, reverse it)
  • FCTRL (an interesting question, must solve)

Basic knowledge of arrays

Working with strings

  • NITIKA (reading strings, and string related formatting)
  • GOODBAD (ASCII characters, lower-case and uppercase characters)
  • FLOW015 (checks basic knowledge of strings and calendar)
  • FRGTNLNG (very nice question to test input processing skills)
  • LAPIN (check whether a string is a palindrome or not)
  • CSUB (good for understanding notion of substrings)

Handling floating point numbers

Off to recursion

Provide Few More Implementation Problems:

Space and time complexity of a program

Module #2: (Basic Mathematics)

Please see https://www.codechef.com/ioi/basics, Mathematics section for very basic mathematics.

Basic Number Theory:

Module #3: Sorting algorithms

Module #4: Greedy Algorithms

Module #5: Stacks and Queues

Stack:

Queue:

  • CHFQUEUE (very nice stack and queue related problem, must do)
  • COOK82C (easy-med level problem)

Module #6: Binary Search:

Module #7: Basic Dynamic Programming

  • ALTARAY (basic dp problem)
  • SUMTRI (very nice and basic dp problem)
  • DBOY (very nice dp problem, must do)
  • XORSUB (basic 2-D dp)
  • STRMRG (a variation of standard LCS (longest common subsequence) problem, must do problem)
  • GRID (simple 2-D dp, prefix/suffix sums over a 2-D matrix, must do)
  • STRSUB (dp with binary search)

Module #8: Basic Graph Theory

Depth first search

  • FROGV (can also be done without requiring dfs by making some simple observations, in fact, dfs is overkill for it)
  • FIRESC (must do dfs problem, can also be done using union-find algorithm)
  • CHFNFRN (check whether a graph is bipartite)
  • FRIEMEET
  • DIVIDE (binary search with bipartite checking type logic)
  • MCO16104 (consider the inverse graph)
  • AUEC (existence of euler circuit in a graph)

Breadth first search

Union Find

  • BIGOF01 (very basic problem on union find, must do)
  • COLGQU (slight harder version on union find)
  • DISHOWN
  • CHFNFRN (check whether a graph is bipartite)
  • MTRWY (slight harder version on union find)
  • JABO
  • FILLMTR (very nice problem on union find, intermediate level, you may also check the video tutorials)
  • GALACTIK (a very nice union find problem)
  • PARITREE (intermediate level problem)
  • SETELE (much recommended problem)
  • MAGICSTR (recommended only for intermediate+ level)

Advanced features of languages

Check Language Constructs. It's good to learn Standard Template Library (STL) - C++ 2 section in https://www.codechef.com/ioi/basics for reading.


Can anyone be blamed for plagiarism in Practice Questions ?

After contests are over, I do look at solutions of other coders and try understand and and improve solutions for lower time..... Can I be warned / banned in this case ?

Why is my solution wrong for practice Question FLOW006

include<stdio.h>

main() { int t,i,n,sum=0; scanf("\n%d",&t); for(i=0;i<t;i++) { scanf("%d",&n); sum=0; while(n!=0) { sum=sum+n%10; n=n/10; } printf("\n%d",sum); } return 0; }

Why is my solution is wrong for practice Question FLOW006

include<stdio.h>

main() { int t,i,n,sum=0; scanf("\n%d",&t); for(i=0;i<t;i++) { scanf("%d",&n); sum=0; while(n!=0) { sum=sum+n%10; n=n/10; } printf("\n%d",sum); } return 0; }

how many approx loops are allowed in 2.5 sec lime limit

How many approx loops can be traversed in 2.5 sec? And what should be best complexity to traverse 10^6?

Is Code Chef a competitive site or can I learn to code also here?

I am new to coding and in search of online certification courses in Coding and Programming. So is this a site for me?

Viewing all 39796 articles
Browse latest View live


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