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

Issue with the problem DEVVOTE

$
0
0

Hello,

When I try to submit the problem https://www.codechef.com/problems/DEVVOTE instead of the usual submission window, I get an error "Problem does not exist in the Practice section. You cannot make a submission to it." Why is this error shown? Could you fix it, please?


help in finding euler path

$
0
0

given a DIRECTED graph i want to know an efficient method to find an euler path in the graph.

i have googled it and now able to do the same for undirected graphs (there the idea is we remove an edge untill it's not a bridge and finally if we have no choice then only we cross that bridge). I try to simulate the same here but stuck at point that how we determine that the current edge is bridge or not(as the graph is changing after every selection so, didn't able to get an online approach)!

please help me (or share any good resource to move on).

Invitation to Geek-a-Hertz Binary Master's Online Round

$
0
0

Hello Codechef community,

St.Mark's Sr. Sec. Public School presents you their flagship contest, "Geek-a-hertz Binary Masters 2017". It is being organised as a part of their technical fest, Geek-a-Hertz.

Contest Link :https://www.codechef.com/GKAH1701

Start Time :18:00 IST, 20th November' 2017

alt text

The contest is a team contest, with teams upto 2 members. This round will serve as qualification for Delhi School students for the onsite round on 22nd November. The problemset will consists of 5 problems. IOI ranking will be followed, meaning there will be partial subtasks in each problem. The rounds have been prepared by me and the editorials of the problems will also be available after the round.

The difficulty of the problems will be maximum Div2 D as compared to normal Codeforces round. Also, it will be a good practice round for ZCO aspirants.

To participate in the contest, you just need a Codechef handle and then just click on the link mentioned above for participating for the contest.

I hope you enjoy the contest and Happy Coding :)

UPDATE 1: The contest has started and is of 4 hours.

PLUM- Editorial

$
0
0

Problem Link
Contest
Practice

Author:Sahil Rajput

Tester:Sahil Rajput

Editorialist:Sahil Rajput

Difficulty
Medium

Prerequisites
Graph Theory

Problem
Given a weighted graph with N nodes and starting position , you have to find the overall weight of MST of the graph taking 1 unit as Pi. Task is simple just implement Kruskal's algorithm in the graph but if their are edges of equal weight first pick up the one with least value of given expression while sorting the edges initially.

Time Complexity
O(ELogV)

Solution Link
Setter's solution

POC1- Editorial

$
0
0

Problem Link
Contest
Practice

Author:Sahil Rajput

Tester:Sahil Rajput

Editorialist:Sahil Rajput

Difficulty
Easy

Prerequisites
Basic maths

Problem
You just have to implement the given formula. Given two integers A and B. If you divide a smaller number with the greater number the answer will be 0 until both integers are not equal. If they are equal answer is 1.

Solution Link
Setter's solution

FLV - Editorial

$
0
0

Problem Link
Contest
Practice

Author:Sahil Rajput

Tester:Sahil Rajput

Editorialist:Sahil Rajput

Difficulty
EASY

Prerequisites
Maths

Problem
Given an array of N size with K elements in the array. Task is to find the Minimum and Maximum number of possible good places in the array, A place is called good place when at least one value is already present adjacent to it.

Explanation
The approach for this question is simple calculation with some exceptions. The exceptions are cases in which K = 0 and K = N, in these cases both minimum and maximum number of good places is 0 since there is no inhabitant or vacant index in the array. Calculate the number of places with indices 2, 5, 8 and so on excluding the one with index N if it exists. Number of that places is equal to x = (floor)(N/3) , and if K ≤ x you can occupy some of these places to reach maximum number of good places equal to 2·K. Otherwise, if K> x, assume that place with indices 2, 5, 8 and so on are occupied, so any place has at least one inhabited place adjacent to it. Therefore number of good places for the value is equal to number of vacant places and is equal to n - k.
Time complexity
 O(1).

Solution Link
Setter's solution

Useful sites for Programmers

$
0
0

Here is the list of useful websites that a programmer should visit
Link :Here

Payment Issue

$
0
0

Sadly, I am facing a dilemma.

I made a big mistake and the payment for my team is still not done. I was busy with my majors and I tried doing the payment today and the SBI account didn't update the beneficiary and long story short I still have not done the payment.

