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

XYPIZQ - EDITORIAL

$
0
0

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

Setter:Sajal Agrawal
Tester:Xiuhan Wang
Editorialist:Taranpreet Singh

DIFFICULTY:

Easy-Medium

PREREQUISITES:

Properties of triangles and Case-based Analysis.

PROBLEM:

Consider two Intersecting Lines interesecting at point $A_0$ having an angle $\theta < 90°$ and marking $N$ points $A_1, A_2 \ldots A_N$ on lower line and $B_1, B_2 \ldots B_N$ on upper line. Now join $A_i$ with $B_{i+1}$ and $B_i$ with $A_{i+1}$ for all $1 \leq i \leq N-1$. Also join $A_N$ and $B_N$.

Here, all these $2*N-1$ are equal in length and are also equal to segments $A_0A_1$ and $A_0B_1$.

The problem requires to find out angle formed by two segments at a specific point. Queries are of four types. Given $N$, $type$ and three integers $x$, $y$ and $z$.

  • Find angle $A_xA_yB_z$ if the query is of the first type.
  • Find angle $A_xB_yA_z$ if the query is of the second type.
  • Find angle $A_xB_yB_z$ if the query is of the third type.
  • Find angle $B_xB_yB_z$ if the query is of the fourth type.

SUPER QUICK EXPLANATION

  • For a given $N$, we can use Triangle sum property and Similarity of trinagles to systematically find out that $\theta = \pi/(2*N+1)$. After this, $\theta$ can be substituted to find the value of required angle.
  • It can also be seen that the angles generated on both lines are following the same pattern. Specifically, Angles formed at point $A_i$ are same as angles formed at point $B_i$. This way, answer to fourth query is exactly same as answer to second query, while answer to third query for triplet $(x,y,z)$ is same as answer to first query for triplet $(z,y,x)$.
  • When $y > 0$, Angle $A_{i-1}A_iB_{i-1}$ is $\pi*(i-1)/(2*N+1)$ Angle $B_{i-1}A_iB_{i+1} = \pi*(2*N+1-2*i)/(2*N+1)$ and Angle $B_{i+1}A_iA_{i+1} = \pi*(i+1)/(2*N+1)$
  • Corner case when $y = 0$, answer is $\theta = \pi/(2*N+1)$

EXPLANATION

Ignore queries for now, first focus on determining the angle $\theta$ between two lines.

Triangle properties used can be found below.

View Content

It can be seen that for a given $N$, Only one value of $\theta$ is valid. Let us try to find this value for say, $N = 4$, as shown in the image below.

alt text

In $\bigtriangleup A_0A_1B_2$, $\angle A_1A_0B_2 = \theta$ and we have $|A_0A_1| = |A_1B_2|$ which implies $\angle A_0B_2A_1 = \theta$. Now, $\angle A_0A_1B_2 = \pi-2*\theta$ which implies $\angle A_2A_1B_2 = 2*\theta$.

Consider $\bigtriangleup A_1B_2A_3$ now, where we have $\angle A_2A_1B_2 = 2*\theta$ and $|A_1B_2| = |A_3B_2|$, implying $\angle A_2A_3B_2 = 2*\theta$. Applying Angle sum property, we have $\angle A_1B_2A_3 = \pi-4*\theta$. Now, $\angle B_1B_2A_1 + \angle A_1B_2A_3 + \angle A_3B_2B_3 = \pi$ which implies $\theta+(\pi-4*\theta)+\angle A_3B_2B_3 = \pi$, giving $\angle A_3B_2B_3 = 3*\theta$. Once again we get $\angle A_3B_4B_3 = \angle A_3B_2B_3 = 3*\theta$.

Proceeding this way, we shall end up with $\angle A_3A_4B_4 = 4*\theta$ and $\angle A_3A_4B_3 = 3*\theta$ giving $\angle B_3A_4B_4 = \theta$. Also, we can find $\angle A_4B_3B_4 = \angle A_4B_4B_3 = 4*\theta$. Using angle sum property in $\bigtriangleup B_3B_4A_4$, we get $\theta = \pi/(2*N+1)$.

Now that we have found $\theta = \pi/(2*N+1)$, we can substitute this value to find out the answer for any query.

