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

trollface.jpg

Name: Anonymous 2010-04-15 18:06

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: Anonymous 2010-04-15 18:11

It makes me wet knowing you can't figure out how to do this without copying the original.

Name: Over 3 Thread 2010-04-15 18:17

This thread has over 3 replies.
You can't reply anymore.

Name: Anonymous 2010-04-15 18:37

>>3
Looks like you made an 'off by one' mistake somewhere in your spam code.

Name: Anonymous 2010-04-15 18:39

>>4
Take a long hard look at an Over 1000 Thread. Then go back to the imageboards, please.

Name: Anonymous 2010-04-15 19:22

No.

Name: Sir Sagealot 2010-04-15 21:20

Name: Sir Sagealot 2010-04-15 21:20

Name: Sir Sagealot 2010-04-15 21:20

Name: Sir Sagealot 2010-04-15 21:20

Name: Sir Sagealot 2010-04-15 21:20

Name: Sir Sagealot 2010-04-15 21:20

Name: Sir Sagealot 2010-04-15 21:20

Name: Sir Sagealot 2010-04-15 21:20

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:21

Name: Sir Sagealot 2010-04-15 21:22

Name: Sir Sagealot 2010-04-15 21:22

Name: Sir Sagealot 2010-04-15 21:22

Name: Sir Sagealot 2010-04-15 21:22

Name: Sir Sagealot 2010-04-15 21:22

Name: Sir Sagealot 2010-04-15 21:22

Name: Sir Sagealot 2010-04-15 21:22

Name: Sir Sagealot 2010-04-15 21:22

Name: Sir Sagealot 2010-04-15 21:23

Name: Sir Sagealot 2010-04-15 21:23

Name: Sir Sagealot 2010-04-15 21:23

Name: Sir Sagealot 2010-04-15 21:23

Name: Sir Sagealot 2010-04-15 21:23

Name: Sir Sagealot 2010-04-15 21:23

Name: Sir Sagealot 2010-04-15 21:23

Name: Sir Sagealot 2010-04-15 21:23

Name: Sir Sagealot 2010-04-15 21:23


Newer Posts