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-03-02 4:34

You fucking broke /prog/ you dipship how the fuck am i meant to read the rest of this page when it's all fucking cursive

Name: Anonymous 2010-03-02 5:06

>>41
We should replace you with people who appreciate nice flowing cursive shapes.

Name: Anonymous 2010-03-02 5:41

>>41,42

We have replaced you both with posters who know the difference between cursive and italics.

Name: Anonymous 2010-03-02 7:42

>>41,42
You're delivering concepts that can be found in various places. I'm afraid that I have no choice but to replace you with artists.

Name: Anonymous 2010-03-02 10:13

And then the /prog/rider was an Italian.

Name: Anonymous 2010-03-02 10:20

Only the fat American would not know solution to this problem.
In mother Russia, who sings such easy problem as lullaby to little baby is bad mother.

Name: Anonymous 2010-03-02 16:49

RUSSIA MY ANUS

Name: Anonymous 2010-03-02 17:41

AMERICA MY ANUS

Name: Anonymous 2010-03-02 17:45

i swear you faggots just want to bump this thread to italicize the board

Name: Anonymous 2010-03-02 18:07

BAMPU

Name: Anonymous 2010-03-02 18:17

PANTSU

Name: Anonymous 2010-03-02 18:42

BUMP MY ANUS

Name: Anonymous 2010-03-02 19:25

SAGE MY BANUS

Name: Anonymous 2010-03-02 19:42

Colpo

Name: Anonymous 2010-03-02 20:55

no
no no no

Name: Anonymous 2010-03-02 21:08

yes
yes yes yes

Name: Anonymous 2010-03-02 21:22

Best tread of the year.

Name: Anonymous 2010-03-02 22:44

can't bampu pantsu cap'n

Name: Anonymous 2010-03-02 23:02

          ∧_∧   / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
          ( ´∀`) < Ho appena cagato sul tuo tavolino da caffé
        /    |    \________
       /       .|     
       / "⌒ヽ |.イ |
   __ |   .ノ | || |__
  .    ノく__つ∪∪   \
   _((_________\
    ̄ ̄ヽつ ̄ ̄ ̄ ̄ ̄ ̄ | | ̄
   ___________| |
    ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| |

Name: Anonymous 2010-03-03 2:44

>>40
You can get O(1) insertion with something like >>15. And by that, I mean O(1) with a really small constant.

Name: Anonymous 2010-03-03 3:04

バンプ for italics.

Name: Anonymous 2010-03-03 3:44


Name: Anonymous 2010-03-03 3:59

>>62
­

Name: Anonymous 2010-03-03 5:09

PAMPU BANTSU

Name: Anonymous 2010-03-03 8:03

Messo in cima mutandine

Name: Anonymous 2010-03-03 8:27

>>65
Intendi "Sbattaru Mutandinu" ?

Name: Sage 2010-03-03 8:42

          ∧_∧   / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
          ( ´∀`) < Sage sage
        /    |    \________
       /       .|     
       / "⌒ヽ |.イ |
   __ |   .ノ | || |__
  .    ノく__つ∪∪   \
   _((_________\
    ̄ ̄ヽつ ̄ ̄ ̄ ̄ ̄ ̄ | | ̄
   ___________| |
    ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| |

       Sage the Cat

Name: Anonymous 2010-03-03 8:49

FUCK THIS THREAD AND EVERYTHING IT STANDS FOR. GET AWAY FROM THE FRONT PAGE!

Name: Anonymous 2010-03-03 11:49

>>68
Why do you hate Italians so much, you raging xenophobe?

Name: Anonymous 2010-03-03 17:06

bampu

Name: Anonymous 2010-03-03 18:37

ciao! non mi piace il sage..  lo amo il bump!

Name: Anonymous 2010-03-03 21:21

What the fuck kind of bastard C/C++ are you programming in? If you're going to use new/delete, use the fucktarded streams cin/cout. If you use printf/scanf, use malloc/free. kthxbi

Name: Anonymous 2010-03-03 22:06

pantsu

Name: Anonymous 2010-03-04 10:07

>>73
This point has already been made by various users.

Name: Anonymous 2010-03-04 19:24

>>74
It bears repeating.

Name: Anonymous 2010-03-04 20:41

you are now reading at a 55 degree angle

Name: Anonymous 2010-03-04 20:43

Requesting sagetank

Name: Anonymous 2010-03-04 20:49

Requesting Cool Mo-Dean.

Name: Anonymous 2010-03-04 22:50

Requesting Italians

Name: Anonymous 2010-03-05 1:43

[i][/i]

Newer Posts