What I didn't consider was tomorrow was the last Saturday and NEFT will go through only on Monday.

What should I do now? I have mailed AmritaPuri and is there any chance that I will get a extension till Monday?

(I know this is a big *** up but any help will be highly appreciated)?


Useful implementations

help in understanding strongly connected component in graphs

$
0
0

http://www.geeksforgeeks.org/tarjan-algorithm-find-strongly-connected-components/

i have read this article but didn't able to understand the line : "low[u] = min(low[u], disc[v]); In case two, can we take low[v] instead of disc[v] ?? . Answer is NO. If you can think why answer is NO, you probably understood the Low and Disc concept."

as far i have understood low[v] is the lowest node value which we can reach from current node. so, if we have found an edge(back edge not cross one) to an upper level node(already visited node) from current node then it means we can reach to this node from current one and if it has (low != disc) then we should consider it's low value as we are able to reach here (and so able to reach anywhere where this node can reach)!

please correct me if i am wrong. and if someone has better tutorials please share :)

GEEK04 - Editorial

$
0
0

Problem Link

Practice

Contest

Author:Bhuvnesh Jain

Tester:Bhuvnesh Jain

Editorialist:Bhuvnesh Jain

Difficulty

MEDIUM

Prerequisites

Recursion, Bitmasks, Dynamic Programming.

Problem

Find the minimum time taken to cross the river by $N$ people if we can allow almost 2 people to use a boat such that the rowing time is equal to the minimum of the rowing time of both the people. Note that, slowing rowing time is given by larger integers.

Explanation

First of all, any type of greedy algorithm will only pass the first subtask and not any other. As the constraints are small and greedy solution will not work, we will try to come up with a dynamic programming solution.

Let us assume we denote the side where all the persons are initially standing by left and their destination, i.e. where they want to reach by right.

The first observation is that we will always try to send 2 people in the boat. This is because the slower person rowing time will then not contribute to the answer. The next observation is that once few people have crossed the river and reached "right", the boat will be brought to the left side by the person whose rowing time is the minimum among the persons at the right side. Next is that we can represent each person by a "bit-mask". The "left" and "right" bitmask denote that if the ${i}^{th}$ bit is one, then the ${i}^{th}$ person is present there else he is not present. The last observation is that the person can be present either in the "left" side or "right" side, not both ways. Thus, we only care about "left" side masks. The "right" mask can be calculated from the "left" ones, by setting the bits to 1 which are not present in "left".

With all these observations, we try to build a recursive solution as follows: (in the code, "turn" means, whether we send people from right to left or reverse)

//call with recurse((2^n)-1, 0) void recurse(LEFTMASK, turn): if (LEFTMASK == 0): return 0 //we transferred all the people RIGHTMASK = ((2^n)-1) ^ LEFTMASK if (turn == 1): //we want to transfer people to the left min_row = infinity, person = -1 for i in [0 .. n-1]: if (RIGHTMASK & (2^i)): if (min_row > row[i]): min_row = row[i] person = i return row[person] + recurse(LEFTMASK ^ (2^person), turn ^ 1) else: //we want to transfer people to the right if (number of set bits in LEFTMASK == 1): //only 1 person is left) for i in [0 .. n-1]: if (LEFTMASK & (2^i)): return row[i] + recurse(left ^ (2^i), turn ^ 1) else: //try every pair of people by sending them to right) ans = infinity for i in [0 .. n-1]: for j in [i+1 .. n-1]: if ((LEFTMASK & (2^i)) && (LEFTMASK & (2^j)): val = max(row[i], row[j]) val += recurse(LEFTMASK^(2^i)^(2^j), turn ^ 1) ans = min(ans, val) return ans

To optimise the above code for the full score, we can use dynamic programming so that each state is visited only once. This is also known as "top-down DP" or "recursion with memoization".

Time Complexity

$O(2^N * N * N)$

Space Complexity

$O(2^N)$

Solution Links

Setter's solution

LRQUER - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Misha Chorniy
Tester:Alexey Zayakin
Editorialist:Oleksandr Kulkov

DIFFICULTY:

EASY

PREREQUISITES:

Segment tree

PROBLEM:

You're given array $A$ of $N$ elements. You have to answer queries of two types.

  1. You're given $L$ and $R$. Find such $M$ that $(A_R-A_M)(A_M-A_L)$ is maximized.
  2. Change $A_X$ to $Y$.

QUICK EXPLANATION:

The problem is equivalent to the finding element closest to $\dfrac{A_L+A_R}{2}$ on the segment $[L;R]$ and processing update queries.

EXPLANATION:

You have to find $A_M$ which minimizes $A_M^2-(A_L+A_R)A_M+A_RA_L$. For this function we want to have $A_M$ as close as possible to the extremum of parabola which is $Z=\dfrac{A_L+A_R}{2}$. So you can reduce this to the queries of lower bound on the segment which is well-known segment tree excercise.

To solve it you have to keep multiset of all values that occur on the segment in each vertex of segment tree, this will allow you to update in $O(\log^2 N)$ by erasing old value and inserting new one in each multiset of vertices which cover position $X$. Also you can get queries in $O(\log^2 N)$ by simply asking lower-bound in each multiset of vertices you decomposed query segment in. You should also compress values, i.e. sort them and assign to each number its order in sorted sequence. This will allow to simplify algorithm and lower time and memory consumes. However one probably can solve the problem with dynamic segment tree instead of compression. Considering the compression to be done, this will look as follows:

multiset<int> st[4 * maxn];

// Inserts or erases element depending on its sign
void update(int p, int c, int v = 1, int l = 0, int r = maxn) {
    if(c < 0) {
        st[v].erase(st[v].lower_bound(-c));
    } else {
        st[v].insert(c);
    }
    if(r - l == 1) {
        return;
    }
    int m = (l + r) / 2;
    if(pos < m) {
        update(p, c, 2 * v, l, m);
    } else {
        update(p, c, 2 * v + 1, m, r);
    }
}

// Returns closest element to c from intersection of [a, b) and [l, r)
int nearest(int a, int b, int c, int v = 1, int l = 0, int r = maxn) {
    if(a <= l && r <= b) {
        auto it = st[v].lower_bound(c);
        int res = inf;
        for(int i = 0; i <= 1; i++) {
            if(it != end(st[v])) {
                res = abs(res - c) < abs(*it - c) ? res : *it;
            }
            if(it != begin(st[v])) {
                it--;
            }
        }
        return res;
    } else if(r <= a || b <= l) {
        return inf;
    }
    int m = (l + r) / 2;
    int A = nearest(a, b, c, 2 * v, l, m);
    int B = nearest(a, b, c, 2 * v + 1, m, r);
    return abs(A - c) < abs(B - c) ? A : B;
}

AUTHOR'S AND TESTER'S SOLUTIONS:

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

RELATED PROBLEMS:

SHUFFL - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Misha Chorniy
Tester:Alexey Zayakin
Editorialist:Oleksandr Kulkov

DIFFICULTY:

EASY-MEDIUM

PREREQUISITES:

None

PROBLEM:

Consider array $A=\{1,\dots,M\}$. Split it into two arrays of equal size with even and odd indices. Erase in each array number on the place $\left\lfloor \dfrac{SX}{Y}\right\rfloor$ where $S$ is the size of subarray and concatenate arrays after that. Each time size of array reduced by $2$. You have to output xor of values which whill remain when there are only two elements. All indexes are considered to be $0$-based.

QUICK EXPLANATION:

Consider procedure in reverse order. You can trace back positions of remained elements.

EXPLANATION:

Assume current position of element in the array is $P$ and size before the last step was $2S$. Let's calculate its previous position. At the last step element $W=\left\lfloor \dfrac{SX}{Y}\right\rfloor$ was erased from both subarrays. After that positions of remained elements split into four different groups.

  1. $[0,W)$. This is even-indexed elements so previous index is $2P$.
  2. $[W,S-1)$. This is even-indexed elements and one element before them is erased so previous index is $2P+2$.
  3. $[S,S+W)$. This is odd-indexed elements so previous index is $2(P-S)+1$.
  4. $[S+W,2S-1)$. This is odd-indexed elements and one element before them is erased so previous index is $2(P-S)+3$.

Given that we can in $M$ steps recover initial positions of last two elements and just output their xor.

AUTHOR'S AND TESTER'S SOLUTIONS:

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

