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

MGAME timeout


Add a Row After Setting DataSource to Datagridview

$
0
0

I had lost of question related to datasource binding of datagrid
I had a DatagridView to which i am setting DataSource from a list

List<Myclass> li = new List<MyClass>();

MyClass O = new MyClass();
O.Name = "Aden";
O.LastName = "B";
O.Id = 12;
li.Add(O);
O = new MyClass();
O.Name = "Li";
O.LastName = "S";
O.Id = 22;
li.Add(O);

Mydgv.DataSource = li;

Where MyClass is

public Class MyClass
{
 public string Name {get; set;}
 public string LastName {get; set;}
 public decimal Id {get; set;}
}

Now Want to add a new Row to My DataGridView

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
Mydgv.Rows.Add(row);

But it is not possible it raise error because Datasource of Datagrid is Binded with List li So My first question is how could i do this

My another Question is For doing this i made a solution to alter my List li and Add New row of data to it and then set it to datasource of datagrid but i think its not a feseable solution is there any better solution

i even try to do it by CurrencyManager

CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[MyGrid.DataSource];
currencyManager1.SuspendBinding();
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
Mydgv.Rows.Add(row);
currencyManager1.ResumeBinding();

As i know throug it i Suspend Binding of DataGrid to Provide Formating to Grid As i Perform in One of my post Make Row Visible false But why it doesn't work here in adding row to datagrid
Please Provide me a detailed and exact solution of Problem
Thanks

Is the Partition the Graph checker correct?

$
0
0

I didn't want to ask during the contest, but I felt like my solution was correct, yet still got WA. I haven't seen an editorial for the problem yet so I have no way to verify my solution.

So I'd like to ask for a test case that my method fails.

My Approach I picked a root node in the tree (Usually 1, but I tried others) and calculated the distance to each node from there, using Breath First Search/Dijkstra.

I then grouped the nodes into 4 groups with each node going into the group number dist_from_root%4

If group(0) + group(2) == group(1) + group(3), Then the answer is 1 and the sets are chosen in the same way

If they not equal I moved only nodes from one of the groups across to the other side until they were equal, but making the answer 2.

Solution Codehttps://www.codechef.com/viewsolution/22349636

Please help me find a failed test case, I've tried to find my error but could not.

December Cook-Off YVSTR plagiarism

Chef and An Ideal Problem Dec 2018 Editorial (Unofficial)

$
0
0

Monty Hall Problem suggests that when a person swaps the doors after knowing the location of goat in one of the two doors. The probability of getting a car in the swapped door is 66%, increased from the previous 33%. Solution---
So, we can randomly choose the initial door and accordingly swap the doors. It's a very naive approach but works.
My solution

BICONT Solution?

correct output, yet 'wrong answer' display on submission

$
0
0

I have written codes that generate the correct output yet when i submit it, it displays 'wrong answer'. Why is it happening, where is the glitch and how can I rectify it? Please respond as quickly as possible. Thanks much.

SNAKEEAT - Editorial

$
0
0

PREREQUISITES - Sorting, Binary Searching, Sliding Window

QUICK EXPLANATION -

Sort the lengths array. For each query K, binary search for the largest element smaller than K and name it cur. Then binary search again on the prefix difference sum array (which will give the number of snakes to be fed to snakes in [prev+1, cur] to make their length at least K) in the region [1, cur] for finding the least index of snake to be killed and let it be prev. The answer is N-prev.

EXPLANATION -

Some key observations before we jump to the solution -

  1. The most optimal choice would be to feed the largest snakes shorter than K[i] on the smallest snakes and to not feed the snakes greater than K[i] in length in $i^{th}$ query.

  2. If we get some X smallest snakes killed for some query K[i], then, for all queries where K[j] > K[i] for some j, we get Y smallest snakes killed where the condition (Y $\geq$ X) will be satisfied.

There are many solutions to this problem some of which are discussed below -

ONLINE SOLUTION -

Let us sort the lengths array as the order doesn't matter for the answer.

Let us solve the problem for a query having its value as K. The most naive solution would be to keep iterating forward from 1 till we reach an element just smaller than K and name this index as cur. Let us keep one more pointer/variable prev which denotes the number of smallest snakes eaten, initially at 0. We will start feeding from $cur^{th}$ snake. Now, to make it of length K, we need to feed it on (K-l[i]) smallest snakes available, where i is initially cur. We increase the prev by (K - l[i]) and decrease i by 1 to reflect this change. We do this process of increasing prev and decreasing i while the condition (i > prev) remains satisfied. Then we print the answer to be simply N - i - 1.

Complexity - $O(Q*N)$

