Consider the four blocks: 10x10 (100), 6x6 (36), 5x7 (35), 3x7 (21). Your program finds the first two as a stack, and then since neither 5x7 nor 3x7 fit that's it. However, blocks 1, 3, and 4 stack nicely, meaning the answer should be 3.
Consider the four blocks: 10x10 (100), 6x6 (36), 5x7 (35), 3x7 (21). Your program finds the first two as a stack, and then since neither 5x7 nor 3x7 fit that's it. However, blocks 1, 3, and 4 stack nicely, meaning the answer should be 3.
A brute force algorithm could be used, but i would like to see a nice description of your own once you find one.
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
Lala, I have to admit, I'm a little bit of a junkie when it comes to programming problems, which is why I asked you to post a link to the problem.
Getting over the terrible altavista babelfish translation of that site, I managed to get the gist of the problem and wrote a solution. It works for all the cases. If you want to discuss the problem let me know, although I know that it would be much more satisfying if you figured it out yourself.
Shure, a compiler can diagnose if parameters to the the scanf family of functions are not pointers but only if the compiler treats diagnosing this functions differently.
Generally it is impossible for the compiler to check the type of parameters that are passed to functions that use variable argument lists. Also there is no way for the compiler to know if a paramiter is an output parameter ( must be a pointer ) or an input parameter ( could be a pointer ).
Kurt
arpsmack
I'd really like to see your solution... I've been struggling with this problem for a while and haven't gotten anywhere =/
Perhaps you could just describe your algo and let me try to implement it....
BTW did you submit your solution? Did you score all points?
Dam.... I just realised... How is the site going to input my program?
There's nowhere telling me how...I'm reading from a file but how am I supposed to know if they're using a file too? And if so what's the files name?
site will redirect stdinput and output, so your program should read from stdinput and output to stdoutput
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Vart
So should I start the program with something like:
And then break that string into the numbers I need? Is there a way to know the length of the input?Code:char str[1000]; scanf("%1000s", str);
Note that you should specify the size of your buffer - 1 to scanf because it will read the number of chars you specify. You need space for the NULL char too.
Instead of writing from a file, read chunks from stdin instead, just as if inputting info from a user.
I believe this is what it's all about.
NoSo should I start the program with something like:
No, and no need.Is there a way to know the length of the input?
When you read a number of blocks and it is zero - exit your main loop
So in pseudocode it will be something like
Code:while(read number) { if(number == 0) break; for(i=0;i<number;i++) { read line; split it in block sizes; } order blocks; count piramid hight and ouput it; }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Thanks! Now I finally scored something =P I got 20/100 points, now I beleive the problem is only whithin my algorithm.
In case anyone wants to know:
I got one more question now though...Code:#include <stdio.h> struct caixas { unsigned int x[5000]; unsigned int y[5000]; unsigned int area[5000]; }; int main (int argc, char *argv[]) { struct caixas caixa; int start = 1, num, i, n, maiorArea, velhoY = 0; int velhoX = 0, teste = 1, numeroBlocos, num2, nn; while (true) { if (start) { if (scanf("%d", &num) == EOF || !num) return 0; num2 = num; velhoX = velhoY = 0; start = 0; } else { numeroBlocos = 0; for (i = 0; i < num2; i++) { scanf("%d %d", &caixa.x[i], &caixa.y[i]); caixa.area[i] = caixa.x[i] * caixa.y[i]; } while (num--) { maiorArea = 0; for (n = 0; n < num2; n++) { if (caixa.area[n] > maiorArea) { maiorArea = caixa.area[n]; nn = n; } } caixa.area[nn] = 0; if ((!velhoX && !velhoY) || (caixa.x[nn] <= velhoX && caixa.y[nn] <= velhoY) || (caixa.x[nn] <= velhoY && caixa.y[nn] <= velhoX)) { velhoX = caixa.x[nn]; velhoY = caixa.y[nn]; numeroBlocos++; } } printf("Teste %d\n%d\n\n", teste++, numeroBlocos); start = 1; } } return 0; }
Since I altered the program to read from stdin I can not test it here... How do I make the input?
Last edited by lala123; 03-15-2008 at 10:54 AM.
Just put the input set in a text file (like boxes.txt) and execute your program on the command line like this:
boxes < boxes.txt
Ok now here's a description of how I solved the problem:
The algorithmic part operates in O(N^2) (approximately N^2 / 2). The sort, assuming you use a decent sorting algorithm, should take O(n log n). Is this the optimal solution? Probably not. But it works, and yes I submitted it and it gets all 100 points.Code:1) Read in N (the number of boxes) 2) Read in each box's length and width into an array (I used an array of structs) If a box's length is less than its width, swap the two values (this is needed to make sorting easier) 4) Sort (in increasing order) the array of boxes by their largest dimension, if two boxes have the same largest dimension, sort by the sum of their dimensions 5) See the following pseudo-code: // stacks[n] = the number of boxes in the stack with box n at the bottom // This is initialized to all 1's since each box is initially in its own stack stacks = integer array of size N globalMax = 1 for j in 1 to N-1 localMax = stacks[j] for k in 0 to j-1 if box[k] fits on top of box[j] localMax = max(localMax, stacks[k] + stacks[j]); stacks[j] = localMax globalMax = max(globalMax, localMax) 6) The answer is now in globalMax Here's a basic skeleton of my program: int main(void) { // declare variables while (1) { // read in N if (N == 0) break; // create an array big enough to hold N boxes (optionally you could just use // a statically allocated array large enough to hold the max value for N) // read in boxes // sort boxes // do algorithm // print result } }
Last edited by arpsmack; 03-15-2008 at 06:34 PM. Reason: Wasn't consistent with array indexing
Amazing =) Thank you.
The awnser is actually much more complicated than what I had imagined :P
I'll now go and try to implement arpsmack's solution but before that I have one more problem I couldn't solve =/
This one is so easy... I'm actually ashamed for having to ask for help in this one...
He says that he'll give a list in this format:
He then asks you to identify where the number equals it's position in the list. The correct output would be:Code:4 4 5 3 1 10 9 8 7 6 1 4 3 2 12 10 0
So I came up with this:Code:Teste 1 3 Teste 2 10
Wich seemed to work, but when I submitted my solution I only got 10/50 points... Any help would be really appreciated.Code:#include <stdio.h> int main(int argc, char *argv[]) { unsigned int start = 1, numero = 0, num, c, posicao = 1, teste = 1; while (1) { if (scanf("%d", &num) == EOF || !num) return 0; posicao = 1; while ((c = getc(stdin)) != '\n') { if (c == 32) // if c equals space { if (numero == posicao++) { printf("Teste %d\n%d\n\n", teste++, posicao - 1); break; } numero = 0; } else numero = numero*10 + (c - 48); // (c - 48) turns '9' into 9 } } return 0; }
Here's the link to the problem: http://olimpiada.ic.unicamp.br/prati...vel2/quermesse