## 120 - Stacks of Flapjacks

Moderator: Board moderators

devious
New poster
Posts: 13
Joined: Wed Feb 22, 2006 1:26 am
what?!

hoooh
New poster
Posts: 1
Joined: Thu Aug 17, 2006 8:52 pm

### 120 Invalid reference memory help me!!!!

Help me

why?

# include <stdio.h>
# include <stdlib.h>

int comparar(const void *a,const void *b)
{
if(*(int *)a<*(int *)b)
return(1);

if(*(int *)a>*(int *)b)
return(-1);

return(0);
}

void Voltear(int pank[100],int pos,int cant)
{
int i;
int aux;
int t;

printf("%d ",(cant-pos)%cant);

t=pos/2+pos%2;

for(i=0;i<t;i++)
{
aux=pank;
pank=pank[pos-i];
pank[pos-i]=aux;
}
}

int Posicion(int pank[100],int num)
{
int i=0;

while(true)
{
if(pank==num)
return i;
i++;
}
}

void Procesar(int pank[100],int copPank[100],int cant)
{
int i;
int pos;
qsort(copPank,cant,sizeof(copPank[0]),comparar);

for(i=0;i<cant;i++)
{
pos=Posicion(pank,copPank);

if(pos==0)
{
Voltear(pank,cant-i-1,cant);
continue;
}

if (pos+i==cant-1)
continue;

Voltear(pank,pos,cant);
Voltear(pank,cant-i-1,cant);
}
}

int main()
{
char c;
char f;
int pank[100];
int copPank[100];
int i=0;

while(scanf("%d%c",&f,&c)!=EOF)
{
pank=f;
copPank=f;

i++;
if(c=='\n')
{
Procesar(pank,copPank,i);
printf("\n");
i=0;
}
}

return 0;

}

nev4
New poster
Posts: 15
Joined: Sun Apr 30, 2006 10:19 am
Location: Lithuania
using pointers and arrays(which are pointers too) causes this(if u try to access memory u shouldn't to).

mosaick2
New poster
Posts: 21
Joined: Wed Mar 08, 2006 4:05 am

Actually, We don't need to consider about the same number inputs.
The problem defined like below.
... All pancakes in a stack have different diameters. ...
So I made a algorithm based on the problem. and I got A.C.

Frankly, If I input the same numbers in my program, It's crazy..

Disatoba
New poster
Posts: 6
Joined: Thu Feb 02, 2006 3:32 pm
Location: Bogot

### 120 Problem

HI I got WA in problem 120, i dont know why? would someone help me with any test cases or ideas for to get AC?
Thanks!

Disatoba
New poster
Posts: 6
Joined: Thu Feb 02, 2006 3:32 pm
Location: Bogot

### 120 WA

HI I got WA in problem 120, i dont know why? would someone help me with any test cases or ideas for to get AC?
Thanks!

Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

### Re: 120 Problem

Disatoba wrote:HI I got WA in problem 120, i dont know why? would someone help me with any test cases or ideas for to get AC?
Thanks!
Post your question to the forum about problems from the set I. This forum is about problems with java.

douzi0108
New poster
Posts: 2
Joined: Sun Oct 01, 2006 7:13 pm

### i dununderstand why this input for 120 will be this output

hi ppl,

below is the input and output.

Code: Select all

``````Input:

1 2 3 1 2
2 3 1
2 3 4 1 8 9 6 7
2 3 4 1 8 9 6 7 0
5 1 3 6 4 1 9 2 0 18 29

Output:
1 1 2 2 3
3 1 3 0
1 2 3
2 1 0
1 2 3 4 6 7 8 9
6 3 1 3 0
0 1 2 3 4 6 7 8 9
7 4 2 4 2 1 0
0 1 1 2 3 4 5 6 9 18 29
9 5 9 6 8 3 8 6 9 0
``````
for the first 5 input, i can see that once all the big pancakes has been to the top, we can start flipping them to the bottom. but when i apply this to the last input, i dun understand why the biggest pancake is not always the first to be chosen.

can someone pls further explain this to me? thx lots!

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:
If I simulate like these:
8 4 6 7 5 2 -> 6
2 5 7 6 4 8 -> 3
7 5 2 6 4 8 -> 5
4 6 2 5 7 8 -> 2
6 4 2 5 7 8 -> 4
5 2 4 6 7 8 -> 3
4 2 5 6 7 8 -> 2
2 4 5 6 7 8 -> 0

5 1 2 3 4 -> 5
4 3 2 1 5 -> 4
1 2 3 4 5 -> 0
Does the algorithm correct? I just want to clarify if my approach is correct.

tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am
I think your approach is correct.
But the flips' ID is a bit funny.
(.... you may have done it on purpose to explain plainly for us)
Convert to "ID = (Number of pancakes) + 1 - ID".
Roby wrote: 8 4 6 7 5 2 -> 1
2 5 7 6 4 8 -> 4
7 5 2 6 4 8 -> 2
4 6 2 5 7 8 -> 5
6 4 2 5 7 8 -> 3
5 2 4 6 7 8 -> 4
4 2 5 6 7 8 -> 5
2 4 5 6 7 8 -> 0

5 1 2 3 4 -> 1
4 3 2 1 5 -> 2
1 2 3 4 5 -> 0
Best regards.

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:
thanx tan_yui, but I got WA... can someone pointed out my mistake? here is my code:

Code: Select all

``removed after AC... thanks a lot tan_yui for your help``
I test with this input:

Code: Select all

``````8 4 6 7 5 2
1 2 3 4 5
5 4 3 2 1
5 1 2 3 4
4 4 1
5 5 5``````
And the output is like this:

Code: Select all

``````8 4 6 7 5 2
6 3 5 2 4 3 2 0
1 2 3 4 5
0
5 4 3 2 1
5 0
5 1 2 3 4
5 4 0
4 4 1
3 0
5 5 5
0``````
Last edited by Roby on Thu Dec 07, 2006 3:49 am, edited 1 time in total.

tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am
Roby wrote:8 4 6 7 5 2
1 2 3 4 5
5 4 3 2 1
5 1 2 3 4
My output is :
8 4 6 7 5 2
1 4 2 5 3 4 5 0
1 2 3 4 5
0
5 4 3 2 1
1 0
5 1 2 3 4
1 2 0
Please check my previous post again.
And, we don't need to consider about the last 2 cases, as mosaick2 says.

Best regards.

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:
What a silly mistake I changed the code so the ID will be like this:
ID = (number of pancakes) - ID
And I got AC..

Oronno
New poster
Posts: 21
Joined: Sun Jul 09, 2006 1:42 pm
Location: Dhaka
Contact:

### 120, how can i get input from text file?

I have solved this problem, but it works only for 1 line input.
I can't understand how can i get multiple input for this progeamme.
Please dont say me to get the input as a string, because i think, it will not the actual solve of this problem.