Page 1 of 1

603 - Parking Lot

Posted: Sun Sep 29, 2002 12:55 pm
by HKcow
Hi, Could anyone tell me why my programme run in WA?
And will there be more than 1 car waiting for one car park? If yes, my programme can't solve the problem.
Please suggest, thx
HKcow

[cpp]
#include <stdio.h>
#include <string.h>

void main() {
int i, j, k, n, waiting;
int wait[20];
int park[20];
int seq[20];
char line[20];
scanf("%d", &n);
for (; n>0; n--) {
memset(wait, -1, 20*sizeof(bool));
memset(park, -1, 20*sizeof(bool));
waiting=0;
scanf("%d", &i);
while (i!=99) {
wait[i-1]=i-1;
park[i-1]=-2;
seq[waiting]=i-1;
waiting++;
scanf("%d", &i);
}
gets(line);
j=waiting;
k=0;
while ((gets(line)!=NULL) && (line[0]!='\0')) {
if (j!=0) {
sscanf(line, "%d", &i);
while (wait[(20+i-1-k)%20]<0)
k=(k+1)%20;
park[wait[(20+i-1-k)%20]]=i-1;
wait[(20+i-1-k)%20]=-1;
j--;
}
}
for (i=0; i<waiting; i++) {
j=seq;
if (park[j]==-2)
printf("Original position %d did not park\n", j+1);
if (park[j]>=0)
printf("Original position %d parked in %d\n", j+1, park[j]+1);
}
if (n>1)
printf("\n");
}
}
[/cpp]

Posted: Sun Sep 29, 2002 2:50 pm
by arc16
i can't find anything wrong with your program, it produce the same output with my AC program for any test case i've tried. Maybe the problem is from reading the input?
And will there be more than 1 car waiting for one car park? If yes, my programme can't solve the problem.
no, there won't be such cases.

Hi, Thx for yr reply~

Posted: Sun Sep 29, 2002 4:50 pm
by HKcow
So, in what cases do you think i can't read the input?
Because i have already considered about the case of no input in the second part.

603 AC

Posted: Tue Dec 03, 2002 9:51 pm
by Shahriar Nirjon
Try this version :

#include <stdio.h>
#include <string.h>

void main() {
int i, j, k, n, waiting;
int wait[20];
int park[20];
int seq[20];
char line[20];

//freopen("c:\\603.in", "rt", stdin);
//freopen("c:\\603px.out", "wt", stdout);


scanf("%d", &n);
for (; n>0; n--) {
memset(wait, -1, 20*sizeof(int)); // <- CORRECTION
memset(park, -1, 20*sizeof(int)); // <- CORRECTION
waiting=0;
scanf("%d", &i);
while (i!=99) {
wait[i-1]=i-1;
park[i-1]=-2;
seq[waiting]=i-1;
waiting++;
scanf("%d", &i);
}
gets(line);
j=waiting;
k=0;
while ((gets(line)!=NULL) && (line[0]!='\0')) {
if (j!=0) {
sscanf(line, "%d", &i);
while (wait[(20+i-1-k)%20]<0)
k=(k+1)%20;
park[wait[(20+i-1-k)%20]]=i-1;
wait[(20+i-1-k)%20]=-1;
j--;
}
}
for (i=0; i<waiting; i++) {
j=seq;
if (park[j]==-2)
printf("Original position %d did not park\n", j+1);
if (park[j]>=0)
printf("Original position %d parked in %d\n", j+1, park[j]+1);
}
if (n>1)
printf("\n");
}
}


INPUT :
2

1
17
3
2
19
20
14
18
4
10
8
99
10
20
12
8
10
2
3
11
4
18
13
7
3
4
5
9
4

7
99
7
19
6

OUTPUT:

Original position 1 parked in 11
Original position 17 parked in 13
Original position 3 parked in 10
Original position 2 parked in 3
Original position 19 parked in 4
Original position 20 parked in 20
Original position 14 parked in 2
Original position 18 parked in 18
Original position 4 parked in 8
Original position 10 parked in 10
Original position 8 parked in 12

Original position 7 parked in 7

603 reading data problem...

Posted: Sat Apr 16, 2005 10:55 pm
by jaracz
hey there!!
How can I read data 'till blank line
for instance

1
2
5
4
23
56

25
346

where it isn't said how many these integer would take place??
scanf reads to end of file, gets also(with blank line indeed), so how can I read only 1 set of integers 'till blank line, then do sth and go on reading...

thx in advance!
(sory for my bad English)

Re: 603 reading data problem...

Posted: Sun Apr 17, 2005 9:47 pm
by ibrahim
jaracz wrote:hey there!!
How can I read data 'till blank line
for instance

1
2
5
4
23
56

25
346

where it isn't said how many these integer would take place??
scanf reads to end of file, gets also(with blank line indeed), so how can I read only 1 set of integers 'till blank line, then do sth and go on reading...

thx in advance!
(sory for my bad English)
First of all i can't understand your query clearly.

I think you want to take input 1 to 56. M i right?

It will be better to use gets.

First take a input using gets. After that make a check, is this blank?
if no, then just use atol or atoi to convert this ascii (string) to integer and store it to an array.
else
break the input process.

I think it will help you. :)

603 parking lots

