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:32

fdgrgtryry

Name: sagebot 2010-04-14 18:32

liuliukthyref

Name: sagebot 2010-04-14 18:32

pokjhg

Name: sagebot 2010-04-14 18:32

;lkjh

Name: sagebot 2010-04-14 18:32

jhgfd

Name: sagebot 2010-04-14 18:32

aweewrawer

Name: sagebot 2010-04-14 18:32

rewgaewfrewfr

Name: sagebot 2010-04-14 18:32

aewrewrewarwerwerw

Name: sagebot 2010-04-14 18:33

aewrwewerewwefewf

Name: sagebot 2010-04-14 18:33

ewagewgewfawfsdsf

Name: sagebot 2010-04-14 18:33

ddsscdssddscsd

Name: sagebot 2010-04-14 18:33

kkukukukiuk

Name: sagebot 2010-04-14 18:33

lkjhfdsa

Name: sagebot 2010-04-14 18:33

edwdwdwdwdwdw

Name: sagebot 2010-04-14 18:33

fdgregregregreg

Name: sagebot 2010-04-14 18:33

;oi;oi;oiloil

Name: sagebot 2010-04-14 18:33

czxccacsacsacsac

Name: sagebot 2010-04-14 18:34

gredfdsgdgdgffsfdsf

Name: sagebot 2010-04-14 18:34

fgtju

Name: sagebot 2010-04-14 18:34

stdyfugihjk

Name: sagebot 2010-04-14 18:34

plplplokokpokpok

Name: sagebot 2010-04-14 18:34

jujijiji

Name: sagebot 2010-04-14 18:34

aesrdtfyguhijo

Name: sagebot 2010-04-14 18:34

aesrdtfyguhijodtrffytfyugyuyu

Name: sagebot 2010-04-14 18:35

plplkokpjoihiuhiujnmlkmlkmlkm

Name: sagebot 2010-04-14 18:35

dtfyuuijio

Name: sagebot 2010-04-14 18:35

frfytgyugyuh

Name: sagebot 2010-04-14 18:35

tfuygyugiuhiuj

Name: sagebot 2010-04-14 18:35

ffguijoikjpok

Name: sagebot 2010-04-14 18:35

jhoihyufytgfyu

Name: sagebot 2010-04-14 18:35

uhoijoijoi

Name: sagebot 2010-04-14 18:35

ryughujik

Name: sagebot 2010-04-14 18:35

rdtguihjoik

Name: sagebot 2010-04-14 18:35

okokoijigtftrdgiojpolk

Name: sagebot 2010-04-14 18:36

xfddgfhjyuh

Name: sagebot 2010-04-14 18:36

rdytfgyuhiojpokjhiu

Name: sagebot 2010-04-14 18:36

fytfyughjh

Name: sagebot 2010-04-14 18:36

edrftguj

Name: sagebot 2010-04-14 18:36

drfyujik

Name: sagebot 2010-04-14 18:36

njnjnjnk

Newer Posts