Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Optimizing the code

Name: Anonymous 2011-12-22 4:42

Any ideas on how I could make this program run faster?
It's supposed to run under 0.5 seconds.
In some cases it does, in others it fails.
[b]The task : [/b]

INPUT:
From the first line of the standard input read one integer (5 <= n <= 100000). Each of the following n lines will have one of the following two formats:

- 1 a - meaning that Mirko said aloud the number a, (0 <= a <= 65535).
- 2 k - meaning that Mirko asks what is the kth smallest number he has said so far. k will always be less or equal to the number of numbers Mirko has said aloud so far.

The total number of different number will not be bigger than 400, but some of the numbers can repeat!

OUTPUT:
To the standard output write one line for each of the 2 k inputs. Representing the kth smallest number at that moment.


Input:
7
1 0
1 1
1 5
2 1
2 3
1 2
2 3

Output:
0
5
2

My solution :

#include <stdio.h>
int main(){
    unsigned short int * a;
        unsigned int n,j,x,y;
    int m=-1;
    scanf("%u",&n);
    a=new unsigned short int[n];
    while (n>0){
        n=n-1;
        scanf("%u %u",&x,&y);
        if (x==1){
            m=m+1;
            j=m;
            a[j]=y;
            while((j>0)&&(a[j]<a[j-1])){
                y=a[j-1];
                a[j-1]=a[j];
                a[j]=y;
                j=j-1;
            }

        }
        else printf("%u\n",a[y-1]);
    }
    delete [] a;
    return 0;
}

Name: Joseph D. Darcy 2011-12-26 5:21

>>29
The only reason that BB-code failed was because the implementation of BB-code on this site lacks proper facilities for comments, I was attempting to comment how I closed the BB-code tags in a FIFO manner, something you apparently are unable to appreciate since you don't understand the value of documentation.

None of those comments are redundant, you don't seem to understand the concepts of code scalability, it's a different, more enterprise kind of scalability. It means that as your program grows, the maintainability does not degrade, if a programmer is new to the program and wants to know what a certain variable is for, he can just look up the generated HTML documentation and search for the identifier. Clearly if you only do toy programs in languages nobody else uses, documentation doesn't matter because you have it all in your head, but I'm so good at programming that I not only keep the code in my own head, but also in the head of others.

That being said, you are not an ENTERPRISE individual. You are probably angry because your horrible uncommented implementation fails in the cases where mine produces a clear error message and exits signaling failure. Well guess what, I am J. Darcy, my programs run faster, scale better, and are overall more well documented than yours, I have two masters degrees but I don't even need them, I get hired and payed just based on the comments in the LaTeX file used to compile my CV. That's right son, I am a lean mean coding machine, I can code around your anti-patterns any day of the week. If you ever want to get a real job someday, try to find the companies I work with, I might just tell them J. Darcy sent you.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List