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 » September CookOff Contest Problem Editorials

September CookOff Contest Problem Editorials

 

Problem Setter for the contest was David Stolp.
Problem Tester for the contest was Aleksey Tolstikov.

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

HOTEL: Hotel Bytelandia

Several approaches exist to this problem. The simplest solution is to simply count the number of guests present at the hotel for each time from 0 to 1000, and print the maximum. A guest is present if the current time is greater than or equal to their arrival time but less than their departure time.

For a better approach, call arrivals and departures "events". Sort the 2*N events by the time at which they occur. In case of ties, place departures before arrivals. Then process the events in order, adding 1 to a count whenever an arrival occurs, and subrtacting 1 when a departure occurs, and print the maximum count.

Problem Setter's Solution
Problem Tester's Solution

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

DIGROT: Digit Rotation

One way to perform a digit rotation is to convert the number to a string, perform a rotation on the string, then convert back to an integer. Another method is to use divisions and multiplications by 10. Although it was possible solve using a depth first search or breadth first search, the maximum value always occurs after one of the following:

Left rotations only (at most 6)
Right rotations only (at most 6)
A single left rotation followed by a single right rotation
A single right rotation followed by a single left rotation

To see why this is so, consider the similar problem where we have to perform 0 or more rotations. In this case, the answer will always have the same number of digits as the original number. Since the number of digits cannot increase as the result of a rotation, each rotation must leave the number of digits unchanged. Suppose we have some sequence of rotations that produces the maximal answer. Any consecutive rotations in different diretions must have no net effect. Thus we can remove such pairs until all rotations are in the same direction. Additionally, any D rotations in the same direction have no net effect, where D is the number of digits in N. We can remove sets of D rotations until fewer than D rotations remain. Now if there are exactly D-1 rotations, we can replace them with 1 rotation in the opposite direction. Thus at most D-2 rotations are needed, all in the same direction.

Now the original problem is equivalent to performing one rotation followed by 0 or more additional rotations. Therefore the solution will involve performing one rotation in one direction, followed by 0 or more rotations in another direction (which may be the same as the first direction). We can show that each of the 4 possible cases can be simplified to one of the 4 cases above.

To solve using a breadth first search, use a set and a queue (or, for a depth first search, use a stack). Initially, insert N into the queue. Then as long as the queue is non-empty, remove a number from it, and rotate that number in each direction. For each of the 2 resulting numbers, check if it exists in the set. If not, insert it into both the set and the queue, otherwise do nothing. When the queue is empty, the largest element in the set is the answer.

Problem Setter's Solution
Problem Tester's Solution


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

LASTDIG: Last Digit Sum

This problem can be solved using dynamic programming, however a more sophisticated solution exists. Denote C(N) as the sum of D(N) over all values from 0 to N-1 (inclusive). Our goal is to calculate C(B+1)-C(A). Note that the first 10 values of D(N) are 0, 1, 4, 3, 8, 5, 2, 7, 6, 9. Each number from 0 to 9 appears exactly once. Also note that D(10*k + i) = (D(k)+D(i))%10, where 0≤i<10. It follows that each number from 0 to 9 will also appear exactly once in D(10*k + i) as i ranges from 0 to 9 and k is fixed. Therefore, C(10*k)=45*k for all k. This allows us to compute C(N) quickly by using C(N) = 45*N/10 if N is a multiple of 10, and C(N) = C(N-1) + D(N-1) if N is not a multiple of 10.

Problem Setter's Solution
Problem Tester's Solution

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

TARPRACT: Target Practice

