One Point Solution

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

Friday, 16 March 2012

Codeforces round #112

Posted on 10:13 by Unknown
It is 17 minutes before the end of the contest, I am giving up on E and D.

Problem A link
There are at most 200 points. You can actually do a O(n*n) search, for each point, iterate through all the other points and if the other point is a left,down,up or right neighbor, set a switch so you know that such a kind of neighbor was found.

int n; 
int x[1000];
int y[1000];


int solve()
{
int c = 0;
for (int i=0; i<n; i++) {
bool up = false;
bool down = false;
bool right = false;
bool left = false;

for (int j=0; j<n; j++) {
right |= ( y[i]==y[j] && x[i]<x[j] );
left |= ( y[i]==y[j] && x[i]>x[j] );
down |= ( x[i]==x[j] && y[i]<y[j] );
up |= ( x[i]==x[j] && y[i]>y[j] );
}
if ( up && down && right && left ) {
c++;
}

}
return c;
}


Problem B link
Imagine the largest case, n=10^9, k=10. Then if you use something as large as v=10000000000, it will finish in one move. So that is a value of v that can work as an upper bound. It is a large bound, but what you can do is do a binary search. If it is not possible for a v, it will not be possible for a smaller v. And if it is possible for a large v, it should be possible for any larger v.

Testing each value in the binary search takes logarithmic time in base k. Thankfully, problemsetter was not evil to allow k=1 :)

bool possible(long n, int k, long v) 
{
long done = 0;
long div = 1;
while (n > done) {
long todo = v / div;
if (todo == 0) {
break;
}
done += todo;
div *= k;
}
return (done >= n);
}

long solve(int n, int k)
{
long hi = 10000000000ll;
long lo = 0;

while (lo + 1 < hi ) {
long ha = (hi + lo) / 2;

if (possible( n, k, ha ) ) {
hi = ha;
} else {
lo = ha;
}
}
return hi;

}



Problem C link
Ok, with such large constraints we want a linear algorithm.

What about k=2, and a string like "00101001" ? Consider the definition of a important fragment: A fragment that begins and ends with 1 and contains exactly k 1s. In the example, there are two important fragments: --101--- and ----1001 Each of these two places has a number of adjacent 0s to the right and left sides (possibly zero). Now the key is to notice that the number of substrings that contain each of these important fragments is equal to (number of left zeros + 1) * (number of right zeros + 1). In other words, for fragment 101, surrounded by 00 to the left and 00 to the right, there are 9 substrings: 00101, 001010, 0010100, 0101, 01010, 010100, 101, 1010 and 10100. For fragment "1001", there are 2: "01001", "1001".

The problem becomes to find these fragments and count the side zeros in O(n). It is not actually so difficult. Let onepos be an array that contains the positions of 1 characters in the string. Then you can find that each fragment that begins at onepos[i] will finish at some onepos[i + k - 1]. Then you can use onepos[i-1] to count the zeros in the left side (consider the corner case when i=0, though) and a similar idea for the right zeros.

Catch: k can be 0. Then you have to count the number of substrings containing only 0 characters. Too bad I didn't see this during the match and had to resubmit.

To deal with this special case, use the onepos array once again, but this time to find all the maximal fragments that contain only zeros. From the sizes of each of these fragments, you can find the number of substrings inside it.

int k; 
string s;

long solve()
{
vector<long> onepos;
for (int i=0; i<s.size(); i++) {
if (s[i] == '1') {
onepos.push_back(i);
}
}
long res = 0;
if (k == 0) {
for (int i=0; i <= onepos.size(); i++) {
long a = ( (i==onepos.size()) ? s.size(): onepos[i] );
long b = ( (i==0) ? -1: onepos[i-1] );
long leftZeros = a - b - 1;
// number of substrings inside this fragment that contains leftZeros zeros
res += ( leftZeros * (leftZeros + 1) ) / 2;
}
} else {
for (int i=0; i + k - 1 < onepos.size(); i++) {
int j = i + k - 1;
long leftZeros = onepos[i] - ( (i==0) ? -1 : onepos[i-1] ) - 1;
long rightZeros = ( (j==onepos.size()-1) ? s.size() : onepos[j+1] ) - onepos[j] - 1;
// number of ways to add zeros to the important fragment.
res += ( leftZeros + 1) * (rightZeros + 1);
}
}


return res;
}


D and E
I avoided D, it seemed that if I found a solution it would take long to code it. Note that the input is a tree (connected, with n-1 vertices) and the single vertex with more than 2 edges can easily be the root you pick. There are never more than 1 path connecting two vertices, so you are only interested in confirming if there is a path or not.

E seemed more interesting so I focused on it. With no success yet. Tried stuff with tries, but everything seems too slow. Might post an update later.
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 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 ...
  • 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 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...
  • 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....
  • ListedLinks 2012-02-10
    Saturday Morning Breakfast Cereal comics: Grace Hopper's ghost That Oracle engineer blog post Oracle would really not like anyone to se...
  • Codeforces "Good bye 2013" round
    So it was a special round for coders of both divisions, problems ranged from the super easy problem A to the super difficult problems E,F,G....

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