CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Wiki » March CookOff Contest Problem Editorials

March CookOff Contest Problem Editorials

 

EXPCOMM (written by Anton Lunyov)

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

MONTRANS (written by Anton Lunyov)

You just simply need to perform this procedure no more than 10000 times and at each step update the maximal profit and optimal number of times to get it if needed. Also note that after 10000 times we definitely obtain some amount of money that we had before and hence after that we can't obtain better profit than earlier so it follows that for any input data the answer is not greater than 10000. The last sentence of the output specification was added just to make the problem trivial. In fact the maximal answer is less than 200.

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

SNAKY (written by Anton Lunyov)

For each cell (r, c) of the grid we can compute the time T[r][c] when the snake makes this cell free. It means that for the next T[r][c]-1 moves this cell will be occupied by the snake and at the move T[r][c] tail will leave it (this cell can be immediately occupied by the head but we don't worry about that). For this just simply model the snake movements that given in the input and mark k-th cell in this sequence by the number k. (First cell is occupied by the tail and we marked it by 1 and really snake will leave it just in one move. Second cell will be free after two moves and so on). Since N, M <= 1000 we can do this using usual two-dimensional array. After that, model the movement of the snake head. If at k-th step the head visits the cell (r, c) then it collides with its body if and only if k < T[r][c]. Indeed, if k < T[r][c] then by definition of T[r][c] it means that the snake body still occupies this cell and collision happens otherwise all is fine. So if at some moment we obtain this inequality then we should output "BODY" and k-1, otherwise snake will collide with the wall. So the overall complexity is O(M N + L + max{M, N}) per test case (we need to clean corresponding two-dimensional array before treatment of the snake movement hence we have M N term, L corresponds to the analyzing of the snake movement and max{M, N} corresponds to the movement of the head.)

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

PALIPALI (written by Anton Lunyov)

Let s be the given string, n is its length and s[i] denotes i-th symbol of s (0 <= i < n). Also we denote by s[i, j) the substring of s from i-th symbol inclusive to j-th symbol not inclusive. Compute for each i the so-called palindrome radius d[i]. Namely d[i] is the maximal number k such that the substring s[i - k, i + k) is a palindrome. This can be done by the elegant Manacher algorithm in O(n) time. Here you can read about it. Also you can use hashes and binary search to find this in O(n log n) time. So now assume that we find numbers d[i] (0 <= i < n). Obviously the substring s[i - k, i+3*k) is beautiful if and only if j <= i + d[i] and d[j] >= 2 * (j - i) where j = i + k. (Since j <= i + d[i] then substring s[i - k, i + k) is a palindrome and since d[j] >= 2*k then s[i - k, i+3*k) is also a palindrome and has the form ttrttr where t = s[i - k, i)). So for each i the number of beautiful substrings of the form s[i - k, i+3*k) is equal to number of those j <= i + d[i] such that 2*j - d[j] <= 2*i (and of course j > i). Let's sort all pairs (2*j - d[j], j) by the usual lexicographical comparator. For each i from 0 to n-1 we at first add all new values of j such that 2*j - d[j] <= 2*i to some data structure T then delete i from T and then add to the answer the number of elements in T that are not greater than i + d[i]. Obviously when we are finding the last quantity, T contains only those values of j such that i < j (since we delete i at each step) and 2*j - d[j] <= 2*i. So the last quantity is number of beautiful substring of the form s[i - k, i+3*k). What is T? We can use Cartesian tree or any balanced binary search tree that support order statistics as T. Also we can use segment tree for sum on the array of numbers from 0 to n-1. Then insertion (deleting) of the element is simply adding (subtraction) of one to (from) the corresponding element of the array and the number of needed beautiful substrings is the sum of values of the corresponding segment of the array. Finally you can use Fenwick tree instead of the segment tree (see author solution). So the overall complexity is O(n log n).

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

GROUPING (written by Anton Lunyov)

At first sort all employees by the numbers c[i] in decreasing order. So we can assume that c[0]>c[1]>...>c[n-1]. If we choose some friendly group i1, i2, ..., ip then the quality of food in the kitchen will be 2c[i1]+2c[i2]+...+2c[ip]. Since all c[i] are different and 2i > 2i-1+...+22+2+1 then comparing friendly groups by food quality is equivalent to the lexicographical comparing.

Hence we can use greedy strategy to find the best friendly group: at each step we must add to the group the employee which compatible with all employees in the group with the largest value of c[i]. We can use several techniques in order to implement this approach efficiently. The simplest way is to loop through all employees and for each employee count the number of compatible with him employees that belong to the current friendly group. If this number coincides with the cardinality of the group then we add him otherwise not.

If we have some friendly group then in order to obtain next friendly group by the value of food quality we need to do the following. Throw away the last employee from the group and starting from the next employee follow the greedy strategy described above (in some cases you can't add any employees after that but it's fine it only means that the next friendly group is obtained by simply throwing away the last employee). Thus the overall complexity is O(n log n + k (n+m) ).

You can view solutions here:

Problem Setter Solution

Problem Tester Solution


Comments

  • Login or Register to post a comment.

mce_bogus="1"

moham @ 21 Mar 2011 11:52 AM

mce_bogus="1"

In the EXPCOMM problem you

fmm @ 30 Mar 2011 11:56 PM

In the EXPCOMM problem you say:

denote I[p] = { 0 ... p-1 }, and t of I[p] (...) C(t). Represent t = (p^j) * s.

but being p of I[p], j = 0.

I mean, how "t" can be a

fmm @ 31 Mar 2011 12:11 AM

I mean, how "t" can be a power of p, if it belogs to the I[p] set ({0... p-1}), which doesn't contains power of p.

Yes, it's a mistake. I mean

anton_lunyov @ 31 Mar 2011 08:02 AM

Yes, it's a mistake. I mean I[p]={0,1,...,p^e-1}.

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com