Corner case: Query 1 1 0 1 can be handled separately.

To answer queries, we need to notice the patter that $A_{i-1}A_iB_{i-1} = \pi*(i-1)/(2*N+1)$ and $B_{i-1}A_iB_{i+1} = \pi*(2*N+1-2*i)/(2*N+1)$ and $B_{i+1}A_iA_{i+1} = \pi*(i+1)/(2*N+1)$. We can form case based analysis to answer queries of first type.

The answer to queries of second and fourth type is same due to symmetry and answer to such queries is $\pi*(2*N+1-2*y)/(2*N+1)$.

It can be easily seen that answer to a query of the third type for triplet $(x, y, z)$ is same as the answer to a query of the first type for triplet $(z,y,x)$.

Time Complexity

Time complexity is $O(1)$ per test 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. :)


MXDIST - EDITORIAL

$
0
0

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

Setter:Bogdan Ciobanu
Tester:Xiuhan Wang
Editorialist:Taranpreet Singh

DIFFICULTY:

Hard

PREREQUISITES:

Convex Hull, Rotating Calipers, Range Minimum Query (RMQ) and bucket decomposition.

PROBLEM:

Given $N$ points and $Q$ queries $(f,t)$ specifying range $[f,t]$, find the maximum squared euclidian distance between any pair of points within the specified range.

QUICK EXPLANATION

  • Divide the points into buckets and build a convex hull for each bucket (preferably using Monotone Chain algorithm).
  • Build an RMQ sparse table considering each block as one unit, rmq[i][j] representing the convex hull formed by points from blocks in the range $[i,i+2^j-1]$. Hulls should be merged in $O(S)$ where $S$ represent the size of the hull.
  • Most distant point pair from a set of points can be easily found by using Rotating Calipers on the convex hull of the given set of points. To answer a query, make a convex hull consisting of points belonging to partially filled buckets and merge it into the appropriate pre-built convex hull from RMQ for intermediate blocks and calculate the answer.

EXPLANATION

Let us solve a trivial problem first. Given a set of points, find the maximum distance between any pair of points.

Lemma: The pair of points having the maximum distance lie on convex hull formed by points.

Proof: Let us assume we have a point pair AB being the most distant point pair, Point A strictly inside the convex hull and point B on or inside convex hull. Lt us extend this line toward A till it intersects the border of the convex hull, say at segment CD. Now, it can be seen, that at least one of $|BC| > |BA|$ or $|BD| > |BA|$ holds, which contradicts the fact that points $AB$ have the maximum distance. This contradicts our assumption. Hence, for the points to be most distant, both points shall lie on the convex hull.

Now, to find the maximal distance, we can use the rotating calipers technique, explained in brief below.

Let us have a pointer variable p and for every point indexed i, move it forward till the distance of (p+1)th point from the line formed by ith and (i+1)th point is more than distance of pth point from same segment, considering distance of pth and (p+1)th point from ith point. whenever moving from ith point to (i+1)th point, consider its distance of both these points from pth point. The maximum of all these is the distance of most distant point pair.

An important point to note is, that binary search is not going to work, as explained well here.

Merging Convex hulls

We merging convex hulls naively (assuming Monotone Chain algorithm is being used). In Monotone Chain algorithm, we find the points having leftmost and rightmost x-coordinate and build lower and upper convex hull separately. So, while merging, we can use merge-sort type technique to merge lower and upper hulls and removing the useless points in time proportional to the size of the convex hull.

Returning to original problem, we can build a RMQ sparse table, where RMQ[i][j] represent the convex hull formed by points in range $[i, i+2^j-1]$ which can be easily rewritten as the convex hull formed by merging RMQ$[i][j-1]$ and RMQ$[i+2^{j-1}][j-1]$. The details of RMQ can be found here.

Now, Let us evaluate the time complexity of this solution. During preprocessing, we have $log(N)$ levels having $N$ convex hulls. We can see, that merging convex Hull take time proportional to the size of convex hull, resulting in preprocessing time being $O(N*log(N)*S)$ after which, each query may take $O(S)$ time as both merging convex hull and calculating most distant point pair take $O(S)$ time.