RELATED PROBLEMS:

SMRSTR - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Misha Chorniy
Tester:Alexey Zayakin
Editorialist:Oleksandr Kulkov

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

You're given array $D$ of size $N$ and $Q$ queries $X$. In each query you have to compute the result of the program

read X
for i = 1..N:
  X = floor(X / D[i])
print X

QUICK EXPLANATION:

You should consider only $O(\log X)$ terms such that $D_i\neq 1$.

EXPLANATION:

You may keep keep first $\min(2\cdot\log_2 X, N)$ terms of $D$ which aren't equal one. If there are more non-one terms than $X$ wiil definitely be equal zero at the end of procedure.

ALTERNATIVE SOLUTION:

You can keep product $P$ of non-one terms of $D$ instead of keeping this terms and divide $X$ by $P$. But you should check then that you output zero if there are too much such terms.

AUTHOR'S AND TESTER'S SOLUTIONS:

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

RELATED PROBLEMS:

Can anyone tell why i am getting SIGABRT in LRQURY

$
0
0

problem: LRQUR

my solution: here Someone please tell why i am getting SIGABRT error, please help..

EDIT: i got it where i was getting wrong. :)


STRQUER - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Misha Chorniy
Tester:Alexey Zayakin
Editorialist:Oleksandr Kulkov

DIFFICULTY:

MEDIUM

PREREQUISITES:

Treap or segment tree

PROBLEM:

You're given array $A$ of $N$ numbers. You have to make some connections between those numbers in such way that any number is connected to at least one another one. Cost of making a connection between numbers $x$ and $y$ is $|x-y|$. Also you have to maintain inserting and erasing elements from the set.

QUICK EXPLANATION:

Maintain $dp[l][r][2][2]$ which is answer to segment $[l,r]$ with indicators of whether endpoints connected with any number inside the segment. Keep it in treap or segment tree to recover the answer.

EXPLANATION:

First of all we should note that optimal connection is segment connecting minimum and maximum numbers from the set with some drop off segments connecting two consequent points.

If we would like to solve the problem in static case we can consider dynamic programming: $dp[n][2]$ with state is number of considerent points (in sorted order) and boolean indicator of either we connected $n^{th}$ number with previous. Then

$$\begin{cases}dp[i][0] = dp[i-1][1],\\dp[i][1]=|A_i-A_{i-1}|+\min(dp[i-1][1],dp[i-1][0])\end{cases}$$

To solve this particular problem however we should consider a bit more general dynamic programming $dp[l][r][2][2]$ which is the answer to subset of numbers from segment $[l,r]$ having two indicators of whether corresponding endpoints are connected with the number inside segment. Now assume that for some $l< m< r$ we know $dp[l][m]$ and $dp[m][r]$ (answers for $r-l\leq 2$ can be handled manually). Then we can say that

$$dp[l][r][a][b] = \min\limits_{\substack{x,y\\x|y=1}}\{dp[l][m][a][x] + dp[m][r][y][b]\}$$

Indeed we can't allow case when $m$ is connected neither to $m-1$ nor to $m+1$ but all other cases are okay for us and they cover everything. To solve the problem we should note that we don't need to calculate values for all $[l,r]$. Actually only ones corresponding to some vertices of treap or segment tree built over given numbers are sufficient. And luckily we can recalculate that dynamic programming while returning from recursion in this trees. In treap it would be straightforward to maintain the same trick in segment tree one should firstly compress numbers or maintain its dynamic version.

AUTHOR'S AND TESTER'S SOLUTIONS:

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

RELATED PROBLEMS:

Can anyone tell why i am getting WA in LRQURY

$
0
0

problem: LRQUR

my solution: here
Someone please tell why i am getting WA in 3 cases, please help..

Re-direct your questions here

$
0
0

Hey guys!

For some reason, I am not able to access my thread "I want to ask a question- ask them all here" . It is giving me 502 bad gateway error on trying to access. Hence, I am unable to assist you.

@admin is working on fixing that, and till then I request you to post your questions here so that I can help you getting their answers. This thread will be deleted later once the main thread starts working again.

Thank You,
With Regards
@vijju123

Please give a counter case for my code in LRQUER

Explain the public section

Viewing all 39796 articles
Browse latest View live


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