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-06 21:46

THIS THREAD IS ONE POST CLOSER TO THREADSTOP.

Name: Anonymous 2010-03-06 22:59

THIS POST IS ONE POST CLOSER TO STOPPING THIS THREAD.

Name: Anonymous 2010-03-07 3:20

>>120
ok

Name: Anonymous 2010-03-07 3:55

>>120
Sure will.

Name: ‮suomynonA 2010-03-07 3:56



Name: Anonymous 2010-03-07 10:17

ARE WE THERE YET?

Name: Anonymous 2010-03-07 12:08

>>126
Well, are you?

Name: Anonymous 2010-03-07 12:12

Someone should sagebomb this thread or ask some World4ch staff to fix this damn Shiichan bug already. I would have even posted in this thread (saging) if not for the idiocy of other posters to bump it.

Name: ‮suomynonA 2010-03-07 12:21

>>128
Why so serious?

Name: Anonymous 2010-03-07 12:22

>>129
Why so 0/10

Name: Anonymous 2010-03-07 13:00

>>129-130
Both of you, back to the imageboards, please

Name: Anonymous 2010-03-07 13:31

>>128
Why are you so mad about the Italians? I think it looks nice.

Name: Anonymous 2010-03-07 13:32

>>132
It was cute the first hundred bumps, now it's gotten quite annoying.

>>128
Nag to Mr VacBob on IRC about it.

Name: Anonymous 2010-03-07 13:40

>>133
get on IRC fgt

Name: Anonymous 2010-03-07 13:41

>>131
Fuck off, ``faggot''.

Name: Anonymous 2010-03-07 14:23

>>134
I already am on IRC, but >>128 is the one bitching about it. Also if he decides to go, request a ban on >>135 thanks.

Name: Anonymous 2010-03-07 14:32

What happened to this awesome board ? :-(

Name: Anonymous 2010-03-07 14:38

>>137
I blame Italians.

Name: Anonymous 2010-03-07 15:04

[/i]

Name: Anonymous 2010-03-07 15:04

hi

Name: Anonymous 2010-03-07 15:05

[b]GOD FUCKING DAMMIT![/b][/i]

Name: Anonymous 2010-03-07 15:06

hi[/i]</i>

Name: Anonymous 2010-03-07 15:36

( ̄へ ̄) Hello. I am "Mr. Likes To Sage Threads". I do believe this is a thread in need of Sage, so I would like to sage it. That is why my name is "Mr. Likes To Sage Threads".

Name: Anonymous 2010-03-07 15:45

( ̄へ ̄) Hello. I am "Mr. Likes To Bump Threads". I do believe this is a thread in need of Bump, so I would like to bump it. That is why my name is "Mr. Likes To Bump Threads".

Name: Anonymous 2010-03-07 16:01

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.
[o]The task : [/o]

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

[o]My solution : [/o]

#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-07 16:05

the problem obviously is that the </i> got truncated. I can't think of a way to get bbcode to make an </i> without a matching <i> to come first.
That means the only solutions involve outside help, like greasemonkey or mods. Either that, or /prog/ actually gets tired of it and lets it die on its own. The lattermost solution is the likeliest to happen, unless someone is able to innovate with their BB

Name: Anonymous 2010-03-07 16:14

WHO FUCK IS HAXIN MY PROG

Name: Anonymous 2010-03-07 16:16

[quote]Either that, or /prog/  actually gets tired of it and lets it die on its own.[/quote]
You mean like /prog/ got tired of haxing anii?

Name: Anonymous 2010-03-07 16:28

>>148
You mean one guy will keep bumping this for two years just to be a faggot? Sure, why not.

Name: Anonymous 2010-03-07 16:31

>>146
the problem obviously is that the </i> got truncated
So how come that there were other attempts at replicating the problem, with other tags, with no success?

Name: Anonymous 2010-03-07 16:46

>>146
That means the only solutions involve outside help, like greasemonkey or mods.

We also can add an unmatched <b>. It's not the solution, but definitely a step in the right direction. Then, an unmatched <u>. Unmatched <span class="o"> would be much harder I'm afraid, but with that problem we better deal when it arises.

Name: Anonymous 2010-03-07 16:55

My vote goes to >>151.

Name: Anonymous 2010-03-07 22:21

>>150
Oh really?

Name: Anonymous 2010-03-10 16:34

>>153
YA RLY

Name: Anonymous 2010-03-10 16:39

>>151
This was already implemented with varying degrees of success in the now-deprecated thread http://dis.4chan.org/read/prog/1268006800/1- .

Name: Anonymous 2010-03-11 4:08

SAX MY ANUS

Name: Anonymous 2010-03-30 13:58

proof of concept bump

Name: Anonymous 2010-03-30 15:04

>>158
Get Out.

Requesting sagetank.

Name: Anonymous 2010-03-30 15:36

>>158
Wait, why did you tell yourself to get out?

Name: Anonymous 2010-03-30 16:43

Ah I love this thread.

Newer Posts