Now, the question is, what is the size of the convex hull? We have randomly generated points in the second sub-task while worst cases in the third sub-task. It can be seen from research that the expected number of points in convex hull of $N$ randomly generated points is roughly of the order of $N^{1/3}$ while if we generate points carefully in $MX*MX$ grid, we can have up to around $MX^{2/3}$ points in convex hull, $MX = 2*10^5$ in our case. This explains the difference in time complexity of the same solution for second and third sub-task. See references here and here.

Now, we see, though it may be sufficient with optimizations to pass this solution for the second subtask, it shall be hard to pass the final subtask. So, Let us optimize the preprocessing part to fit the time limit by bucket decomposition.

Let's first divide all $N$ points into blocks of size $SZ$ and now, build RMQ considering each block as one position in RMQ. This way, to answer queries, we can use RMQ to find convex hull formed by points in intermediate blocks and naively build a temporary convex hull by points which lie in partially filled blocks (can be at most 2 for each query). We can merge these two blocks and calculate the answer for this block.

Now, Let us calculate the time complexity of this solution. If $C = \lceil N/SZ \rceil$, we can build RMQ on blocks in $O(S*C*log(C))$ time and answer each query in $O(SZ+S)$ time, resulting in time complexity $O(S*(N/SZ)*log(N) + Q*(S+SZ))$. It works fast enough for $SZ = sqrt(S*N*log(N)/Q)$.

Editorialist solution

The editorialist who like different solutions, choose to use graham scan as means of building and merging convex hulls as opposed to Monotone Chain algorithm in case of setter and tester solution. This slows down editorialist's solution by $log(S)$ due to the fact that for Graham scan, sorting by the polar angle is required while in Monotone Chain algorithm, merging convex hull can be done in merge sort type manner in $O(S)$ for both lower and upper hulls. This is the reason Monotone Chain algorithm is preferred to Graham scan in this problem.

An interesting problem to try is GEOCHEAT.

Time Complexity

Overall time complexity is $O(S*(N/SZ)*log(N/SZ) + Q*(S+SZ))$ where $SZ$ is the block size chosen.

Space Complexity is $O(N+S*(N/SZ)*log(N/SZ))$.

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. :)

WA IN COMB1

$
0
0

Hi, There was a contest held named Code Stumble and for one of the question I was unable to get an accepted solution and I can't understand why. I compared some of the AC submitted code and tried random test cases on both of them and found out that all the answers are same. Can anyone help me as to where possibly I could have gone wrong.

Link to the question : https://www.codechef.com/COMB2018/problems/COMB1/

My logic for the question was that Sum of odd numbers from 1 to 2n is (n - 1)squared: eg. 1 + 3 + 5 = 9 which is 3 squared. 1 + 3 + 5 + 7 = 16 which is 4 squared.

For a series not starting at 1 simply subrtact the smaller square : (1 + 3) + 5 + 7 = 16, Subtrating bracketed terms is 16 - 4 or 4^2 - 2^2 = 12. Generally: a..b odd integers, Sum is ((b+1) / 2)^2 - ((a-1) / 2)^2, both a and b included in sum. Where a and b were calculated as a=2l-1 and b=2r-1

Link to my Solution : https://www.codechef.com/viewsolution/22501845

Thank you in Advance.

COMB2018 Question Copied from Hackerearth

$
0
0

I just found that one of the question of COMB2018https://www.codechef.com/COMB2018/problems/COMB7 is copied directly from a HackerEarth past contest https://www.hackerearth.com/practice/algorithms/string-algorithm/basics-of-string-manipulation/practice-problems/algorithm/swapping-game-6/ .

Those who haven't even submitted simple digit DP problem have also submitted this. Cheers to ABV-IIITM Gwalior for selecting participants for onsite for a prize money of 75k on the basis of this ranklist.

Please consider the rank list removing the above mentioned question copied from other platform since their solutions are easily available.

Invitation to Alohomora

$
0
0

Hello Codechef.

RECursion, the coding community of NIT Durgapur, is organizing an ACM-ICPC style based contest ALOHOMORA. The contest will be held in two stages, online round and onsite round. The online round will be held on 19 January at 21:30 IST.

There will be 7 problems which are to be solved in 2 hours.

