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

Need some help in TWOFL

$
0
0

I have understood what I have to do in the problem. I implemented the solution for the first subtask, it went well. But for the other subtasks, I am not able to understand the DSU part. I found out the length of each component having same integer from the grid using BFS. So far, I know that there are different sets (here components) and we can merge them and the index of each component will be representing the set (component). But, how are we finding if the components are adjacent? Also, how are we managing the situation where we need to merge an already connected component to the next component.

I went through the Author's solution but I am not able to understand what he did after finding the length of all components. Can anyone help me here understand the last part.

Here is the solution I prepared till now: https://ideone.com/fzVkPV


WA ZCO15001

Problem "TLG"

$
0
0

What is the Problem in this Program? i will run on my desktop it's run Perfectly when i upload this same program on codechef that make error "Wrong Answer" why??

include<iostream>

using namespace std; int main(){ long t, i; cin>>t; long a[t], b[t], winPlayer[t],lead[t], finalLead = 0, temp = 0; for(i=0; i < t; i++) { cin>>a[i]>>b[i]; } for(i=0;i<t;i++) {="" if(a[i]="">b[i]) { lead[i] = a[i] - b[i]; winPlayer[i] = 1; } else if( b[i] > a[i] ){ lead[i] = b[i] - a[i]; winPlayer[i] = 2; } } finalLead = lead[0]; for( i = 1; i < t; i++){ if( finalLead < lead[i] ){ finalLead = lead[i]; temp = i; } } cout<<winPlayer[temp] <<" "<<finalLead; return 0; }

Getting wa even after using same approach as editorial

why so much less contests are being conducted ???

$
0
0

I want to ask that why so much less contests are being conducted in MAY relative to MARCH or APRIL. As i have summer vacations now and i am all free for enjoying contests .only challenge,cookoff and lunchtime are being conducted.

I request admin to conduct more programming contests as most of us are free in summer vacations. It will be really helpul if u can do anything in this direction.

It's just what i feel

no intentions for hurting anyone's feelings :)

BfS code for short coding contests.

$
0
0

I just studied BFS. I am trying to find short and simple DFS code/snippet for short programming contests.If anyone has shortest and simplest implementation of BFS, let me know.

Discord Server: Coder Community

$
0
0

We at Blancode have take an initiative to bring all the coders from the world under one roof.

A Discord community to share , discuss , learn and grow.

here is link :https://discord.gg/yfr3Hza

Thanks to @puneetrai04 and @karngyan for making this server.

Note:we have strict rules if anyone who uses inappropriate language will be thrown out.

Do not hesitate to ask questions.

ARCTR - Editorial

$
0
0

Problem Link

Practice

Contest

Author:Igor Barenblat

Tester:Misha Chorniy

Editorialist:Bhuvnesh Jain

Difficulty

MEDIUM-HARD

Prerequisites

Dynamic Convex-Hull Trick, Heavy-Light Decomposition

Problem

You are given a tree with $N$ nodes. $M$ speedsters travel on the tree. The $i^{th}$ speedster starts at time $t_i$ from vertex $u_i$ and travels towards $v_i$ at a constant speed of $s_i$. For every vertex, we need to find the first time, any speedster visited it. In case, it was not visited by any speedster, report the answer as -1.

Explanation

For simplicity, let us assume that the tree is rooted at node $1$ and the depth of all vertices from the root is calculated. The depth is basically the distance of the vertex from the root of the tree i.e. $1$.

Let us first write the equation for time taken by speedster $i$ reach a vertex $x$. If the vertex doesn't lie on the path from $u_i$ to $v_i$ then it is not visited by speedster $i$, or the time taken is INFINITY (a large constant). For all the other vertices on the directed path from $u_i$ to $v_i$, the time taken is given by:

$$\text{Time taken} = t_i + \frac{\text{Distance from vertex }u_i}{s_i}$$

$$\text{Distance between x and y} = \text{Depth[x]} + \text{Depth[y]} - 2 * \text{Depth[lca(x, y)]}$$

where $lca(x, y)$ is the lowest common ancestor of vertices $x$ and $y$.

We can now modify the equation for the time taken to reach any vertex on the path from $u_i$ to $v_i$ as follows:

Let the lowest common ancestor of $u_i$ and $v_i$ be $lca$. Calculate the final time at which we reach vertex $v_i$. Let us denote this by $t_f$. We now split the path from $u_i$ to $v_i$ into 2 parts: one from $u_i$ to $lca$ and from $lca$ to $v_i$. NIte that these paths are directed. The image below shows how to calculate the time at any vertex $x$ and $y$ on the 2 different paths.

From the above figure, for a node $x$ on path from $u_i$ to $lca$, the time to reach it is:

$$\text{Time taken to reach x} = t_i + \frac{(Depth[u] - Depth[x])}{s_i} = \big(t_i + \frac{Depth[u]}{s_i}\big) - \frac{1}{s_i} * Depth[x]$$

Similarly, for a node $y$ on path from $lca$ to $v_i$, the time to reach it is:

$$\text{Time taken to reach y} = t_f - \frac{(Depth[v] - Depth[y])}{s_i} = \big(t_f - \frac{Depth[v]}{s_i}\big) - \frac{1}{s_i} * Depth[y]$$

If we observe carefully, both the above equations look the form $Y = MX + C$, where the first bracket part is $C$, time to be calculated is $Y$, $\frac{1}{s_i}$ is the slope ($M$) and the depth of the node is $X$.

The problem asks us to find minimum time at which every node is visited by any speedster, and the above equations clearly show that time to reach it only depends on the depth of the node and pair $(constant, slope)$ which is known beforehand for every speedster. Thus, this indicates the final solution will use the Dynamic convex-hull trick (Dynamic variant as the slopes are not guaranteed to be in increasing/decreasing order). If you don't know about it or its use case, you can read it here

So, let us first try to solve a simple version of the problem where the tree is a bamboo(a straight path). This basically rules out the tree of the problem and reduces it to updates and queries of the following form on an array:

  1. Update: Add a line $(M, C)$ denoting $Y = MX + C$ to every index in range $[l, r]$.

  2. Query: Find the minimum value at any index $l$ for a given value of $X = w$.

We have range updates and point queries. So, we will use segment trees for the solution. In each node of the segment tree, we will keep the lines (represented by $(M, C)$) and for querying, we will just use the convex-hull trick to evaluate the minimum at node efficiently. Below is the pseudo-code for the segment-tree:


    def init(t, i, j):
        if i == j:
            seg[t].clear()      # remove all the lines
            return
        mid = (i+j)/2
        init(2*t, i, mid)
        init(2*t, mid+1, j)

    def update(t, i, j, l, r, M, C):
        if i > r or j < l:
            return
        if l <= i and j <= r:
            # within required range
            seg[t].add_line({M, C})
            return
        mid = (i+j)/2
        update(2*t, i, mid, l, r, M, C)
        update(2*t+1, mid+1, j, l, r, M, C)

    def query(t, i, j, l, r, X):
        if l <= i and j <= r:
            return seg[t].evaluate_minimum(X)
        mid = (i+j)/2
        if i <= mid:
            if j <= mid:
                return query(2*t, i, mid, l, r, X)
            else:
                a = query(2*t, i, mid, l, r, X)
                b = query(2*t+1, mid+1, j, l, r, X)
                return min(a, b)
        else:
            return query(2*t+1, mid+1, j, l, r, X)

The time complexity of the above operations on segment tree is $O({\log{n}}^{2})$ for both update and query. This is because each update and query will visit at most $O(\log{n})$ nodes and operation on every node (addition of a line or querying for minimum) is $O(\log{n})$. For a good reference code to the Dynamic convex hull, you can look up this.

Back to the tree problem. We see that we can easily handle queries on an array and the queries on the tree as basically those on a path. Voila, we can simply use Heavy light Decomposition or any other data structure you are suitable with (Euler Path or Centroid Decomposition). Thus, we efficiently solve the problem.

You can look at the author's implementation for more details.

Feel free to share your approach, if it was somewhat different.

Time Complexity

$O((N + M) * {\log{N}}^3)$

Space Complexity

$O(N + M)$

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.


STOPSTALK related issue!

$
0
0

Why is Stopstalk not retrieving submissions from Codechef? It's showing some error related to downtime of Codechef, what does it mean?

TACNTSTR - editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Tuấn Anh Trần Đặng
Tester:Kamil Dębowski
Editorialist:Tuấn Anh Trần Đặng

DIFFICULTY:

Medium

PREREQUISITES:

Dynamic Programming

Problem

Given the string S count the number of the strings of same length T such that T is lexicographically bigger then S and when we reverse the order of characters in both of the two strings T is still lexicographically bigger than S.

Solution

We will using dynamic programing (dp). Let F[i][ok1][ok2] is the number of the strings T that we can generated if we already got the first i-1 characters of it and ok1 and ok2 represent the following information:

  • ok1 = 0 mean the first i - 1 characters of T still match the corresponding t - 1 characters of S. ok1 = 1 mean T is larger then S.
  • ok2 = 0 mean in the reversed order the first t - 1 characters in T is already lexicographically larger then the corresponding characters in S. ok2 = 0 if otherwise.

Let N is the length of S The result of course will be F[0][0][0]. We can initialize the dp with F[N + 1][1][1] = 1. We will calculate F in the decreasing order of i. With each i, ok1, ok2, we try to put all possible character in position i so that T is never lexicographically smaller than S in the original order:

//let s is 0-indexed
for (int i = N - 1; i > 0; i--)
    for (int ok1 = 0; ok1 <= 1; ok1++)
      for (int ok2 = 0; ok2 <= 1; ok2++) {
      //make sure that T is always lexicographically larger than S in original order
        for (char c = ok1 == 0 ? s[i] : 'A'; c <= 'Z'; c++) {
          int nextOk2 = ok2;
          if (c != s[pos]) {
            //if we put a character c > s[pos] in position pos of T then T became lexicographically larger than S in reversed order
            nextOk2 = c > s[pos];
          }

          f[pos][ok1][ok2] = (f[pos][ok1][ok2] + f[pos + 1][ok1 || c > s[pos]][nextOk2]) % MOD;
        }
      }

