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 » April 2011 Contest Problem Editorials

April 2011 Contest Problem Editorials

 

L2GRAPH (written by Zac Friggstad)

 

This is a special instance of the "metric embedding" problem. Recall that a metric space is a collection of points with a distance function d(x,y) satisfying d(x,x) = 0, d(x,y) = d(y,x), and d(x,y) <= d(x,z) + d(z,y) for any points x,y,z in the metric. The shortest-paths-in-a-graph distance is a metric as is the Euclidean distance in any d-dimensional Euclidean space. A well-studied problem is how well we can embed one metric space in another. Here, an embedding from a metric X to a metric Y is simply a mapping of the points in X to the points in Y. The quantity L/K in the problem is called the "distortion" of the embedding.

An important result is that any metric on n points can be embedded in some Euclidean space of dimension d with distortion L/K where d*L/K = O(log^2 n). More specifically, one can obtain distortion O(log n) in O(log n)-dimensional space. There are graphs (constant-degree expanders) whose shortest-path metric has distortion Omega(log n) with any embedding into Euclidean space of any dimension. Furthermore, these graphs cannot be embedded into o(log^2 n)-dimensional space with constant distortion, so these results are essentially tight. The algorithm for finding such an embedding is actually quite simple and can be easily implemented in polynomial time. Strictly speaking, it is a randomized algorithm and the expected distortion is O(log n), but a deterministic variant can be developed. The interested reader is referred to [1] and [2]. In particular, theorem 3.1 in [1] contains many useful and interesting facts regarding embedding a finite metric into more structured spaces.

1) J. Bourgain, "On Lipschitz embedding of finite metric spaces in Hilbert space", Israel J. Math, 52, 1985, p. 46-52.

2) N. Linial, E. London, and Y. Rabinovich, "The geometry of graphs and some of its algorithmic applications", Combinatorica, 15(2), 1995, p. 215-245.

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

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

GENIND (written by Zac Friggstad)

 

The problem breaks down into checking cycles of even length and cycles of odd length in different ways. Cycles of even length are easy once the following fact is established. Say a cycle C is bad if the sum of the f(v) values of nodes in the cycle exceeds floor(|C|/2). Then we have that no cycle of even length is bad if and only if no cycle of length 2 is bad. One direction is obvious so let's assume we have a bad cycle v_1, v_2, ..., v_k of even length. That is, assume the total of the f(v_i) values exceeds k/2. Consider the edges (v_1,v_2), (v_3, v_4), ..., (v_{k-1}, v_k). There are k/2 edges and the total weight of the f(v_i) values exceeds k/2, so then there must be edge (v_i, v_{i+1}) with f(v_i) + f(v_{i+1}) > 1 so that edge is bad. Thus, we only have to check f(u) + f(v) for edges u,v to see if there are any bad cycles of even length which takes O(m) time (discounting the cost of fractional arithmetic, though using floating point numbers would probably be safe enough for this problem).

Finding bad cycles of odd length takes a bit more effort. Once we have verified that f(u) + f(v) <= 1 for each edge e = (u,v), let's add weights to the edges of G where edge e = (u,v) gets weight g(e) = 1-f(u)-f(v) (which is non-negative). Let C = v_1, v_2, ..., v_k be an odd length cycle and notice that the weight of the edges in C is k - 2*(f(v_1) + f(v_2) + ... + f(v_k)). If C is a bad cycle then the sum of the f(v_i) values exceeds (k-1)/2 so the sum of the edge weights of C is strictly less than k-2*(k-1)/2 = 1. On the other hand, if C is a good cycle then the sum of the edge weights of C is at least 1. So, we have reduced the problem of finding a bad odd length cycle to that of finding an odd length cycle whose total edge weight is strictly less than 1.

Now, form a bipartite graph B with a copy of each node in V on each side. For an edge e = (u,v) with weight g(e), connect u on the left to v on the right and also connect v on the left to u on the right with a weight g(e) edge. It isn't too hard to argue the following. For any node v, there is an odd length cycle in G of total edge weight strictly less than 1 if and only if the shortest path from the copy of v on the left of B to the copy of v on the right of B is strictly less than 1. So, we just have to compute the length of the shortest path between the two copies of each node using Dijkstra's algorithm which, when using the standard implementation of Dijkstra's in contest settings, is n calls to an O(m*log m + n) algorithm.

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

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