Participants need to register themselves in teams of max 2 members. Top 5 teams from NIT Durgapur and Top 5 teams from other institutions in the online round will qualify for the onsite round. The onsite round will be held on 2nd February 2019. There are cash prizes and goodies for top 5 winners at onsite round.

We invite you all to participate in the event.

Liga168 Link Alternatif

EARTSEQ - EDITORIAL

$
0
0

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

Setter:Evgeniy Artemov
Tester:Xiuhan Wang
Editorialist:Taranpreet Singh

DIFFICULTY:

Easy-Medium

PREREQUISITES:

Sieve of Eratosthenes, Number-Theory and Constructive Algorithms.

PROBLEM:

Given an integer $N \geq 3$, find $N$ distinct numbers such that every pair of consecutive numbers have a common factor $ > 1$ and all possible consecutive three elements have no common factor $ > 1$. Also, all numbers found should be between $1$ and $10^9$. First and Last elements are also considered adjacent here.

SUPER QUICK EXPLANATION

  • A simple way to generate $N$ such distinct numbers is to take a prime and multiply two consecutive elements by this number. This way, the condition of co-primes is handled, but the numbers generated go beyond $10^9$.
  • A better way would be to reuse smaller primes while ensuring all the generated numbers are distinct.

EXPLANATION

This is a constructive problem, and hence, have multiple solutions. Feel free to share your solution in comments.

Note: If we represent a number as the product of prime numbers, Prime factorization of a common factor of the pair of numbers is the common part of prime factorization. If we consider prime values or product of few primes, they have relatively lesser factors as compared to composite numbers. So, for most of the problems involving prime factors/factorization/divisibility, prime numbers bear special importance.

Let us try similar problems first.

Try solving the same problem assuming there's no upper bound on maximum values. Now, we can just multiply a distinct prime number to every pair of consecutive numbers. This way, every two consecutive numbers are not co-prime while every three consecutive numbers are coprime.

The above solution is sufficient to solve the first subtask, but in the second subtask, the values exceed $10^9$, so we need something better.

Try solving the same problem, assuming we are allowed repeated values. Here, we can choose prime numbers and multiplying each position by exactly two primes such that every two consecutive positions share prime factor while every three consecutive numbers do not share any prime factor, taking special care to choose a different prime number when reaching the end of the array.

Now, let us solve the original problem.

Think up and try to mix the above solutions so as to reduce the magnitude of generated numbers while keeping each number distinct.

The construction used in my solution is to reserve the first few primes and then multiply the first two positions by a non-reserved prime, next two positions by another prime and so on. Now, We can multiply Last and first position by a reserved prime, second and third position by different reserved prime, and so on. This way, we are guaranteed to have every pair of consecutive positions divisible by that particular prime number, and every three consecutive positions are coprime since no prime is multiplied to three consecutive positions.

Implementation

For this problem, we can preprocess and calculate a list of primes beforehand using the sieve of Eratosthenes and then for every test case, use primes from the list instead of recomputing.

Interesting Fact

This problem can also be approached as a challenge problem where we need to minimize the maximum value generated in sequence. Can you minimize? Say for $N = 50000$

Time Complexity

Time complexity is $O(MX*log(log(MX)) + \sum N)$ per test case where $MX$ is the upper limit till primes are found. Finding primes up to $10^6$ suffices for this problem.

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. :)

Can anybody explain the solution to this interesting string-problem based on optimization ?

$
0
0

This is a problem from previous contest : https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/superior-substring-dec-circuits-e51b3c27/

Problem-Statement :--->

You are given a string of length . If a string contains at least one character whose frequency is greater than or equal to the half of the length of the string, then the string is called superior.

You are required to find the length of the longest superior substring available in the given string .

Note: Here half is considered under integer division i.e. , etc.

Input format

First line: Integer 't' that represents the total number of test cases

For each test case:

First line: Integer 'n' that represents the length of the string

Next line: String 'S' of the length 'N'

Output format :

For each test case, print the length of the longest superior substring in a new line.

Constraints : 1<=T<=10

1<=N<=10^5

The string contains only lowercase English alphabets.


Taking Input from console in Java

$
0
0

Why is taking input from Scanner class is slower compared to using a BufferedReader? I tired to understand from other online sources but it was not elaborate.

