One Point Solution

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

Saturday, 7 April 2012

Topcoder open 2012: Round 1B

Posted on 15:58 by Unknown

I have posted my TCO 2012 post about round 1B: http://community.topcoder.com/tco12/algorithm-track-round-1b/

Bonus comments:

  • I think the best way to describe the problem set is intense. I really liked it. I solved all problems, but it made me feel like I had worked hard to get it rather than making me feel it was an easy problem set
  • The guessing contest seems to have been a popular idea. But it is going to be hard to compute all those results after each round. Today it really took me much longer than needed.
  • The recurrence for 1000 is as follows f(p, subset). p is the number of left-most back seats that have already been matched. subset contains all the front seats that are already matched (each to one of those p seats). Then, for position #p, you just have to pick a front seat not in the subset that can be moved to #p, calculate the cost to move it and recurse to f(p+1, subset union (picked seat)). You can then optimize this recurrence by noticing that #p is always equal to the length of the subset. And of course, the usual trick to use bitmasks to represent subsets.
  • Cute code for the problems:
  • #define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++) 
    struct FoxAndKgram
    {
    int maxK(vector <int> len)
    {
    map<int,int> cnt;
    // Number of pencils of each length:
    for_each(x, len) {
    cnt[*x]++;
    }
    for (int k = 50; k >= 1; k--) {
    int allowed = cnt[k]; //can use all pencils of size k

    // for each pair (i,j) such that (j > i) and i + j = k - 1:
    for (int i=1; (i < k) && (k - 1 - i > i); i++) {
    allowed += min(cnt[i], cnt[k-1-i]);
    }
    // if odd, we can use two k/2 pencils in each row
    if (k % 2 == 1) {
    allowed += cnt[k/2]/2;
    }
    // k is valid if :
    if (allowed >= k) {
    return k;
    }
    }
    return 0;
    }
    };
    struct FoxAndDoraemon 
    {
    int minTime(vector <int> workCost, int splitCost)
    {
    int n = workCost.size();
    sort(workCost.rbegin(), workCost.rend());
    // need to split n-1 times.
    // tree shape? Third example needs 2nd tree shape
    // o But in other times, it is possible we need
    // / \ The first one.
    // o o
    // / \ / \ ..
    // o o o o
    // vs.
    //
    // o
    // / \ ..
    // o o
    // / \ ..
    // o o ? ? ?
    // / \ ..
    // o o
    //
    // max time is 50*3600 + 50*3600
    // linear search?

    // Binary search works too, but is not needed.
    for (int t=1; t<=50*3600*2; t++) {
    //Is it possible to do it in time = t?
    // If at one time, we are forced to do a given homework instead
    // of splitting, do homework.
    int trees = 1;
    int done = 0;
    int current = 0;
    while ( (done < n) && (trees > 0) && (current < t) ) {
    while ( (done < n) && (current + workCost[done] + splitCost > t) ) {
    if ( (current + workCost[done] > t) || (trees <= 0) ) {
    // impossible time. Dijkstra... Forgive me!
    goto next;
    }
    trees -= 1;
    done++;
    }
    // split current trees
    trees *= 2;
    current += splitCost;
    if (trees >= n - done) {
    break;
    }
    }
    if (trees >= n - done) {
    return t;
    }
    next:;
    }
    return -1;


    }
    };
    const int INF = 16*16*2; 
    struct FoxAndPhotography
    {
    int dp[1<<16];
    vector <int> heightsFront;
    vector <int> heightsBack;
    int n;

    int rec(int mask)
    {
    int & res = dp[mask];
    if (res == -1) {
    int p = __builtin_popcount(mask);
    if (p == n) {
    res = 0;
    } else {
    res = INF;
    // decide which front passager gets matched to
    // back passenger #p
    int behind = 0;
    for (int i=0; i<n; i++) {
    if (!( (1<<i)&mask )) {
    if (heightsFront[i] < heightsBack[p]) {
    // There are {behind} people to the left of
    // person i. That is the cost to take her to
    // position #p.
    res = std::min(res, behind + rec(mask|(1<<i)) );
    }
    behind++;
    }
    }
    }
    }
    return res;
    }

    int getMinimumSwaps(vector <int> heightsFront, vector <int> heightsBack)
    {
    // it is only necessary to swap the people in one of the rows.
    // swapping two persons in the front is equivalent to swapping the same
    // two positions in the back
    memset(dp, -1, sizeof(dp));
    this->heightsFront = heightsFront;
    this->heightsBack = heightsBack;
    n = heightsFront.size();
    int x = rec(0);
    return ( (x >= INF) ? (-1): x);
    }
    };
Email ThisBlogThis!Share to XShare to Facebook
Posted in explanation, tco, tco2012, topcoder | 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)
      • Google codejam round 1A
      • Evil SRM 541 is evil
      • To solve Google Codejam: Hall of Mirrors II
      • To solve Google Codejam: Hall of Mirrors I
      • To solve Google Codejam: Hall of Mirrors III
      • Google Code Jam 2012: Qualification round
      • Topcoder SRM 540
      • Topcoder open 2012: Round 1B
      • Codeforces Croc Champ 2012 - Round 1
      • Codeforces April Fools Day Contest
    • ►  March (20)
    • ►  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