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

Fast string input

$
0
0

What is the fastest way to input strings in c and c++?

I know there are many ways to get integers fast but I was not able to find anything that helps with strings especially char strings.


BYTEISLE - Editorial

$
0
0

PROBLEM LINKS

Practice
Contest

DIFFICULTY

EASY

EXPLANATION

Note that the type of each Bytelandian is completely determined by the number of Byteknights in the island. Because there are between 0 and N Bytelandians, the number of solutions is actually only between 0 and N (modulo 1000000007 :P).

Firstly, we have to determine the number of Byteknights. Let knight[n] be the number of Bytelandian that satisfies a _ i <= n <= b _ i; that is, the number of Bytelandians that said it is possible to have n Byteknights in the island. It is easy to see that the number of Byteknights can be n iff knight[n] = n.

So, we need a data structure that can perform these operations efficiently:
1. Increase the values of knight[a _ i]..knight[b _ i], for each Bytelandian i by 1
3. Query the value of knight[i], for each 0 <= i <= N

Because all queries are needed only after the updates, the so-called 'partial difference' data structure suffices. Let diff[i] be knight[i]-knight[i-1]. Operation 1 can be simulated by increasing diff[a _ i] and decreasing diff[b _ i + 1]. Operation 2 is simply the sum of diff[1]..diff[i], which can be precalculated after the updates take place.

Secondly, we have to determine the lexicographically smallest solution. If it is possible for a Bytelandian to be a Byteknaves, then we should assign it Byteknaves, otherwise the solution would not be lexicographically smallest because '1' is greater than '0'. A Bytelandian i can be assigned Byteknave iff there is n is a possible number of Byteknights (valid solution) and n is not in range a _ i..b _ i, i.e., he is lying.

A segment tree will perform the step efficiently. Let tree[n] = 1 if knight[n] = n, or 0 otherwise. Then we can query the number of valid solutions in a range in logarithmic time. After we decide that Bytelandian i is a Byteknave, we set all elements of the tree in the range a _ i..b _ i to zero. If it is not possible for Bytelandian i to be a Byteknave, because the number of valid solutions in the range a _ i .. b _ i is the same of the number of valid solutions in the range 0..N, we must assign it a Byteknight.

SETTER'S SOLUTION

Can be found here.

TESTER'S SOLUTION

Can be found here.

Codechef WA/TLE test case display feature request

$
0
0

Like on codeforces, wouldn't it be great to have an option to see for which test cases your code gets a WA/TLE on codechef practice section as well ? Each person can debug his own code. They dont have to ask each time on the forum for some one else to look at their code and debug it which in itself is a very tedious task and hence many a times questions like those (WA in xyz qts) go unanswered. The learning process can be a lot faster - user doesnt have to wait for help unless he doesn't understand the problem completely.

There are times when users use completely different/unique algorithms in contests and get WA. After the contest, they refer to the editorial only to find a different algorithm and eventually let go. In such situations, there might be cases when the user might just have missed one line of cases or may be some corner case. However in many cases, he never finds out what it was.

Now some might claim that this will make it easy to get AC in practice compared to contest problems. Well that is true but then why not just leave that choice to the individual user - if he thinks that this feature is not benefiting him - he can choose not to use it and solve problems traditionally.

Well these are just my thoughts - would like to know what you guys think. Peace :)

segment tree problem

$
0
0

problem link :-http://www.spoj.com/problems/GSS1/ why i m getting WA

enter code here
#include<iostream>
#include<cmath>
#include<utility>
#include<cstdio>
using namespace std;
int N=50000;
int M[1<<23][2];
int a[1<<23];
void initialize(int node,int i,int j)
{
if(i==j)
{
   if(a[i]>0)
   M[node][0]=a[i];
   else
M[node][0]=0;
M[node][1]=a[i];
}
else
{      
 initialize(2*node,i,(i+j)/2);
 initialize(2*node+1,(i+j)/2+1,j);
  M[node][0]=M[2*node][0]+M[2*node+1][0];
  M[node][1]=M[2*node][1]>M[2*node+1][1]?M[2*node][1]:M[2*node+1][1];
 }
 }
 pair<int,int> query(int node,int b,int e,int i,int j)
 {
  pair<int,int>  p1,p2;
   if(i>e||j<b)
    return make_pair(0,-15008);
    if(b>=i&&e<=j)
    return make_pair(M[node][0],M[node][1]);
    p1=query(2*node,b,(b+e)/2,i,j);
    p2=query(2*node+1,(b+e)/2+1,e,i,j);  
    return make_pair(p1.first+p2.first,max(p1.second,p2.second));

  } 
  int main()
  {
    pair<int,int> AL;
int m,n;
cin>>n;
for(int i=0;i<n;i++)
    cin>>a[i];
initialize(1,0,n-1);
cin>>m;
int x,y;
while(m--)
{
cin>>x>>y;
AL=query(1,0,n-1,x-1,y-1);
if(AL.first==0)
cout<<AL.second<<endl;
else
   cout<<AL.first<<endl;

}

   }

