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

Optimizing the code

Name: Anonymous 2010-03-01 8:19

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

[b]My solution : [/b]

#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: sagebot 2010-04-14 18:36

kokpokpokpojiuh

Name: sagebot 2010-04-14 18:36

gfgiuhoij

Name: sagebot 2010-04-14 18:36

tfyugiuhiu

Name: sagebot 2010-04-14 18:37

uhiuh

Name: sagebot 2010-04-14 18:37

uiuhiuhiuhl

Name: sagebot 2010-04-14 18:37

rdftgyhujik

Name: sagebot 2010-04-14 18:37

yuhoij

Name: sagebot 2010-04-14 18:37

redyuio

Name: sagebot 2010-04-14 18:37

dfghuijk

Name: sagebot 2010-04-14 18:37

giuhjoijoihftrd

Name: sagebot 2010-04-14 18:37

guyhiuhjoijd

Name: sagebot 2010-04-14 18:37

guygyuhiu

Name: sagebot 2010-04-14 18:37

dytfgyuh

Name: sagebot 2010-04-14 18:38

sedftyuhuj

Name: sagebot 2010-04-14 18:38

dfghjn

Name: sagebot 2010-04-14 18:38

edtrfgyuhjiok

Name: sagebot 2010-04-14 18:38

hfytgiuhj

Name: sagebot 2010-04-14 18:38

sderfgtyhu

Name: sagebot 2010-04-14 18:38

ewretyui

Name: sagebot 2010-04-14 18:38

sxdfgyhuj

Name: sagebot 2010-04-14 18:38

fyuhu

Name: sagebot 2010-04-14 18:38

gfytgyu

Name: sagebot 2010-04-14 18:38

tfyugu

Name: sagebot 2010-04-14 18:38

hiujoi

Name: sagebot 2010-04-14 18:39

uhiuhiuh

Name: sagebot 2010-04-14 18:39

joijoijoijp

Name: sagebot 2010-04-14 18:39

dtrfyuiuh

Name: sagebot 2010-04-14 18:39

zxsxdcfvhjbjk

Name: sagebot 2010-04-14 18:39

cchgvgg

Name: sagebot 2010-04-14 18:39

jojiokjoi

Name: sagebot 2010-04-14 18:39

mkmjjhnhvcgfchgvhj

Name: sagebot 2010-04-14 18:39

juh

Name: sagebot 2010-04-14 18:39

ijoijoi

Name: sagebot 2010-04-14 18:39

xcxvcxfdx

Name: sagebot 2010-04-14 18:40

ijikokpo

Name: sagebot 2010-04-14 18:40

oijaoiewjroiwer

Name: sagebot 2010-04-14 18:40

iuhiuh

Name: sagebot 2010-04-14 18:40

sredytfyu

Name: sagebot 2010-04-14 18:40

tdytvhhjnbkjnj

Name: sagebot 2010-04-14 18:40

cchgvhjnjknlk

Newer Posts