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: >>158 2010-03-30 16:52

>>159
It was a typo. I meant that >>157 should get back to wherever he came from and these Shiichan exploiting threads should get fixed (along with the exploit), or if no mod is willing to fix the issue themselves, the thread should be killed by its users in some way (spammed to 1000?).

Name: Anonymous 2010-03-30 16:55

>>161
Don't worry, I'll start another one once this one dies. From behind more or less seven proxies, of course.

Name: Anonymous 2010-03-30 17:39

>>162
Good. We'll add your set to the list.

Name: Anonymous 2010-03-30 20:07

>>163
Sorry, you forgot your bampu. That is unacceptable.

Name: Anonymous 2010-03-31 1:10

Why did you have to bring back this one? Only the [m] one was useful.

Name: OP 2010-04-01 7:51

I'm gonna keep bumping my thread until someone helps me with the optimization.
That's all.

Name: OP 2010-04-01 11:50

bumpety bumpety bump bump

Name: Anonymous 2010-04-01 12:49

>>166-167
You're not OP, but you already knew that.

Name: Anonymous 2010-04-01 20:04

Sagefault - thread bumped.

Name: Anonymous 2010-04-01 20:10

Requesting sage tank

Name: Anonymous 2010-04-01 20:41

Cancelling sage tank request.

Name: Anonymous 2010-04-01 20:54

>>171,170
Different people.

Name: Anonymous 2010-04-01 21:00

>>172
How can you tell?

Name: Anonymous 2010-04-01 22:07

>>173
Because I was one of those people, but not the other, duh.

Name: Anonymous 2010-04-02 0:14

Requesting age jeep.

Name: Anonymous 2010-04-02 21:11

BEEP BEEP IM AN AGE JEEP

Name: Anonymous 2010-04-02 23:27

CLANK CLANK IM A SAGE TANK

Name: Anonymous 2010-04-03 2:47

Sagefault (thread bumped).

Name: Anonymous 2010-04-03 9:05

This thread has been [sup]threadstopped[/sup, you can't reply anymore

Name: Anonymous 2010-04-03 10:33

Sagefault (thread bumped).

Name: Anonymous 2010-04-03 20:28

     | \
     |Д`)   No one is here.
     |⊂     I can dance now !
     |

     ♪  ☆
   ♪   / \    RANTA TAN
      ヽ(´Д`;)ノ   RANTA TAN
         (  へ)    RANTA RANTA
          く       TAN

   ♪    ☆
     ♪ / \   RANTA RANTA
      ヽ(;´Д`)ノ  RANTA TAN
         (へ  )    RANTA TANTA
             >    TAN

Name: Anonymous 2010-04-03 20:29

sage
this
shit

Name: Anonymous 2010-04-03 20:31

>>182
Good idea.

Name: Anonymous 2010-04-04 0:34

>>182
I wrote a program for you, if you would like to fuck this thread's shit:


(defun scroll-this-shit-off-the-front-page ()
  "Scroll a stupid fucking topic off the page"
  (loop for each $x in xrange([1..10])
        \x -> begin do |y|
        $fh `open` "DIS.4CHAN.ORG".downcase!;
        $fh.WriteLine(">>67", y);
        $fh ! close.
        end)
  (format t "I scrolled that shit motherfucker"))

Name: Anonymous 2010-04-04 0:43

>>184
I have a better one


module Sagebomb where

import Network.Curl

progPost id' name mail body = do
  let content =
        ["bbs=prog", "email=", " lol what 2"
        ,"kotehan=" ++ name, "meiru=" ++ mail
        ,"id=" ++ show id', "com=" ++ body]
  curlPost "http://dis.4chan.org/post" content

sagebomb id' = progPost id' "Haxus der Mensch" mail ""
  where mail = replicate 1000 '\n'

Name: Anonymous 2010-04-04 1:52

>>185
Optimization thread
Haskell sagebomb

Chuckle.

Name: Anonymous 2010-04-04 2:11

Sagefault (thread bumped).

Name: Anonymous 2010-04-04 8:52

>>184
Is that every programming language simultaneously? Amazing.

Name: Anonymous 2010-04-04 9:01

bump

Name: Anonymous 2010-04-04 9:01

Wait... what?

Name: Anonymous 2010-04-04 10:23

>>184
Wow.

Name: Anonymous 2010-04-04 13:49

>>188,191
What, have you rubbernecks never seen a LOOP macro before? Please.

Go read your On Lisp, by Paul Graham.

Name: Over 193 Thread 2010-04-04 16:19

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

Name: Anonymous 2010-04-04 16:37

>>185
Nice try, but that exploit was already patched, Haskell-programmer-kun.

Name: Anonymous 2010-04-04 17:06

>>192
>>184's LOOP macro was quite different from the real LOOP macro. It looks like a combination of Haskell, Python and Perl.

Name: Anonymous 2010-04-04 19:49

>>195
And Ruby (|y|, .downcase!) and maybe C# (WriteLine).

>>184 is clearly an EXPERT PROGRAMMER.

Name: Anonymous 2010-04-04 21:10

hmm

Name: Anonymous 2010-04-04 22:33

hmm
hax

Name: Anonymous 2010-04-05 5:51

This forum is not optimized enough

Name: Anonymous 2010-04-05 13:09

>>199
Just you wait til our CFLAGS kick in. Then you'll see.

Newer Posts