Please help me by figuring out what's causing NZEC Run time error in this c# code block

$
0
0

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        static int Main(string[] args)
        {
            Double x,y;
            x = double.Parse(Console.ReadLine());
            y = double.Parse(Console.ReadLine());
            if (x > 0)
            {
                if (x % 5 == 0 && y - x > 0.5)
                {
                    y-=0.5;
                    Console.Write(y - x);

                }
                else
                {
                    Console.Write(y);
                }

            }
            else
        {
            Console.Write(y);
        }
        Console.ReadLine();
        return 0;

    }
}

}

RETPO - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Vitalij Kozhukhivskij
Tester:Shang Jingbo and Gerald Agapov
Editorialist:Devendra Agarwal

DIFFICULTY:

SIMPLE

PREREQUISITES:

Maths.

PROBLEM:

You need to reach point (x,y) from (0,0) using snake game rule only difference is that snake cannot travel straight for more than 1 unit and the first step is in the Y-axis.

Explanation

As the snake move is totally symmetrical , we can assume that number of steps required to reach from (0,0) to (x,y) is independent of the sign of x and y.

Reason
First move is in either positive Y or negative Y axis. From there , snake can either move in negative or positive x axis and so on.

Case 1: Let us solve the case when y = 0, i.e steps required to reach (x,0) from (0,0):

Wlog we can assume that x is positive . Now if x is odd , then total number of 2x+1 steps are required.
Proof is immediate by sketching a small diagram [ Remember that the first move is towards Y axis ] . If x is even , then total number of steps required is 2
x , proof by sketching.

Case 2: Let us solve the case when x = 0, i.e steps required to reach (0,y) from (0,0):

Wlog we can assume that y is positive . Now if y is odd , then total number of 2y-1 steps are required . If y is even , then total number of 2y steps are required.

Case 3: Let us now solve the case when neither x nor y are zero :

Till now , you must have realized that the snake move is perpendicular and it will cover 1 unit in y and 1 unit in x direction . So first move to (min(x,y) , min(x,y) ) and cover the remaining distance by the above cases discussed.

Pseudo Code

get_answer(x,y):
    x= abs(x);  //get the absolute value of x
    y=abs(y);   //get the absolute value of y
    z=min(x,y); //get the minimum of x and y
    return 2*z + val(x-z,y-z))

// if a is 0 , then it is case 2 , else it is case 1.

val (a , b) :       //either case 1 or case 2
    return ( (a&1) ? (2*a+1) : (2*a) ) + ( (b&1) ? (2*b-1) : (2*b)  );

Complexity:

O(1).

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution
Tester's solution

Code Explanation for LEPERMUT @qzthrone

$
0
0

Hi
I tried to figure it out, but I didn't get. can any one explain this code. what exactly doing.

for i in range(N):
    A[i] -= i + 1;
    if A[i] > 0:
        inversions += A[i];
    elif A[i] < -1:
        inversions -= A[i] + 1;

It's about logic, what he/she righten. click here for orginal code

Error on SIGFPE?

$
0
0

I am using int datatype in my code, but I am having % operations, I check about divide by 0 error also.

There is any more possibility of occurring error.


TLG: WHY IS IT GIVING WRONG ANSWER?

$
0
0
    #include<iostream>
    #include<stdio.h>
    #include<cmath>
    using namespace std;
    int main()
    {
        int n,s[10000],t[10000],i,temp[10000],ans,k=0;
        scanf("%d",&n);
        ans=temp[0];
        for(i=0;i<n;i++)
        {
                scanf("%d",&s[i]);
                scanf("%d",&t[i]);
                temp[i]=abs(s[i]-t[i]);

        }
        for(i=0;i<n;i++)
        {
            if(temp[i]>ans)
            {
                ans=temp[i];
                k=i;
            }
        }
        if(s[k]>t[k])
        printf("1  ");
        else
        printf("2  ");
        printf("%d",ans);
            return 0;
    }

PRGIFT - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Praveen Dhinwa
Tester:Praveen Dhinwa and Hiroto Sekido
Editorialist:Lalit Kundu

DIFFICULTY:

CakeWalk

PREREQUISITES:

None at all :)

PROBLEM:

Given an array of integers a1,a2...an, check whether there is a subarray which consists of exactly k even integers.

EXPLANATION:

Since n<=50, you can traverse over all subarrays and count how many even numbers are there in each substring. Complexity: O(N3).

Or, we can do this cleverly. If there are atleast k even numbers in the whole array, we can always pick a subarray which will contain k even numbers.

One tricky case would be k=0, and all numbers are even in the array. In this case we can't pick any subarray.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution
Tester's solution

small factorials

$
0
0

The code runs fine on my compiler and other IDE's present online,but in codechef it gives me an error,i don't know where the problem lies,can anyone help me out in this?

Code:

#include<stdio.h>

int main() {
long long n,t;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
int f=1;
while(n>0){
f=f*n;
n--;
}
printf("%lld",f);
}
return 0;
}

TSHIRTS - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Lalit Kundu
Tester:Praveen Dhinwa and Hiroto Sekido
Editorialist:Lalit Kundu

DIFFICULTY:

EASY-MEDIUM

PREREQUISITES:

Dynamic Programming, Bitmasking, Recursion

PROBLEM:

There are N(<11) persons. Each person has his collection of distinct tshirts. What are the total number of different such arrangements such in which no two people wear same kind of tshirt. A person can have atmost 100 distinct tshirts.

EXPLANATION:

It is a very standard kind of Dynamic Programming with bitmasking. So the idea here is to keep a 2-D array DP[2N][N], where DP[i][j] will store the answer only if tshirts from id 1 to j have been used. Also, let's say i when denoted in binary be b1,b2...bn. If bp(1 ≤ p ≤ n) is 1, it means that person with id==p has been alloted a tshirt and all persons with bits 1 are assigned distinct tshirts.
So, we'll write a recursive DP, which is obviously easier to write.
We keep a function rec(mask, tid) which means that tshirts till id tid have been processed and mask denotes which persons have been given tshirts. At each step we assign tshirts to each possible person and recursively calculate the number of ways.a
Following pseudo code will make it more clear.

dp[1<<N][N]={}
memset dp to -1    // dp[i][j]=-1 means it hasn't been calculated yet
def rec(mask, tid):  // tid denotes the tshirt id
    if mask==2**(N)-1:  // all bits are set, all persons have been alloted tshirts
    return dp[mask][tid]=1
    if tid==101:    // all tshirts finished
    return 0
    if dp[mask][tid]!=-1:   // it has already been calculated, we won't calculate it again
    return dp[mask][tid]
    ans=0
    ans += rec(mask,tid+1)  // the case when we don't assign tshirt with id=tid to anyone
    // the case when we assign tshirt with id=tid to someone
    // we will assign the tshirt with id=tid to all possible persons we can, and add to answer the respective number of ways
    // note that we are assigning distinct tshirts only, since tshirt with id=tid has never been assigned before to anyone.
    for persons p(1<=p<=N) which have tshirt with id=tid:
    if mask&(1<<p): // person p has already been alloted a tshirt
        continue    // do nothing
    ans += rec(mask|(1<<p),tid+1)   // assign tshirt tid to person p and add to answer next cases

    return dp[mask][tid]=ans;

Our answer will be rec(0,1) ie. starting with mask=0(no one has been assigned any tshirt) and tid=1. Complexity will be in worst case O(K*2N).

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution
Tester's solution

FURTHER LINKS AND SIMILAR PROBLEMS

A little bit of classics: dynamic programming over subsets and paths in graphs
SOCOLA
Topcoder Problem
Dp with Bitmasking

Does CodeChef have any Code Of Conduct?

$
0
0

I am new to CodeChef. I am not sure what can be considered as an improper conduct on this website. I do not want to be penalized for doing something unknowingly which is not the accepted behavior. Is there any such Code of Conduct?

participate in august cook off

$
0
0

when i click on register link in august cook off it saya access denied help me

Runtime Error(SIGSEGV) in Kisses and Hugs problem


Relevant Phrases of Annihilation - problem in submission

$
0
0

Hello Friends,

I have created a solution for Medium practice problem "Relevant Phrases of Annihilation".

I have tested it and it is working fine for all the cases but it is showing as wrong answer.

It seems there is some problem in input and output format. This is my first problem so i would like to have some help here.

Solution is in Java language with username sgpandey123

Thanks, Sanjay

uva 10459 - Find all the best and worst roots of an undirected unweighted tree.

$
0
0

Given an undirected tree, how can I find out those vertices which when taken as the root of the tree will minimize the depth of the tree. Similarly, I want to find those vertices which when taken as the root of tree, will maximize the depth of tree. I want to answer this for a general tree in linear time. (Linear in the number of vertices of the tree)

Uva 10459 link to the question

Post office problem in hard

$
0
0

Respective sir/madam, I have submitted the code for post office problem,but it is showing runtime error sigsegv though I have checked the array references perfectly.I can't understand what to do.I will be highly obliged if you kindly help me out in this problem..

tsort error

$
0
0

getting a runtime error, can anyone help me out in this?

code:

include <stdio.h>

void main(){
int a[10^6];
int i,t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
for(i=0;i<n;i++)
printf("%d",a[i]);
}
}
int sort(int a[10^6],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return 0;
}

What are some good questions on Bitwise operations?

$
0
0

I am weak in bitwise operations, so I want to practice questions related to bitwise operations & shifting.

I know these questions: PPTREE,PPXOR,FAULT,TAAND,CHXORR,RRFRNDS. If anyone knows more , please add. Thanks.

Viewing all 39796 articles
Browse latest View live


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