Invitation to RATED Contest(Div1+Div2) Enigma on Codechef, 10 January 2019

$
0
0

Hello, CodeChef community,

Tired of searching for the perfect teammates in every competition? Want to prove your worth as a solo coder? Looking for the perfect platform to display your skills?

This solo coding competition is designed for the masterminds who think they perform best alone.

Interested? Wanna find out more about it? No Problem! Plinth in association with Aavas Financiers Limited has a rated contest on CodeChef waiting just for you!

The contest shall be a great opportunity for students to test their programming aptitude. This national level programming contest shall assess the intelligence of participants through various rounds. It is an Online Qualifier Round for the Onsite Round to be held during Plinth'19.

There will be 2 rounds to decide the winner:

First Round: Online Qualifier for onsite will be held on 10th January 2019 on Codechef which is a rated round(Div1+Div2).

Second Round: Onsite round will be held at The LNM Institute of Information Technology, Jaipur on 20th January. More details will be announced later.

The Contest details of Enigma'19 are as follows:
- Contest duration will be 3 hours.
- Solo Coding Event
- Start time: 10th January 2019, 21:00 hrs IST
- End time: 11th January 2019, 00:00 hrs IST

Contest Link: Click Here

Problem Setters: panikxodiacaditya10_tds115

alt text

Good Luck and have Fun!

Extremely unbalanced short contests

$
0
0

It has been my observation that CodeChef's short contests like monthly Cook-Offs and Lunchtimes have become extremely unbalanced in difficulty level.

The best example I have is of COOK101 (December Cook-Off). Being in Division 1, I could not do a single question from that contest (thankfully I made no submissions). I understand Division 1 is meant to be hard but among 5 questions at least 1 should be on the easier side (greedy algorithms, implementation problems, etc.).

But the true problem occurs with Division 2 (COOK101B). Their first question was easy, and the next 4 questions are common with Division 1. Look at the fall in successful submissions, 2080 to 85. That means for more than 95% of participants, acing the Cook-Off meant doing the easy question fast. That was the end of the contest for them.

And usually also, if there are 5 questions in a Cook-Off or Lunchtime, the last 3 have successful submissions in single digits, compared to >100 submissions for the first two problems.

Now, I understand that having hard questions is good for learning. But still the contests are so unbalanced, it's not fun at all. And Division 2 contests are not appropriate for Division 2 level at all. Division 1 also feels like as if its for rating >= 2100.

I think that many other users might also be disappointed with the current difficulty distributions.

Editorial for GARGOYLE - PELT2019

$
0
0

Difficulty: Easy

Problem setter: panik

Prerequisites: Implementations, usage of maps/dictionary(if in python).

Explanation: I this question for statements to not contradict, all the statement arrays of the selected people should be similar. We can do this by taking the count of each distinct array with the help of maps and then Iterating all of them and finding the maximum count with the correct Statements.
Note: A statement array will only be considered correct if the count of True in it is equal to the count of that type of substring. This could be seen in sample Test case 2. Although the count of T T T was 2, the final answer was 1 because according to them all of them were true speaking by according to the 3rd person, he only considered himself to be a true speaking person.

Time complexity: O(nlog(n)) -> log(n) factor occurs due to usage of maps.

Author's Solution: click here

Tester's Solution: click here

DISCHAR - Editorial

$
0
0

PROBLEM LINK:

Author:Lalit Kundu

Tester:Sergey Kulik

Editorialist:Florin Chirica

DIFFICULTY:

cakewalk

PREREQUISITES:

greedy

PROBLEM:

You're given a string. Let's consider a set of all possible strings obtained by deleting some letters from my initial string. From this set, we pick those for which every letter appears exactly once. What's maximum length possible of picked string?

QUICK EXPLANATION:

The resulting string is the one for which each character from the input appears exactly once in the output.

EXPLANATION

We'll make 2 observations and based on them, we'll find the solution. Let's denote by x the initial string and by res the string x after removing some characters (possibly none), such as no two characters are equal and the length of res is maximal.

Observation 1