The complexity will be O(N × 2 × 2 × 26)

Author's/Tester's Solutions:

Setter's solution
Tester's solution

Help -- KRILLIN problem getting WA

CSUB - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Lalit Kundu
Tester:Shang Jingbo and Gerald Agapov
Editorialist:Devendra Agarwal

DIFFICULTY:

Cakewalk

PREREQUISITES:

Adhoc

PROBLEM:

Given a string S consisting of only 1's and 0's, find the number of substrings which start and end both in 1.

Quick Explanation

You need to find number of pairs of 1.

Explanation

Find total number of 1's in the given string. Let suppose that the total number of 1's in the string is n , then the answer is (n*(n+1))/2.

Reason

Let's suppose that the n 1's in the string occur at x1 , x2, ... , xn position in the string then all substring starting from xi and ending at xj are taken, so total possible ways in taking it is (n*(n+1))/2

Pseudo Code

solve(string s)
    int n = 0 
    for ( i = 0 ; i< s.size() ; i++)
        if s[i] == '1' 
            n++
    return (n*(n+1))/2

Complexity:

O(N), You just need a single pass of the string.

AUTHOR'S and TESTER'S SOLUTIONS:

Author's solution
Tester's solution

Minion Chef and Bananas (MINEAT) .....Please tell me what's wrong with my solution

getkey() function

$
0
0

hi there,

i have been trying to build a puzzle game in c language. for this i need a function to input arrow keys.

here is my function which i got from on book.

include<dos.h>

sgetkey()
{ union REGS i,o;
while(!kbhit());
i.ah.h=0;
int86(22,&i,&o);
return(o.h.ah);
} but it keeps on showing

error:storage size of i is unknown. error:storage size of o is unknown.

I am doing in this codeblocks. So please share a suitable solution.

Output is correct but getting wrong answer during submission.

$
0
0

http://discuss.codechef.com/problems/ERROR

include<stdio.h>

include<string.h>

int main() { int i,t; scanf("%d",&t); for(i=1;i<=t;i++) { int len,j,k,l,flag=0,count1=0,count2=0,count3=0;; char num1[100001]; char num2[3]; char sample1[3]={'1', '0', '1'}; char sample2[3]={'0', '1', '0'}; scanf("%s", num1); len=strlen(num1); for(j=0;j<len-2;j++) {

        for(k=j;k<=j+2;k++)
        {
            num2[count1]=num1[k];
            count1++;
        }
        for(l=0;l<3;l++)
        {
            if(num2[l]==sample1[l])
            count2+=1;
        }
        for(l=0;l<3;l++)
        {
            if(num2[l]==sample2[l])
            count3+=1;
        }
        if(count2==3)
        {
            flag=1;
            break;
        }
        else if(count2==3)
        {
            flag=2;
            break;
        }
        count1=0,count2=0,count3=0;
    }
    if(flag==1 || flag==2)
    printf("GOOD");
    else
    printf("BAD");
    printf("\n");
}

return 0; }


my mergesort program does not perform sort what is the error i could not get it?

$
0
0
    void merge(int L[],int R[],int A[]){
        int nL=sizeof(L)/sizeof(L[0]);
        int nR=sizeof(R)/sizeof(R[0]);
        int i=0,j=0,k=0;
        while(i<nL && j<nR){
            if(L[i]<=R[j]){
                A[k]=L[i];
                i++;}
                else{
                    A[k]=R[j];
                    j++;
                }
                k++;
        }
        while(i<nL){
            A[k]=L[i];
            i++;
            k++;
        }
        while(j<nR){
            A[k]=R[j];
            j++;
            k++;
            }
    }
    void mergeSort(int A[]){
        int n=sizeof(A)/sizeof(A[0]);
        if(n<2)
            return;
        int mid=n/2;
        int left[mid];
        int right[n-mid];
        for(int i=0;i<mid;i++)
         {
            left[i]=A[i];
         }
        for(int i=mid;i<n;i++){
            right[i-mid]=A[i];
        }

        mergeSort(left);
        mergeSort(right);
        merge(left,right,A);

}

void printArray(int *A,int size){ for(int i=0;i<size;i++){ cout<<A[i]<<endl; }}

Please tell me the logic for the code.

$
0
0

For eg. 2341 then we subtract the adjacent terms with each 2-3=1 2-4=1 4-1=3 then after subtracting we get :113 then we minus 1-1=0 1-3=2 that is the single term which is 2 this must be the answer. Please tell the Logic For code

Majin Vegeta, VEGETA

getting runtime error in c(sigabrt)

quickmatch 10 coming soon with 3 hiring companies

$
0
0

This time we have 3 hiring companies and good ones, good coders working in them...

Will update details once problems are ready. Readying it. https://www.codechef.com/KQ102018

Edit: Codechef to share 250 laddus to top contestants (number of tops depends on number of participants). Thank you codechef.

Viewing all 39796 articles
Browse latest View live


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