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: Anonymous 2010-04-13 18:26

>>240
That's impossible!!!

Name: Anonymous 2010-04-13 20:32

>>241
Noooooooooo

Name: Anonymous 2010-04-13 23:45

Where are you, General?

Name: Anonymous 2010-04-14 6:28

You mean, General Protection Failure?

Name: Anonymous 2010-04-14 8:49

>>240
no fucking way

Name: Anonymous 2010-04-14 16:00

>>239,241,244

I'm on to you, Mr. sa\ufeffge.

Name: Anonymous 2010-04-14 16:28

>>244
Failure
Whoa, that's one faulty MPU you got there.

Name: Anonymous 2010-04-14 17:00

Bump for more optimization

Name: Anonymous 2010-04-14 17:20

>>248
Yes, please wear it out. Once this thread is stopped no more optimizing can be performed.

Name: sagebot 2010-04-14 18:20

l

Name: sagebot 2010-04-14 18:20

l

Name: sagebot 2010-04-14 18:20

lg

Name: sagebot 2010-04-14 18:21

lgj

Name: sagebot 2010-04-14 18:21

lgjk

Name: sagebot 2010-04-14 18:21

lgjkt

Name: sagebot 2010-04-14 18:21

lgjktc

Name: sagebot 2010-04-14 18:21

lgjktck

Name: sagebot 2010-04-14 18:21

lgjktcks

Name: sagebot 2010-04-14 18:21

lgjktcksv

Name: sagebot 2010-04-14 18:21

lgjktcksvm

Name: sagebot 2010-04-14 18:21

lgjktcksvmc

Name: sagebot 2010-04-14 18:22

a

Name: sagebot 2010-04-14 18:23

v

Name: sagebot 2010-04-14 18:23

vl

Name: sagebot 2010-04-14 18:23

vck

Name: sagebot 2010-04-14 18:23

oioi

Name: sagebot 2010-04-14 18:23

oijoij

Name: sagebot 2010-04-14 18:23

oioiojojp

Name: sagebot 2010-04-14 18:24

oijoijoijoi

Name: sagebot 2010-04-14 18:24

sage

Name: sagebot 2010-04-14 18:24

iijijijiji

Name: sagebot 2010-04-14 18:24

oijoijoioioijoij

Name: sagebot 2010-04-14 18:24

oijojoijoijoi

Name: sagebot 2010-04-14 18:25

ijojawoiejpw

Name: sagebot 2010-04-14 18:25

ijoijoijoi

Name: sagebot 2010-04-14 18:25

oijoijoijoi

Name: sagebot 2010-04-14 18:25

aoiwjejwoiew

Name: sagebot 2010-04-14 18:25

iojojiojoijoij

Name: sagebot 2010-04-14 18:27

oijoij

Name: sagebot 2010-04-14 18:27

awerw

Newer Posts