This solution is inefficient but can be passed in TL if we optimise the solution by binary searching the cur and prev as both functions are monotonic in nature.

Now, let us also make a prefix sum array defined as follows -

$$presum[x] = presum[x-1] + (10^9 - l[x]),\hspace{4mm} \forall \hspace{4mm}1 \leq x \leq N$$

For every query having value K, we first find the value of cur by binary searching on length array as it is in increasing order. Then, we also find the largest index prev which is going to get killed. In the naive solution also, we were taking the summation of (K - l[i]) from cur by iterating backwards and set the prev equal to that summation and it can be observed that the function (i > prev) is monotonic as i is decreasing and prev increasing. prev can be found simply by binary searching method on sample space [1, cur]. Binary searching method works because we are finding the smallest j for which the condition

$$presum[cur] - presum[j] - (cur-j)*(10^9 - K) \hspace{4mm} \leq \hspace{4mm} j$$

is satisfied and this function is monotonic with increasing j and then we set prev = j. We want the shortest j as this is the demand of the question to minimize the snakes being eaten OR maximize the snakes of length $ \geq K$ which is (N - j).

The need for the presum array is that it is the number of snakes required to be killed to make the length of snakes in [prev+1, cur] to be more than or equal to K and which is the demand of the question to maximize the number of snakes of length K.

After finding prev, answer is (N - prev) for the query having value K.

Complexity - $O((N+Q)*log(N))$

OFFLINE SOLUTION -

Let us again sort the length array in increasing order as the order doesn't matter. Also, let us sort the queries array along with their indices in increasing order of K values. Hence, a 2-pointer offline solution will be to maintain 3 variables - cur, prev, prefix - where cur means that $cur^{th}$ element is the largest element smaller than K value of current query which we are processing. prev refers to the largest index smaller than cur which is killed and can also be said that prev smallest snakes will be killed. Another thing to observe is that cur and prev can only increase if the queries are sorted according to K value of queries according to observation 2.

Let us assume that currently we are processing answer for $j^{th}$ query in sorted order. prefix refers to

$$ \sum \hspace{2mm} (K[j] - l[h]), \hspace{4mm} \forall \hspace{4mm} cur \geq h > prev $$

To calculate prefix, we keep on increasing cur and update prefix by adding (K[j] - l[cur]) to prefix till we get l[cur+1] to be greater than K[j]. Then, We keep on increasing prev and keep updating prefix by subtracting (K[j] - l[prev]) while (prefix > prev). Then we get the answer for that query to be simply (N - prev) as prev refers to the largest index of snake which will be killed. Before proceeding to the next query, we also update the prefix by adding
[(cur - prev) * (K[j+1] - K[j])]. The need for the prefix is that it is the number of snakes required to be killed to make the length of snakes in [prev+1, cur] to be exactly equal to K[j] and which is the demand of the question to maximize the number of snakes of length K[j].
Remember, that we sorted the query and length arrays both in increasing order.

Complexity - $O(N*log(N) + Q*log(Q))$

There also exists another solution which is to do the naive solution as explained above, but to not do computation again for some K previously encountered. This can be implemented easily by mapping K value of query to its answer and checking everytime before computing answer for a query, that if the answer for same K value has been computed before using a BST (map or set in C++). It can be proved easily that it is also efficient.

Offline solution implementation - here


TLE IN MGAME

Wasted my time on UEMATH1 just should've googled it *sigh*

$
0
0

This wasn't a maths competition, it was just to see who is average in maths and who knows how to google answers :)

Codechef rating

$
0
0

My rating on 14/01/2019 was 1381 and on 15/01/2019 it dropped by 69 points to 1312? how is this possible? I believe that I have not violated codechef's code of conduct.

Rating not updated.?

$
0
0

Rating is not updated for External contest Plinth-Enigma'19 LNMIIT Techfest - (Rated for all)?? My rating before above contest is 1484.And After that, it inc. by 83.So 1567.And For accidental purpose of JAN Long challenge.It is Dec.by83.So 1484 as above. After Jan Long challenge It will 1581.So I think Above Contest rating is not added.

Invitation to CodeChef December Long Challenge sponsored By ShareChat

$
0
0

Hello CodeChef community,

You are invited to CodeChef December Long Challenge 2018 sponsored by ShareChat. This is a 10-day competition with 8 problems and it’s open to programmers across the globe. The contest problems will be available in English, Hindi, Bengali, Russian, Mandarin and Vietnamese.

Participants will also be in the running for some exciting jobs at ShareChat - India’s fastest growing social network. To apply, all you need to do is fill the application form on the contest page and participate in the December Long Challenge.