A naive DP solution to this problem solves it in O(N*2^N). Store the state of the targets in a bitmask, and for each of the possible 2^N states, simulate each of the possible outcomes from firing at each of the N targets, then choose the one with the lowest expected number of shots. Since it's possible to hit a target that's already been hit, the expected number of shots required is (1+sum(P(T)*E(T)))/(1-Q), where P(T) is the probability of hitting target T (and T hasn't been hit yet), E(T) is the expected number of additional shots needed if target T is hit, and Q is the probability of hitting a target that's already been hit or missing completely (if Q is 1, the shot is useless).

The solution can be improved by noticing that once 2 adjecent targets have been hit, the remaining targets to the left of these targets can be considered indepently of those to the right, since no single shot could ever have the potential to effect both. The number of states where no 2 adjacent targets have been hit is O(φ^N), where φ is (sqrt(5)+1)/2, the golden ratio. The total runtime is thus O(N*φ^N).

Problem Setter's Solution
Problem Tester's Solution

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

YASEQ: Yet Another Sequence

Let us first prove that the entire sequence consists only of N-1, N, and N+1. The proof proceeds inductively. Suppose for some i > N that the first i terms of the sequence are all N-1, N, or N+1. For all j satisfying i-N+1 < j ≤ i we have A[j] ≥ N-1 = i-(i-N+1) ≥ i-j, hence A[i] ≥ N-1. Similarly, for all j satisfying j < i-N-1 we have A[j] ≤ N+1 < i-(i-N-1) ≤ i-j, hence A[i] ≤ N+1. It follows that N-1 ≤ A[i] ≤ N+1 for all i.

We now have that A[i] depends only on A[i-N-1] and A[i-N]. Specifically:

A[i] = N if A[i-N] == N-1 and A[i-N-1] == N+1
A[i] = N+1 if A[i-N] != N-1 and A[i-N-1] == N+1
A[i] = N-1 if A[i-N] == N-1 and A[i-N-1] != N+1
A[i] = N if A[i-N] != N-1 and A[i-N-1] != N+1

Now let's consider sequences consisting only of N and N+1, and sequences consisting only of N-1 and N. For a sequence consisting only of N and N+1, we'll have A[N]=N, and then A[i+N+1]=A[i] for all i. For a sequence consisting only of N-1 and N, we'll have A[i+N]=A[i] for all i. Call the N+1 terms "pegs", and the N-1 terms "holes". Pegs are periodic with period N+1, and holes are periodic with period N. What happens when a sequence contains both pegs and holes? Simple. Every time a peg and hole collide, the peg fills the hole and they are both eliminated.

So all we have to do is figure out which pegs fill which holes and when. The process works as follows. Create a stack. Starting at the beginning of the sequence, every time a peg is found push it on the stack. Every time a hole is found, match it with the top peg from the stack (unless the stack is empty, in which case do nothing). If i is the index of the peg, and j the index of the hole, then the index x at which they collide will satisfy x = i (mod N+1) and x = j (mod N). We can use the Chinese Remainder Theorem to find that x = j*(N+1) - i*N (mod N*(N+1)).

Now to answer each query, compute the position of the peg and hole associated with the query. If there is a peg or a hole, check if it has been eliminated.

Problem Setter's Solution
Problem Tester's Solution

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


Comments

  • Login or Register to post a comment.

The problems are really nice

paramanantham @ 19 Sep 2011 10:55 AM
The problems are really nice Hats off to DAVID stolp

@DigitRotation ... Is there

blackBird @ 19 Sep 2011 11:02 AM
@DigitRotation ... Is there some simple support for the claim ( of the 4 possibilities for max ) or is it too much of math ? If its some simple logic, please tell. Thanks :)

Solution of Target Practice

snktagarwal @ 19 Sep 2011 12:48 PM
Solution of Target Practice is not very clear. Can you elaborate on the scheme to calculate the expected value for a single configuration from amongst 2^N. Thanks

Are cook-off problems put up

pragrame @ 19 Sep 2011 04:18 PM
Are cook-off problems put up in practice? It'd be nice to check if some of our (late) codes would pass the judge, and I didn't find TARPRACT among the practice problems.

@DigitRotation: Could you

gaurav_saxena2000 @ 19 Sep 2011 04:27 PM
@DigitRotation: Could you please explain a bit how to use breadth first search or depth first search in this problem ?

Please can someone throw

mtk @ 19 Sep 2011 10:40 PM
Please can someone throw light on DIGROT. How the 4 possibilities have been reached upon?

@all : PIEGUY'S Editorial is

paramanantham @ 22 Sep 2011 12:05 AM
@all : PIEGUY'S Editorial is clear about Digit rotaation...Why are u confusing the things guys... we can do right shift as well as left shift...Why u r going to use BFS or DFS?? example: for 90 left rotation-9; right rotation -9; for 211011; left rotation- 121101,112110,11211,11121,11112,21111 but if u rotate one left and one right shift then 121101->211011(actual number) but already done 2 rotation...so its the max number... :) Hats off to David Stolp :) for nice problems &snktagarwal: if u know the ans exp for target practice please share...

Could somebody explain how

abhinavatul @ 29 Sep 2011 07:05 PM
Could somebody explain how the expected number of shots is defined by given formula?
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