Problem A – Standing Ovation – CodeJam Qualification Round 2015

Problem Link: Standing Ovation

Contest: Google CodeJam Qualification Round 2015

Category: Ad-hoc

My Solution:

Time complexity: O(n) for every test cases

#include
using namespace std;
int main()
{
    freopen("A-large-practice.in", "r", stdin);
    freopen("A-large-practice.out", "w", stdout);
    int t, n, i, ans, sum;
    /* sum - stores no of people standing
       ans - no of friends to be called */ 
    string s;
    cin >> t;
    for(int tc = 1; tc<=t; ++tc)
    {
        cin >> n >> s;
        ans = 0;
        sum = s[0] - '0';
        for(i=1; i<=n; ++i)
        {
            if(sum < i)
            {
                ans += i - sum;
                sum += i - sum;
            }
            sum += s[i] - '0';
        }
        cout << "Case #" << tc << ": " << ans << endl;
    }
    return 0;
}

Feel free to ask. Just a comment away!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s