One Point Solution

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

Wednesday, 11 April 2012

Topcoder SRM 540

Posted on 06:03 by Unknown

Fun problem set. But I spent most of the time solving a problem that was much easier than 550. Instead of solving the real 550.

div1 250: The one with the intervals

All right all right. Let us count the number of correct arrays (a1, a2,... an+1) such that ai opi ai+1 = bi for all i Such that opi are operators (+ or -) and all ai are positive.

Let us say that a1 = x. We have x > 0 as the first condition. If op1 was -, then we would have to solve the equation: b1 = a2 - a1 = a2 - x. Or (a2 = b1 + x). We got a new condition: (b1 + x > 0). Likewise, if the first operator was positive we would have: b1 = a2 + x ; a2 = b1 - x. So the new condition will become b1 - x > 0.

Let us define for each i : ai = s * x + y. Such that s is 1 or -1 (the sign). For each i, you can find the values of s and y by using the values from (i-1) by using the equations. Then you have to use the condition: s * x + y > 0. Depending on the sign of x, this will become either a lower bound for x or an upper bound for x. At the end, you will have plenty of upper and lower bounds (if you do not have any upper bound, there are infinite results) (x must be positive, so the lower bound is at least 1). Pick the minimum upper bound and the maximum lower bound, then use a single subtraction to count the number of ways x can be set.

Implementation can be tricky. I would post my beautiful code I made during the match, but I am in a different computer and TopCoder won't let me CNP my own code.

So here it is:

#include <valarray>
#include <queue>
#include <sstream>
#include <iostream>
#include <set>
#include <map>
#include <cassert>

using namespace std;

typedef long long int64;
#define long int64


struct ImportantSequence
{
int getCount(vector <int> B, string operators)
{
int n = B.size();
int s = 1;
long y = 0;
const long INF = numeric_limits<long>::max();
long lowBound = 1;
long upperBound = INF;
for (int i=0; i<=n; i++) {
// value of the last number is
// x*s + y > 0
// x*s > -y
if (s == -1) {
// -x > -y
// x < y
upperBound = std::min(upperBound, y);
} else {
// x > -y
lowBound = std::max(lowBound, -y + 1);
}

if (i==n) {
break;
}
if ( operators[i] == '-') {
//new number is:
// B[i] = (x*s + y) - z
// z = (x*s + y - B[i])
y -= B[i];
} else {
//new number is:
// B[i] = (x*s + y) + z
// z = B[i] - (x*s + y)
// z = B[i] - x*s - y
s *= -1;
y = -y + B[i];
}


}
if (upperBound == INF) {
return -1;
}
if (upperBound <= lowBound) {
return 0;
}
return upperBound - lowBound;

}
};
#undef long

div1 550: The one with the colors

I spent most of the match thinking that colors are independent. As such you can find the probability that each color will be ugly and then combine the 3 probabilities. I spent most of the match baffled as to why I failed the last test cases. From my analysis, I was certain the colors were independent, but examples suggested that was not the case. But I could not find a reason for this... Until, some few minutes before the end, I noticed that while all differences must be at most d2, only one of the differences between color components must be at least d1. Thus the colors are not independent. Yay.
Email ThisBlogThis!Share to XShare to Facebook
Posted in explanation, goodday, srm, 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 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)
      • 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