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

August CookOff Contest Problem Editorials

 

CodeChef August 2011 Cook-Off : Editorials

 

Problem Tester for the contest was Harsha S

Problem Setter for this contest was Anil Kishore

I hope you liked the problems of this cook-off. Though they may be relatively easier than usual cook-offs, this time I thought a few more people should solve more number of problems and give problems that are more idea centric and needs less code. In Short - contests with relatively easier problems, all levels of coders can test their skills, be it speed or solving more number of problems than before. Hope these are satisfied tonight. For some problems, I'll write just the idea in short, and a more detailed explanation will be provided if needed. Please do comment below.

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

NDLVOTE : Vote for the Noodle Soup

 

The score starts with 0 and we are given total score after each of Po's clicks and we need to find the minimum number of users other than Po that could have possibly voted. The important thing to notice is, we just need to maintain the total number of users so far that have voted at least once, and we have the freedom to assign +1 or -1 to each of them :). We can get the total score of users other than Po by just subtracting Po's vote,

which is just the vote he clicked now. Also, if we can reach a score of S, we can also reach -S ( all votes reversed ). So, we will try to get the score T = abs(S). If we have N users already and to get a total score of T,

1.) T >= N : All N users vote +1, still we need score of (T-N) more, so we need (T-N) more users

2.) T < N : Let some T out of N users vote +1. The remaining (N-T) users should contribute zero, which is possible only if (N-T) is even, else we need just one more user.

 

Problem Setters Solution

Problem Testers Solution

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

DRAGNXOR : Open the Dragon Scroll

 

We need to make A^B maximum by shuffling the 1 bits within A and B. As 1 ^ 0 = 0 ^ 1 = 1, we need to make maximum number of (1,0) or (0,1) pairs, where a pair means the corresponding bits from A and B at a particular bit position. Also, to make the result maximum, we want all the ones towards the most significant side (left). Lets pair each 1 bit of A with a 0 bit of B. There can be at max x = minimum_of( number of 1s in A, number of 0s in B). Similarly, pair each 1 bit of B with a 0 bit of A. There can be at max y = minimum_of( number of 1s in B, number of 0s in A). Rest of the pairs are either (1,1) or (0,). So, the number of ones in the result is at max P = x + y. The answer is (1111...) : P times followed by (...000) : N-P times, which is nothing but the integer ( ((1<<P)-1) << (N-P) ).

 

 

Problem Setters Solution

Problem Testers Solution

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

 

ROTSTRNG : Rotate the String

 

We are given a string S and we keep rotating it till we get back to the string S. Note that this can always be possible, if we rotate S a multiple of N times, where N is the length of S. But, we are more concerned if it can be reached before that. Let S(i) = S[i..N-1] + S[1...i-1]. So S(0) = S. Once we know for each i, if S(i) equals S, then we can simulate the rotations of Monkey and Po, as any rotation will result in a string which is one of S(i). We can use KMP here :). Consider a string S2 = S + S and find the KMP Failure Function F for S2. F[i] denotes the length of the maximum proper-suffix that is also prefix of S2.

If F[i] = N, then N length suffix of S2[0,..,i], i.e., S2[i-N+1,..,i] = N length prefix of S2[0,..,i] = S2[0..N-1] = S. So, we can find all such indices using O(n) KMP and then just simulate the rotations by just going through the S(i)s in order. Note that answer will always exist and there is never a -1 case.

 

 

Problem Setters Solution

Problem Testers Solution

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

 

TKCHOCS : Collect the Chocolate Chips

 

After reading the problem, you may feel its a Dynamic Programming (DP) problem. Yes, it is.. but there are few more things to consider. Po moves down and Mantis moves left and they need to maximize the choco chips they collect. You can run a dp normally, but the problem with this is, you also need to remember the cells visited by Po and do not add that when Mantis visits it again. That doesn't sound good. If you observe carefully, Po should always move down and Mantis should always move left, and they both should reach (N,1). Both of them can not cross the diagonal ! So, they can only meet on the diagonal. We can as well try all possible cells at which both of them reach the diagonal ( refer setters solution ), or you can do a O(n) method going from bottom-top on the diagonal ( refer testers solution ). Complexity is O(n^2).

 

 

