Tip : The problem you are now attempting to solve is nothing but a Addition & Array Manipulation Problem . You come to see the solution here Because you did not found any clue from Question to solve this Problem . Or you did not Read it Carefully . Am I Right ? I also didn't find any clue.
So, My only request is that please go back Read the Question Once More please. Hope this time you will find something that will help you to solve this problem . Isn't it better to see the Solution ?
Didn't Find Anything that will help you . Before watching the solution please read the Clearance section.
Clearance : To solve this problem you have to just find a Valid Starting Grid .For this you have to Read Two Lines Very Very Carefully .
" The number of the first car in the grid is displayed at the top of the pole, the number of the car in second place is shown below that , and so on. " -- This simply means no two car occupy the same position and the final position of a car never been a Negative number .
And the second is :
" A positive value v beside a car number in the pole means that car has won v positions relative to the starting grid. A negative value v means that car has lost v positions relative to the starting grid. A zero beside a car number in the pole means the car has neither won nor lost any positions relative to the starting grid ( the car is in the same position it started ). " -- This line simply means you have to Add this value with the Current Position . If the result after adding is Occupied the position already then put -1 . Otherwise store the current car number into that position you found after adding in the Resulting array .
Code :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(scanf("%d", &n) && n)
{
int car, pos, m;
int arr[n+2];
memset(arr,0,sizeof(arr));
bool flag = true;
for(int i=1; i<=n; i++)
{
scanf("%d %d", &car, &pos);
m = i + pos;
if(m<1 || m>n)
{
flag = false;
continue;
}
if(!arr[m]&& flag)
arr[m] = car;
else
{flag = false;}
}
if(!flag) puts("-1");
else
{
for(int i=1; i<=n; i++)
{
printf("%d",arr[i]);
if(i<n) printf(" ");
}
printf("\n");
}
}
return 0;
}
=>Question or Need Help ?? Leave a Comment .
Thanks. First I didn't get the problem. Then came here and took your advice to read the problem description again. And then got it and solved it too :D
ReplyDelete