One Point Solution

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

Friday, 4 October 2013

Codeforces #204 (div 1) Recap

Posted on 10:52 by Unknown

So, I could only solve 2 problems and I am not entirely sure of them.

Problem A - The one with rounding

Link to problem statement

The first thing is to notice that all numbers are guaranteed to have exactly three decimal points. From now on. we can just care of the decimal part (x[i] will be number from 0 to 999), and minimize the absolute value of the difference.

  • If x[i] is 0 (So it was a number that ends with .000, like 12.000), then no matter what we do, it will contribute 0 to the difference.
  • Else if we choose to round down the number, the difference increases by (-x[i]) / 1000.0 .
  • If we choose to round up, then the difference changes by (1000 - x[i]) / 1000.0.

n numbers have to be rounded up. If i of those numbers are zero, then the total difference will be: (1000 * (n - i) - sum(x[i]) ) / 1000.0. In other words, the only thing that determines the difference is the number of zeros that we round up. We can just brute force for this number and pick the best difference we find


// input:
string a[4000];
int n2; // n in the statement

// other:
int x[4000];

double solve()
{
int n = n2 * 2;
for (int i=0; i<n; i++) {
istringstream st( a[i] );
int z=-1, f=-1; char ch;
st >> z >> ch >> f;
x[i] = f; // only the modulo , the decimal digits matters
}
int res = 5000000;
int z = count(x, x + n, 0); // number of zeros
int sum = accumulate(x, x+n, 0); // total sum

// how many zeros will be ceil?
for (int i = 0; i <= z && i <= n2; i++) {
res = min(res, abs( (n2 - i) * 1000 - sum ) );
}
// divide result by 1000
return res / 1000.0;
}

During the match I first tried a badly-thought greedy algorithm. I even tested it comparing it with a bruteforce algorithm before submitting, but it still failed, apparently the bug with the lame greedy needed very specific input. But while coding the greedy algorithm I noticed the -x[i] and 1000-x[i], which helped me solve the problem correctly (assuming it passes)

Problem B - The one with permutations

Problem statement

The first thing I noticed was that result for the second example was a integer, that is too much of a coincidence. Somehow I decided that maybe the result is equal to (number of steps needed to sort the permutation without the random stuff) * 2 - 1. It turns out the estimation was wrong, but the idea was almost correct (I think).

Anyway, assume you have to do X steps in order to solve it correctly. After you make a step, your friend will throw a coin, and there is a 50% probability he will help you and a 50% probability he will make things worse. It appears to me that he helping you will always decrease the number of required steps by 1, and he not helping you will always increase the number of required steps by 1 (This in addition to the step you performed). The result is the following recurrence, 'f(x)` where x is the number of required steps.:

`f(0) = 0`
`f(1) = 1`
`f(x) = 2 + f(x-2)/2 + f(x) / 2`
`f(x) = 4 + f(x-2)`

We can just fill the recurrence iteratively.


int n;
int p[3000];


double solve()
{
//a) cost to sort
int res = 0;
for (int i = n - 1; i >= 0; i--) {
pair<int,int> mx = make_pair(-1, -1);
for (int j = 0; j <= i; j++) {
mx = std::max(mx, make_pair(p[j], j));
}
res += i - mx.second;
for (int j = mx.second; j < i; j++) {
p[j] = p[j+1];
}
p[n-1] = mx.first;
}
// b) Recurrence:
double dp[3];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= res; i++) {
dp[i % 3] = 4 + dp[ (i + 1) % 3 ];
cout << i << " ; " << dp[i % 3] << endl;
}

return dp[res % 3];
}

Problem C : The one with parenthesis and cycles

Statement

I didn't have much time to code it (less than 10 minutes), but I think I found a solution.

It appears to me that the best parenthesis sequence has to be cyclic because the cost function is cyclic. When n is even, it should be of length n, when n is odd, it should be of length 2n. Since m is guaranteed to be even, we can half it.

So we just need to generate the cyclic parenthesis sequence. The cost of the sequence is easy to find because the costs are cyclic. Constraints are large in the case n is odd. But I am pretty sure we can use a meet in the middle approach so that the process is `O(2^n)`.

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)
      • Coding habits
      • SRM 595
      • Editorial for SRM 594 Div1 Hard: FoxAndAvatar
      • SRM 594 editorial (Part 1)
      • SRM 594: More slowness
      • So, TCO 2013 video contest
      • WolfDelaymasterHard editorial
      • SRM 593 Editorial (Part 1)
      • c++ and python topcoder generic testers
      • Together we can send vexorian to the TCO 2013
      • SRM 593: Meh
      • More answers to Quora questions.
      • Codeforces #204 (div 1) Recap
    • ►  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)
    • ►  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