RECTCNT (written by AnhDQ)

 

The key point of this problem is: there is not any three of the points sharing a same X-coordinate; and the given points are all separate. So we can manage all "vertical" segments in a list by their X-coordinate and length. Then we group the same segments in length by sorting, for every group of K segments, we have K*(K-1)/2 respective rectangles. Sum up all results and print out!

Complexity: O(N x log2 N)

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

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

CPOINT (written by AnhDQ)

 

We can solve this problem by using Ternary Search algorithm:

- At first, we take a look at a sub-problem: Finding an integer point on the line y=c satisfying its sum of distances to N given points (S') is mininum. In this sub-problem, every integer point on the line y=c will lead to a respective value of S', and all of that values together make a parabolic-like curve on the plane. So we can use Ternary Search algorithm to find the respective mininum value of S'.

- Just imagine that the problem we have to solve is upgraded from the above one, since all the minimum values of S' will together make a parabolic-like curve from which we can find the minimum value of S. Our task is applying Ternary Search algorithm again!

- On the other hand, we can also imagine that all values of S (for respective integer points) will together make a parabola in 3D-space, so our work is using Ternary Search in certain layers when we try to cut it by a plane!

See the source code for details and find more information about the Ternary Search algorithm at: http://en.wikipedia.org/wiki/Ternary_search

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

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

SPREAD (written by AnhDQ)

 

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

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

NUMGAME2 (written by Snigdha Chandan)

 

For this particular combinatorial game theory problem the the values of N for which the first player looses are 1,5,9,13,17,21,25 etc.

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

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


Comments

  • Login or Register to post a comment.

I found this month's problem

rudradevbasak @ 12 Apr 2011 05:10 PM

I found this month's problem qualtiy to be disappointing.

By my estimate, this was the intended breakup:
Easy : NUMGAME2
Medium : SPREAD, RECTCNT
Hard : CPOINT, GENIND
Challenge : L2GRAPH

Here's what I have to say about each.

NUMGAME2 : Fine for an easy problem

SPREAD : Standard Textbook Question, this was the one which disappointed me most. I see that the author intended Segment Tree solutions to TLE. But one can see that 5 of the fastest 12 submissions are Segment Tree based. Even the BIT trick is fairly standard.

RECTCNT : Easy for a 'Medium' problem. Especially when a harder version exists at https://www.spoj.pl/problems/RECTANGL/. Also by my estimate there is only 1 test file with just 10 test cases, which in my opinion is a bit weak.

CPOINT : Way too easy for a 'Hard' problem. Add to that very weak time limits, there are almost 50 people with total execution time < 1 sec for 2 test files (my estimate) where time limit for 1 test file is 15 secs !

GENIND : Nice problem, although one would expect the hardest problem of a contest to be a bit harder.

L2GRAPH : Good challenge problem. Still, I would want Challenge problems to be such that they tax the mind more, and depend less on looking up Approximation Algorithms for well researched problems. Also the error in the judge took multiple days to spot when there were more than one complaints in comments to this problem.

Besides this, it would be nice if there were notifications telling us when Rejudgings have been completed.

for num game read the best

dabbcomputers @ 12 Apr 2011 06:37 PM

for num game read the best tutorial.

http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=algorithmGames

Yes , I agree with

mkagenius @ 12 Apr 2011 08:05 PM

Yes , I agree with Rudradev.

Regarding L2GRAPH.

Looking up research paper for specific approximate implementations is such a waste of time.

I don't think anyone would have done that if it would not have been for better rank ( and money for some people ).

Do you except anyone would remember that algorithm past this month, I don't think so.

 

IN THIS CONTEST I SAW A NEW

dabbcomputers @ 12 Apr 2011 09:54 PM

IN THIS CONTEST I SAW A NEW FUNCTION USED BY MANY USER i.e getchar() and getchar_unlocked

please anybody clear the difference in between both functions..?

@Amritpal getchar_unlocked is

balajiganapath @ 12 Apr 2011 10:40 PM

@Amritpal

getchar_unlocked is not thread safe (ie it does not use locking), so it is faster than getchar.

Many of the top solutions to

pieguy @ 13 Apr 2011 05:37 AM

Many of the top solutions to CPOINT (and also both the tester's and setter's solutions) will fail a test case such as:

 

2

-3 -5

5 8

 

because the best integer point is not necessarily close to the best non-integer point.

@balajiganpathi thnx for ur

dabbcomputers @ 13 Apr 2011 10:29 AM

@balajiganpathi thnx for ur response.... could u more elloborate it......? cos there is no concept of threading or multithreading in c,c++

@admin when would you add

dabbcomputers @ 13 Apr 2011 10:30 AM

@admin when would you add scores to the profiles...?

I think CTPOINT can be best

foofoo @ 13 Apr 2011 05:55 PM

I think CTPOINT can be best done using Weiszfeld's algorithm ( which is just the iteration over partial derivaties w.r.t x and y ) which will converge faster and it will be super fast if you start with the average i.e. medians for x and y.. this was what i was trying but couldnt get it right ..... god damn $%#&^&%&$&#

If i am not mistaken this is different from what the editorial mentions.

Thanks

> CPOINT : Way too easy for a

madkite @ 14 Apr 2011 02:14 PM

> CPOINT : Way too easy for a 'Hard' problem. Add to that very weak time limits, there are almost 50 people with total execution time < 1 sec for 2 test files (my estimate) where time limit for 1 test file is 15 secs !

It isn't so easy like you can think... Even official author's solution give sometimes incorrect result. You can try generate maximum random input data with maximum ranges - in most cases there is exists best result than one gives by this. If you use double precision (IEEE 754) - target function have a lot of local minimums.

For example. This is one example of correct input data: http://pastebin.com/raw.php?i=r1Pt0LEt.

Problem Tester Solution gives following result: 1549168790163.353027.

Problem Setter Solution give following result: 1549168790163.349365 (point with coordinates {x=53474448, y=-6903807}).

But you can try to calculate result for point {x=53474437, y=-6903806} (use function calXY() from author's solution) - it have better score (sum of distances less more than 1e-3): 1549168790163.348145.

And this point {x=53474469, y=-6903803} give better result that author's result point if you use better precision than double during calculation.

BTW, real distance is a bit greater than one calXY() returns. But problem statement requires absolute precision 1e-6.

So it was sometimes very difficult to get AC on solution even with right minimization algorithm that use double. I have pushed my solution only when added random search.

> Add to that very weak time

madkite @ 14 Apr 2011 02:47 PM

> Add to that very weak time limits, there are almost 50 people with total execution time < 1 sec for 2 test files (my estimate) where time limit for 1 test file is 15 secs !

Forgot to say: time limit isn't weak. You are lucky if you wrote in 5 min stupid trenary search with double variables and got AC with 0.5 sec execution time. Another person, for example, wrote advanced solution that uses long precision, that uses some trickly multidimensional quasi-Newton optimization method, that returns in 0.001 sec more precise result, but codechef verdict is WA - he need to add random/bruteforce search for minimum of function that use double. So 1 sec per case for such search is a rather hard limit.

For the question CPOINT I had

pratikmoona @ 15 Apr 2011 01:27 PM

For the question CPOINT I had a couple of doubts. Could it not be possible that there be another local minima far from the overall minima, but a "score" from an integer point near the other local minima might be lesser. The iterative algorithm will not ensure which minima it would reach?

Or can it be mathematically proven that there will only be one such point?

> Or can it be mathematically

madkite @ 15 Apr 2011 05:19 PM

> Or can it be mathematically proven that there will only be one such point?
Teoretically sum of distace is monotonic, so global minumum = local minimum, this is obvious (in integer coordinates there can be 4 point where this minimum is reached). But not in practice where you use float numbers with rounding. Problem have so big limits, that 64-bit arithmetics isn't enought. So even author's solution gives incorrect results.

Hi, Can anybody prove using

ashish_massand @ 17 Apr 2011 08:46 AM

Hi,

Can anybody prove using the grundy numbers or any other way that the numgame has solutions 1,5,9,... It is easy to write a brute force solution to check for losing positions and then write the original code which is exactly what I did. But I couldn't come up with a proof for the result.

If the number is in that

triplem @ 17 Apr 2011 10:35 AM

If the number is in that list, any move will result in a number not in that list since you can't subtract a multiple of 4.

If the number is not in the list, you can subtract either 1, 2, or 3 to leave a number in that list.

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