One Point Solution

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 6 March 2012

Codeforces round #111 (div2 only)

Posted on 09:06 by Unknown
Oh boy, I hate-respect these div2 only matches, because even though they are supposedly easier than matches with div1, I do terribly at them.

Problem A: Twins
As you want to increase the possibility that the coins you don't take are less than the ones you take. You are interested in taking the coins with the greater value. If you take X coins, take the X largest coins. You can try this for every value of X from 1 to N until you find a value in which the coins you take are enough to be greater than the remaining coins.

The examples were quite weak, during the match I accidentally sorted ascendingly instead of descendingly, yet examples were correct. I guess the existence of pretests can be a good pretext for adding weak example cases.

Problem A: Unlucky Ticket
First, say you are only interested in cases where the first half can match to greater values in the second half. Later you can just reverse the string and re-use your code to catch the second kind of unluckiness.

You have some digits on one side that have to be matched with greater digits on the other side. In fact, this is a typical bipartite matching. That's what I did during the match. I knew that there was likely a solution that didn't need that, but pasting a MaxFLow code you have coded years ago is a lot easier than thinking of something clever.

If you do want a clever algorithm, here is one: Let us count the times each of the digits appears in the right and left places. It is harder to find a digit greater than 8 than it is to find a digit greater than 1. So, you can just iterate from 8 to 0 (If 9 exists in the left side, it is impossible to match). Let us say right is the number of digits in the right side that can be matched to your current value in the left.

For d = 8, you want to match left '8' digits with something in the right. We can now use the right side's 9s, so let us say: right += rightcount[9]. Then we have to check (leftcount[8] <= right). If that is true, then there is a match for all 8 digits, else there is not and we can just return "NO". If there exists, do right -= leftCount[8].

For d = 7, you can now match you left side's 7 digits to the unmatched right digits greater than 8 and also those equal to 8. So you do right += rightcount[8]. Then if (leftcount[7] <= right), and so and so. IF you manage to match all left digits from 8 to 0, then there is a valid match.

Problem C: Find pair
I didn't like that this problem allows duplicates but tries not to mention how they are supposed to work. No single duplicate value in the samples. Thankfully, there is one in the pretests. I actually tried to handle them from the beginning but I made a mistake in my logic on how to handle them...