I hope you will join your fellow programmers and enjoy the contest problems. Joining me on the problem setting panel are:

I hope you will enjoy solving them. Please give your feedback on the problem set in the comments below, after the contest.

Invitation to CodeChef January Long Challenge 2019 sponsored By ShareChat

$
0
0

Happy New Year to the CodeChef community!

Celebrate the new year with CodeChef’s January Long Challenge 2019 sponsored by ShareChat. This programming contest lets you tackle 8 problems over 10 days and it’s open to programmers in every country and at every skill level. The contest problem statements will also be available in English, Hindi, Bengali, Russian, Mandarin and Vietnamese.

Participants will also have the opportunity to apply for jobs at ShareChat - India’s fastest growing social network. Visit the contest link for more details.

I hope you will join your fellow programmers and enjoy the contest problems. Joining me on the problem setting panel are:

I hope you will enjoy solving them. Please give your feedback on the problem set in the comments below, after the contest.

Contest Details:

Time: 4th January 2019 (1500 hrs) to 14th January 2019 (1500 hrs). (Indian Standard Time — +5:30 GMT) — Check your [timezone](https://www.timeanddate.com/worldclock/fixedtime.html?msg=CodeChef+January+Challenge+2019&iso=20190104T15&p1=44.

Contest link:https://www.codechef.com/JAN19

Registration: You just need to have a CodeChef handle to participate. For all those, who are interested and do not have a CodeChef handle, are requested to register in order to participate.

Prizes:

Top 10 performers in Global and Indian category will get CodeChef laddus, with which the winners can claim cool CodeChef goodies. First to solve each problem except challenge - 100 laddus. Know more here: https://www.codechef.com/laddu. (For those who have not yet got their previous winning, please send an email to winners@codechef.com)

Good Luck!
Hope to see you participating!!
Happy Programming !!

PARRTY - EDITORIAL

$
0
0

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

Setter:Mohammad Nematollahi
Tester:Xiuhan Wang
Editorialist:Taranpreet Singh

DIFFICULTY:

Medium-Hard

PREREQUISITES:

Segment Tree and Implementation would do.

PROBLEM:

Given $N$ people numbered $1$ to $N$ and $M$ conflict pairs of people, answer $Q$ queries of format. Given $K$ pairs in a query namely $[L_i, R_i]$ inviting all people in range $[L_i, R_i]$ to party. Determine if all people invited to party get along or not.

QUICK EXPLANATION

  • The brute solution to answer each query in $O(N+M)$ time can be easily written. This solution takes $O(Q*(N+M))$ in the worst case if all queries consist of a single interval of people invited and shall time out.
  • We can develop another solution answering each query in $O(K^2)$ time by iterating over every pair of intervals in the query and checking if both of the intervals contain a pair of people who do not get along. If it holds true for any pair of intervals in query, answer is NO, otherwise YES. This solution too won't work as it may take time to answer if there are too many intervals in few queries.
  • To find out whether any pair of an interval of people contains any conflict pair, we shall use segment tree operations.
  • Now combine the above two solutions, using the first solution when $K$ is large, and the second solution when $K$ is small, to fit the overall solution into the time limit.

EXPLANATION

The solution to this problem is based on merging two slower solutions to fit the time limit, so let us discuss both solutions.

Brute Solution:

To answer each query in $O(N+M)$ time, we can simply maintain an array included, marking all the elements present in intervals and then iterating over all conflict pairs to check if both persons in any pair are included or not. This solution performs well when there are few queries with a high number of intervals.

Segment Tree Solution:

We can also build a solution which, for each query check all pairs of intervals to see if the first member of any conflict pair lies in the first interval and second person of conflict pair lie in the second interval of selected pair. If we tried to maintain a set of conflict pairs, it shall time out. We need something smarter.

Let us store these queries and answer them all together efficiently.

What we can do is, to build a range max Segment tree initially filled with -1 and start considering each person one by one. While considering yth person, Consider all conflict pairs $(x,y)$, $x < y$ and update these positions with value y.

Now, whenever we have an ith interval in query $[L, R]$ where $R$ is the person being considered, we can check all intervals ending before $L$ and check if range max of any interval is always strictly smaller than $L$. The reasoning is as follows.

Considering only first $i$ persons, when we reach the ith person, we update all positions having a conflict with the ith person with value i. Now, If any interval has range maximum x if implies that in that interval, there is a person having a conflict with the xth person. So to check if any conflict pair is there, we iterate over all intervals ending before the current interval and check if this interval has a conflict with any person having index $\geq L$. (Meaning people in the current interval). This compares all unordered pairs of intervals and thus, runs in $O((N+M)*log(N)+\sum K^2)$ to answer all queries.

This solution won't work due to square factor and shall time out when there are few queries with a large number of intervals.

Merging solutions:

Let us define a limit $SQ$. We use brute solution when $K \geq SQ$ achieving worst case when $K = SQ$ taking $O(Q*(N+M))$. This may seem large, but here, $Q*SQ \leq 2*10^5$

We shall use the second solution when $K < SQ$ as our second solution is good in answering queries when $K$ is small. If all queries are answered by this solution, it takes $O((N+M)*log(N)+\sum K^2)$ time. But, since $K < SQ$, $\sum K$ cannot exceed $2*10^5*SQ$.

By choosing a reasonable value of $SQ$, we can achive $O((N+M)*Q/SQ +(N+M)*log(N)+Q*SQ)$.

Implementation hints:

It is useful in the second solution to sort the intervals beforehand so you only need to check intervals indexed up to the current interval. Additionally, do not forget to consider the case when a conflicting pair lies inside a single interval only in the second solution. This can be easily handled by considering the pair of an interval with itself too.

There are quite a number of problems merging solutions, but I don't remember any at present, so please share the link of any problem you know using a similar idea.

Time Complexity

Time complexity of this solution is $O((N+M)*Q/SQ +(N+M)*log(N)+Q*SQ)$ in worst case. Memory complexity is $O(N*log(N)+M+\sum K)$ in worst case.

AUTHOR'S AND TESTER'S SOLUTIONS:

Setter's solution
Tester's solution
Editorialist's solution

Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)


