HDU : 3282 (Running Median)



// http://acm.hdu.edu.cn/showproblem.php?pid=3282

Notes : 
1. Output the middle indexed value of the current sorted list
Use insertion sort 
whenever u get a value, positioned it in the right place 

2. Be careful about output format 
suppose u need to print 1 to 12
1<sp>2<sp>3<sp>4<sp>5<sp>6<sp>7<sp>8<sp>9<sp>10\n
11<sp>12\n

here, <sp> = space
\n = newline

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define N 1000000
using namespace std;

vector <int> a;

void insertionSort (int n)
{
    vector <int> ::iterator it;

    for ( it = a.begin (); it < a.end (); it++ ) {
        if ( n < *it ) {
            a.insert (it, 1, n);
            return;
        }
    }

    a.push_back (n);
}

int main ()
{
    //freopen ("out.txt", "w", stdout);
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        int dataset, n;
        scanf ("%d %d", &dataset, &n);

        printf ("%d %d\n", dataset, n / 2 + 1);

        a.clear ();
        int input;
        int nums = 0;

        for ( int i = 0; i < n; i++ ) {
            scanf ("%d", &input);
            insertionSort (input);
            if ( i % 2 == 0 ) {
                nums %= 11;
                if ( nums == 10 ) {
                    nums = 0;
                    printf ("\n");
                }

                if ( nums != 0 ) printf (" ");
                nums++;
                printf ("%d", a [i / 2]);
            }
        }
        printf ("\n");

    }

	return 0;
}

// @END_OF_SOURCE_CODE

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