It is a coincidence, I was just writing the editorial for SRM 535 yesterday (I finished it last night, should come to topcoder's site soon). And the division 2 1000 from that match requires the same trick that is used in this problem. To turn the question of getting the element K of a sorted sequence into a counting problem.

Counting problem? Yeah, let us say you need element #K . Ask yourself: How many elements begin with -10^9, if the number is greater than K, then you can say with certainty that your element will begin with -10^9. Else do K -= (number of elements that begin with -10⁹) proceed to the next number, 999999999, how many begin with that number? Decide to use the number or decrease K again.

After deciding the first element, you can decide the second using the same logic. Note though that if the first element appears multiple times, it affects the number of elements the second element appears.

Of course, you cannot do that right away, the values can be very large or small. Just notice that the number of different values is at most 100000, so you can just use the values from a sorted array (or better, a map, look at my implementation next).
// long long caused the black plague. 
typedef long long int64;
#define long int64

int a[100000];
int n;
long k;

#define var(q, s) typeof(s) q=s
#define for_each(q,s) for(var(q,s.begin()); q!=s.end(); q++)

pair<int,int> solve()
{
map<int, int> numbers;
for (int i=0; i<n; i++) {
numbers[a[i]]++;
}
pair<int,int> res;
long amount = 1;
for_each(q, numbers) {
//how many pairs start with this number?
long s = q->second * (long)n;
if (k <= s) {
amount = q->second;
res.first = q->first;
//can do it
break;
} else {
k -= s;
}
}
//now the second part
for_each(q, numbers) {
//how many pairs end with this number?
long s = amount * q->second;
if (k <= s) {
res.second = q->first;
break;
} else {
k -= s;
}
}
return res;
}



If it wasn't for the duplicates condition, you could do something as easy as sortednumbers[ (K-1)/n][ (K-1)%n ] . If you have any doubts, just remember that this is how decimal numbers work. In order to select a number from 00 to 99... But duplicates would break this logic.

Problem D: The one with the spanning trees

I had a very strange idea about Kruskal algorith: Take a look to Kruskal's MST algorithm. You try edges in ascending order of weight. For each vertex you try, if it connects two different components, use it, else do not use it.

There is a catch, when there are many edges with the same weight that connect different components, you can pick any of them. My theory is that it does not matter which of those edges you pick, at the end of all the steps with the same weight, the same nodes will belong to the same components. This allows you to treat all edges in order of their weight, and deciding for each group of edges with a weight which of those edges are never useful, or always useful or only useful a handful of times. But this part is missing from me yet. I am not sure the theory is true either.

After the match, I learned thanks to codeforces coder sanchit_h, that there are a couple of easy to apply edge properties in regards to a MST: http://en.wikipedia.org/wiki/Minimum_spanning_tree#Cycle_property. Need to improve my theory.
Email ThisBlogThis!Share to XShare to Facebook
Posted in codeforces, explanation | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • TopCoder SRM 557 - finally
    SRM 557 Explanation for division 1 Easy and match recap. Explanations for div2 easy and div2 medium. It feels like it has been ages since t...
  • SRM 589 Editorial
    I have finished writing the editorial for TopCoder SRM 589: http://apps.topcoder.com/wiki/display/tc/SRM+589 . As you most likely noticed. L...
  • SRM 590 recap and editorial
    Another week another Topcoder match. Not a great day. I had a bad flu and still do. Div1 500: The one with Xor Given a list of cards with nu...
  • SRM 546: relief
    I figured I should post something about this SRM. I've been very busy these weeks because the semester is ending and I tried to win a t-...
  • SRM 601 editorial (minus div1 hard)
    It is up: http://apps.topcoder.com/wiki/display/tc/SRM+601 This was a very dry editorial to write. All problems were mathy ad hoc or complex...
  • Member SRM 505: Part 1
    So, let me explain a couple of problems from a Topcoder Member SRM that I wrote and never got an editorial. BTW, it was the last member SRM....
  • SRM 533: Div1 500 MagicBoard explanation
    Finally solved it. It is a nice problem that is worth explaining in a post. You have a grid/board of at most 50x50 cells. Some cells contain...
  • SRM 554 div1 hard: TheBrickTowerHardDivOne
    Link to problem statement We got infinitely many bricks of dimensions 1x1x1 and C different colors. Count the number of towers of size 2x2...
  • SRM 526: The killing wait for results
    While I wait for results, here is my perspective on this algorithm contest. It began with issues, it had to be postponed 15 minutes. TC has ...
  • TopCoder SRM 570: CentaurCompany and CentaurCompanyDiv2
    Another 570 editorial update: http://apps.topcoder.com/wiki/display/tc/SRM+570 . This time for the division 2 hard and division 1 medium. My...

Categories

  • acm
  • algorithm
  • answers
  • arenaplugin
  • badday
  • behindthescenes
  • bugs
  • c++
  • censorship
  • codechef
  • codeforces
  • contests
  • crocchamp
  • editorial
  • editorial.srm
  • embarrassing
  • explanation
  • gcj2013
  • gmp
  • goodday
  • google
  • googlecodejam
  • greed
  • groklaw
  • health
  • html
  • httpseverywhere
  • implementation
  • ipsc
  • ispc
  • java
  • kawigiedit
  • kindagoodday
  • lamebook
  • languages
  • lego
  • listedlinks
  • marathon
  • nasa
  • offtopic
  • ouch
  • postmortem
  • postportem
  • practical
  • probably_not_a_good_tip
  • problemsetting
  • programming
  • python
  • quora
  • rant
  • recap
  • slightlygoodday
  • snippet
  • srm
  • stl
  • strategy
  • swerc
  • tco
  • tco12
  • tco13
  • tco2012
  • tco2013
  • ternarysearch
  • topcoder
  • tricks
  • ubuntu
  • uva
  • vjass
  • vkcup
  • wc3
  • zinc

Blog Archive

  • ►  2014 (1)
    • ►  January (1)
  • ►  2013 (141)
    • ►  December (14)
    • ►  November (8)
    • ►  October (13)
    • ►  September (11)
    • ►  August (14)
    • ►  July (15)
    • ►  June (13)
    • ►  May (13)
    • ►  April (12)
    • ►  March (11)
    • ►  February (11)
    • ►  January (6)
  • ▼  2012 (94)
    • ►  December (5)
    • ►  October (6)
    • ►  September (8)
    • ►  August (6)
    • ►  July (3)
    • ►  June (5)
    • ►  May (8)
    • ►  April (10)
    • ▼  March (20)
      • Topcoder Open 2012 round 1A
      • Codeforces round #114. 167C: Wizards and numbers
      • Official TCO 2012 blogger
      • Codeforces round #114 (div1)
      • 5 reasons editorial votes in Topcoder are useless
      • Codeforces VK Cup round 2: (ouch)
      • Codeforces round #113 div2 (unofficial)
      • Writing SRM 538
      • SRM 537: oh my
      • Codeforces round #112
      • Language features: Rants and wishes
      • Google code jam registration - Just note something...
      • Codeforces: VK Cup round 1: Problem C: Abracadabra
      • Codeforces: VK Cup round 1 (update 1)
      • SRM 536: heh
      • SRM 535 editorial: The making of
      • Codeforces round #111 (div2 only)
      • SRM 535 : lame
      • A note about SRM 537 and 540 money prizes
      • The March of Destruction begins!
    • ►  February (16)
    • ►  January (7)
  • ►  2011 (51)
    • ►  December (7)
    • ►  November (12)
    • ►  October (5)
    • ►  September (1)
    • ►  August (3)
    • ►  July (4)
    • ►  June (3)
    • ►  May (7)
    • ►  April (3)
    • ►  March (2)
    • ►  February (1)
    • ►  January (3)
  • ►  2010 (9)
    • ►  December (4)
    • ►  October (1)
    • ►  June (1)
    • ►  May (1)
    • ►  January (2)
  • ►  2009 (1)
    • ►  December (1)
Powered by Blogger.

About Me

Unknown
View my complete profile