If a character appears in x, then it must appear in res. Let's proof it: suppose there's a character ch such as it appears at least 1 time in x, but it appears zero times in res. Then, let's add it to res. Since it didn't appear before in res and res already contained each two characters different, after adding ch to res, res will still contain each two characters different. But length of res is increased by one. This is a contradiction of the fact that res was the answer (there exist a string res' for which each two characters are different but length of res' is greater than length of res). The contradiction was obtained from assuming observation 1 is false. Hence, observation was is true.

Observation 2

If a character appears two or more times in x, then it can appear at most once in res. This is a direct consequence of the property that res has each two characters different.

Combining the observations

Let's iterate each character from x. We're build incrementally string res, starting from res = NULL. Suppose we're processing now the character ch. We have two cases.

  1. Character ch didn't appear before in x.
  2. Character ch appeared before in x.

Let's suppose we're in case #1. Using observation 1, we're forced to add the character to res. Now, let's suppose we're in case #2. Since character ch have already appeared in x, it has been already added in res (when it appeared first time in x). Using observation 2, we are not allowed to add the same character in res. So, we're forced to skip this character ch (or equivalently, to delete it from x).

There's a single detail remained unsolved. Suppose we processed the first i characters of x. We need to know whether a character ch (corresponding to position i + 1) already appeared on the first i characters. We can keep an array used[ch] = 1 if character ch already appeared or 0 otherwise. We can use this to know if character ch appeared at first i characters. Then, we update used[ch] = 1 (we know for sure that character ch appears in the first i + 1 characters, as it appears in position i + 1) and move on.

Complexity

Since the only thing we do is to iterate over elements of strings, the complexity is O(n).

AUTHOR'S AND TESTER'S SOLUTIONS:

To be updated soon
To be updated soon

Pizza Slice (XYPIZQ) mini editorial

$
0
0

Consider this diagram. Half of the line segments are drawn in red. The line segments $a_0b_1,b_1a_2,a_2b_3,b_3a_4$ also exist, but are ommited here for clarity.

alt text

We do not yet know what angle $x$ is, but we can work out the other angles in terms of $x$. We will be working in radians here. $\pi\ radians = 180\ degrees$

Because the slice is an isosceles triangle, we can calculate:

$c = \frac{\pi-x}{2} = 0.5\pi - 0.5x$

Knowing that all the inner triangles are also isosceles trangles, we can also calculate:

$d = \pi - 2c = x$

$e = c - d = 0.5\pi - 1.5x$

$f = \pi - 2e = 3x$

$g = \pi - c - f = 0.5\pi -2.5x$

$h = \pi - 2g = 5x$

$i = \pi - e - h = 0.5\pi - 3.5x$

$j = \pi - 2i = 7x$

Also because of the isosceles triangle, $x = i$, so we can now derive the value of $x$:

$x = 0.5\pi - 3.5x$

$4.5x = 0.5\pi$

$9x = \pi$

Now we can go back and substitute $\frac{\pi}{9}$ for $x$ and work out all the angles exactly.

We can also see a pattern in the angles. The angles of the tops of the triangles go:

$x,3x,5x,7x..$

while the angles of the sides go

$0.5\pi-0.5x$

$0.5\pi-1.5x$

$0.5\pi-2.5x$

$0.5\pi-3.5x$

$..$

So we can work out the angles of specific triangles with closed form formula, instead of having to calculate them all.

AGECAL - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:AmirReza PoorAkhavan
Tester:Danial Erfanian
Editorialist:Michael Nematollahi

PREREQUISITES:

NONE

PROBLEM:

An especial calendar and two dates are given. You are asked to calculate the number of days between the first day and the second day. It's guaranteed that the first date is not after the second.

EXPLANATION:

There are many ways to approach this task. Some have better complexity than the others. Since the constraints are not too strict, we're free to choose whichever that is fast enough.

First, let's define two variables; $ans$ as the answer to the problem and $sm$ as the number of days of a non-leap year. Now, Let's calculate which day of the year each given date is. We can do this for each date by iterating over the months that come before the month of that date and adding the number of days of the month to some variable like $res$. finally, we add the day of that date to $res$. A pseudo-code of a function that does the same is here:


int calc(Date date){
    int res = 0;
    for (each month m that comes before date.month)
        res += m.days;
    res += date.day;
    return res;

}