Problem Setters Solution

Problem Testers Solution

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

 

KCHIPS : Angry Chefs - Crispy Chips

 

We can answer queries off-line. A very high-level explanation is below.

Let's have a S ( data structure ) where we store some indices, one for each village. We process villages in increasing order of indices ( as will be explained below ) and in S we just store the Kth most recent index of that village ( if it exists i.e., it occurred more than K times already ) so far, for each of the villages.

We have villages ( indices i ) and queries ( intervals [a,b] ). We take all indices i and all end points of intervals i.e., b's and sort them and process in increasing order.

When you process an element

- If its a village index, note it down under the indices of this village. Update S accordingly i.e., remove old Kth recent index and insert new Kth recent index

- If its a query interval end b, get the corresponding a, and the answer for this query is just the number of integers in S that are greater than equal to a. Store the answer at the query index in ans[] array.

 

Finally, print ans[] array.

We can use a BIT ( Binary Indexed Tree ) as S. If you are a Java fan, you will love to see the nicely written very abstract solution of the tester.

 

 

Problem Setters Solution

Problem Testers Solution

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

Comments

  • Login or Register to post a comment.

@SITZ: they are posted now :)

admin @ 22 Aug 2011 12:58 AM

@SITZ: they are posted now :)

Anil Kishore's contest is

mkagenius @ 22 Aug 2011 03:37 PM
Anil Kishore's contest is always nice :) Can you explain a little bit , the hashing method for Rotate string.

what is KMP in rotate string

rockjiten @ 22 Aug 2011 08:56 PM
what is KMP in rotate string

@admin, have you considered

hacker007 @ 22 Aug 2011 11:11 PM
@admin, have you considered releasing the test data after each contest? I believe it would be very helpful.

KMP is Knuth Morris Pratt

foofoo @ 23 Aug 2011 09:18 AM
KMP is Knuth Morris Pratt algorithm, http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

Just one thought about Rotate

foofoo @ 23 Aug 2011 09:21 AM
Just one thought about Rotate String, author says "Note that answer will always exist and there is never a -1 case." :). This is really a non trivial thing to me. Also in the solution by author the last loop runs only n times? Would someone explain more?

@foofoo : thanks for giving

flying_ant @ 23 Aug 2011 11:08 AM
@foofoo : thanks for giving the link to wiki page on KMP.. I should have put that in first place :) Regarding your question on Rotate String, lets say you start with 0 and each time add +x to it, generating the sequence {0, x, 2x, 3x, ... } and you take (mod n) each time. This sequence will come back to 0 i.e., {0, x%n, 2x%n, 3x%n, ... , 0 } after k steps, where k = n / gcd(x,n) . So, for any x it will be less than or equal to n and after k steps you will be back to index 0, which is the starting index of original string S :) .. @mkagenius : thank you. For Rotate String, all you want to check is.. after some rotation, Is (suffix of S) + (remaining prefix part) == S ? You can use Hashing for that, but it may not work always. Store the prefix hashes in A ( i.e., A[i] = hash(S[0..i]) ) and Suffix hashes in B ( i.e., B[i] = hash(S[i..n-1] ) in O(n). From this you can get Hash if any rotated string in O(1). Note that this may not work always, unless you have a good hash function or combination of more than one hash functions. I'm also interested to see a solution using this.. did any one use this in contest ?

whats is mean by contribute

piyushurpade @ 7 Oct 2011 10:22 AM
whats is mean by contribute zero

in vote problem

piyushurpade @ 7 Oct 2011 10:22 AM
in vote problem
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