COOMILK - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Kamil Debowski
Tester:Niyaz Nigmatullin
Editorialist:Kamil Debowski

DIFFICULTY:

CAKEWALK

PREREQUISITES:

none

PROBLEM

Given a sequence of strings "cookie" and "milk", you should check whether after each "cookie" the next string is "milk".

EXPLANATION

We should implement exactly what is written in the statement. For each string s[i] if it is "cookie" then we must check if s[i+1] is "milk". So the answer will be "NO" in two cases (assuming that s[i] == "cookie"):

  1. i = N, what means that s[i] is the last string
  2. s[i+1] == "cookie"

Let's see the pseudocode of this intended solution:

bool answer = true
for i = 1 to N do
    if (s[i] == "cookie") then
        if (i == N or s[i+1] != "milk") then
            answer = false
if (answer) then
    print("YES")
else
    print("NO")

POSSIBLE MISTAKES

One wrong approach is to think that the answer is "NO" if and only if some two consecutive strings are both equal to "cookie". For example, ("milk", "cookie", "cookie", "milk") indeed gives answer "NO". The last of given in the statement example test cases shows that this claim isn't correct. The answer is "NO" also if the last string is "cookie". In such a situation Limak indeed eats a cookie and doesn't drink milk in the next minute.

ALTERNATIVE APPROACH

A mistake described in the previous paragraph can be fixed by additionally checking the last string of the given sequence. So we can get a slightly different implementation:

bool answer = true
for i = 1 to N-1 do   #we iterate to N-1 only!
    if (s[i] == "cookie" and s[i+1] == "cookie") then
        answer = false
if (s[N] == "cookie") then
    answer = false

AUTHOR'S AND TESTER'S SOLUTIONS:


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

could anybody help me regarding mgame problem because i cannot understand editorial

stl or data structures to store trees

$
0
0

what are the good ways(data structures or stl) to store trees?

GARGOYLE - PELT2019 Probable Verdict Error

$
0
0

LINK TO MY SOLUTION

It got WA verdict during the contest last week but the similar solution is mentioned in the editorial. LINK TO THE EDITORIAL

When I commented on the same and posted the link to my solution there, the author @panik replied ->

" I am looking into your solution although its giving the correct ans on codechef compiler on the TC on which your solution is showing wrong, i am looking into it."

On codeforces posts, someone suggested with possible test cases and errors, it's giving correct answer on every such case.

Till now, there is no reply from the Author.

Moral of the story : Can anyone please let me know what's actually my mistake in the code?

(Directi Interview question) Given an array A and m queries

$
0
0

Given an array A and m queries each query is integer T

For each query find index i and j such that

| (|sum of elements from i to j| - T) | is minimum

where |x| is abs(x) and array can have negative numbers as well

I was asked this question in directi interview. I had the solution of finding all possible sum and store their indices and sort.

so there will be n*n sums possible.

That would take O(nn log(n*n))

Now for each query binary search T .That would be O(m log(n n))

But he asked to optimize it.I didn't clear the round.

Can anyone give hint for this?

I asked the question on Stackoverflow as well.

Viewing all 39796 articles
Browse latest View live