Posted: Fri Aug 25, 2006 11:10 pm
by L@sogang
I dont know why WA;

plz help me;

give me critical input


[/code]

//@@ ID:18150 START OF SOURCE ENJOY PROGRAMMING

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string>
#include <map>
#include <functional>
#define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t))
//#define
//#define _int64 long long
//#define lld I64d

using namespace std;

class position
{
public:
int oi;
int ci;
};

class op
{
private:
int value;
public:
op(int v) :value(v){}
void operator() (position& elem) const
{
elem.ci += value;
elem.ci %= 20;
}
};



bool cmp(position p1,position p2)
{
return p1.ci < p2.ci;
}

int find(const vector<position>& v,int k)
{
int i;
for(i=0 ; i < v.size(); i++)
{
if(v.ci == k)
return i;
}
return -1;
}
int find2(const vector<position>& v,int k)
{
int i;
int temp=-1;
vector<int> v2;
for(i=0 ; i < v.size(); i++)
{
v2.push_back(v.ci);
}
vector<int>::iterator pos = find_if(v2.begin(),v2.end()
,bind2nd(less<int>(),k));
if(pos == v2.end()) return -1;
else
{
pos = upper_bound(v2.begin(),v2.end(),k);
return distance(v2.begin(),pos) - 1;
}

}

int main()
{
int i,j,k;
vector<position> pos;
vector<int> ori;
vector<position>::iterator aaa;
int ii;
map<int,int> park,temp;
int testcase;
char ch;
int ty;
char line[256] = {0,};
position p;
int diff=0;
int tempindex=0;
cin >> testcase;
while(testcase--)
{
while(cin >> k)
{
if(k == 99)
break;
ori.push_back(k);
p.ci = k;
p.oi = k;
pos.push_back(p);
}
gets(line);
// gets(line);
while(gets(line))
{
if(!isdigit(line[0]))
break;
k = atoi(line);
if(pos.empty()) continue;
sort(pos.begin(),pos.end(),cmp);
ii = find(pos,k);
if(ii == -1)
{
diff=0;
tempindex = find2(pos,k);
if( tempindex > -1)
{
ty = pos[tempindex].ci;
diff = k - ty;

park[pos[tempindex].oi] = k;
pos.erase(pos.begin()+tempindex);
}
else
{
ty = pos.back().ci;
diff = 20 - ty + k;
park[pos.back().oi] = k;
pos.pop_back();
//if(pos.empty()) break;
}

for_each(pos.begin(),pos.end(),op(diff));

}
else if(ii > -1)
{
park[pos[ii].oi] = k;
pos.erase(pos.begin()+ii);
//if(pos.empty()) break;
}
}
for(i=0 ; i < ori.size() ; i++)
{
if(park[ori] != 0)
cout << "Original position " << ori
<< " parked in " << park[ori] << endl;
else cout << "Original position " << ori <<" did not park" << endl;
}
park.clear();
ori.clear();
pos.clear();

if(testcase) cout << endl;
}
return 0;
}

//@@ END OF SOURCE GOOG JOB

Re: 603 - Parking Lot

Posted: Fri Jul 09, 2010 2:06 am
by Mata
Hi

I'm having some troubles with this problem, I get WA, I've try several test cases but can't get AC. Can you help find where my code fails?.

I have three arrays of size 20, in wait I store the positions each car is waiting,wait_ is for the original waiting position and parks is to store the position that car i was parked. I read each line and simulate the process for each car that leaves the parking lot.

Thanks.

Code: Select all

AC.

Re: 603 - Parking Lot

Posted: Thu Aug 19, 2010 6:06 am
by leonardost
Hi Mata,
Mata wrote:I'm having some troubles with this problem, I get WA, I've try several test cases but can't get AC. Can you help find where my code fails?.
It seems there is indeed something wrong with your code. Experiment with this input:

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
99
17
18
1
5
2
10
9
7
3
19
4
6
11
14
13
20
15
12
8
16

The output should be

Original position 1 parked in 1
Original position 2 parked in 2
Original position 3 parked in 3
Original position 4 parked in 4
Original position 5 parked in 5
Original position 6 parked in 6
Original position 7 parked in 7
Original position 8 parked in 8
Original position 9 parked in 9
Original position 10 parked in 10
Original position 11 parked in 11
Original position 12 parked in 12
Original position 13 parked in 13
Original position 14 parked in 14
Original position 15 parked in 15
Original position 16 parked in 16
Original position 17 parked in 17
Original position 18 parked in 18
Original position 19 parked in 19
Original position 20 parked in 20


I was having trouble getting AC with this problem too, but then I saw that you shouldn't print a newline at the end of the last output. Dumb mistake. After I corrected that I got AC :). I was thinking though, shouldn't the judge give a Presentation Error in these cases? Giving you a WA seems pretty harsh for just an extra newline. It can also confuse and frustrate you, as you try to find what's the error in your code when actually there's nothing wrong.

Re: 603 - Parking Lot

Posted: Thu Aug 19, 2010 6:25 am
by Mata
leonardost: thanks for the reply, I got Ac by changing my array size from 20 to 25.

Re: 603 - Parking Lot

Posted: Mon Dec 05, 2011 12:40 am
by sohel
test