This part takes $O(N)$ complexity.

Now that we know what day of the year our dates are, let's make sure that none of them are the last days of a leap year. It's not necessary, but it makes the implementation a bit easier. It's easy to do so with two if conditions. Something like this:

 
if (day1 == sm + 1) day1--, ans--;
if (day2 == sm + 1) day2--, ans++;
Note that we also alter $ans$ because we're changing the dates. Hence, the answer to the new dates will not be the same as the answer to the old ones.

Now, let's move $day1$ forward so it becomes the same as $day2$. Meanwhile, we modify $ans$ because it changes as we change the dates. It can be done like this:


if (day1 <= day2) 
    ans += day2-day1;
else{
    if (year1 % 4 != 0)
        ans += sm - (day1 - day2);
    else
        ans += sm+1 - (day1 - day2);
    year1++;
}
day1 = day2;

Finally, let's move $year1$ forward until it reaches $year2$. While doing so, we don't change the month and the day of the dates. Subsequently, our dates will be the same once their years match. Here's a code that does this:


while (year1 < year2){
    ans += year1%4? sm: sm+1;
    year1++;
}
This part takes $O(yt)$. Hence, our solution for each test runs in $O(yt + N)$. Refer to the editorialist's code for the complete implementation.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here

Tester's solution can be found here

Editorialist's solution can be found here.


Test Cases

$
0
0

Why doesn't CODECHEF make the test cases visible after the contest?

Will we not get a certificate of participation in Snackdown this time like we got in previous edition?

Need your help! YVMUN

$
0
0

I cant understand why i am getting wrong answer in YVNUM. I matched all testcases and corner cases that i can think of ..with the testers solution and my solution ..all are hiving same answer..therefore i request u to guide me where i am getting wrong .given below is the ideone link of my code please do check https://ideone.com/e.js/GrO14s"

Create a Java program that will compute for the subject grade of a student based on the specification stated below

$
0
0

String [ ][ ] student= {{“Francis M. Reyes”,”92”,”89”,”93”,”94”},{“Joseph E. Estrada”,”80”,”70”,”81”,”78”},{“Ferdinand E. Marcos”,”77”,”93”,”83”,”90”},{“Cory C. Aquino”,”78”,”73”,”70”,”70”},{“Emilio A. Aguinaldo”,”78”,”86”,”78”,”80”},{“Ramon M. Magsaysay”,”91”,”81”,’’78”,”80”},{“Gloria M.Arroyo”,”84”,”74”,’’94”,”88”}};

Given the String array student having the initialization shown on the table above get the following:

GWA-General Weighted Average

Name of the student who obtained the highest GWA

Name of the student who obtained the lowest GWA

GWA=Prelim Grade 20% + Midterm Grade 20 % + Pre-final Grade 20% + Final Grade 40%

Name of the student who obtained the highest Prelim Grade

Name of the student who obtained the highest Midterm Grade

Name of the student who obtained the highest Pre-final Grade

Name of the student who obtained the highest Final Grade

Names of passing students (GWA>=75)

Names of failing students (GWA<75)

SAMPLE OUTPUT:

Francis M. Reyes GWA: 92.4

Joseph E. Estrada GWA: 77.4

Ferdinand E. Marcos GWA: 86.6

Cory C. Aquino GWA: 72.2

Emilio A. Aguinaldo GWA: 80.4

Ramon M. Magsaysay GWA: 82

Gloria M. Arroyo GWA: 85.6

Student with highest GWA: Francis M. Reyes

Student with Lowest GWA: Cory C. Aquino

Student with Highest Prelim Grade: Francis M. Reyes

Student with Highest Midterm Grade: Ferdinand E. Marcos

Student with Highest Pre-final Grade: Gloria M. Arroyo

Student with Highest Final Grade: Francis M. Reyes

List of Passing Students:

Francis M. Reyes

Joseph E. Estrada

Ferdinand E. Marcos

Emilio A. Aguinaldo

Ramon M. Magsaysay

Gloria M. Arroyo

List of Failing Students:

Cory C. Aquino

PALIN is getting NZEC even after running successfully with test cases on codechef IDE

Viewing all 39796 articles
Browse latest View live


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