Postal Service

All submissions for this problem are available.
The Chef is on vacation in the beautiful country of Chefville. The island is a narrow strip of land running from north to south. A single road from north to south connects all the villages in the country. There, he observed, the postal services works in a very curious way.
There is a postman for every village. In the morning, say postman A, starts traveling to the North, disbursing all the mails on the way. When he meets another postman coming from the opposite direction on the way, say B, A hands over all the mails to B which are to be delivered to addresses that lie in the North and vice versa for B. Then both turn back and start traveling in the opposite directions. So, now A travels to the South and when he meets another postman on the way, the same thing happens again. This keep on till the duty hours of the day finishes and they don't loose any time in the swapping and turning back. All postmen travel at the same speed of 1 m/s always and they arbitrarily choose their direction to start with in the morning.
Now, at any given time of the day, the Chef wants to know the position of any given postman and the number of times he has met another postman till that time (including this moment of time).
Input
The road can be considered to stretch infinitely in both the directions for this problem. The first line contains the total number of test cases (<= 10). Each of the case begins with N (0 < N <= 500) on a separate line, the total number of villages in Chefville. The next line contain N distinct integers separated by space which are the positions (between 0 and 1000000000, in meters including those, 0 being the position of a northern point on the strip) in increasing order of the N villages on the road. The next line the initial directions of the corresponding postmen of the villages as N integers which are either 0 or 1. 0 means North and 1 South. Then comes the total number of queries Q (<= 1000) for this test case on separate line. The next Q lines contain two integers, first the postman number I (0 <= I < N) and then the time T (<= 1000000000 in seconds).
Output
For every query of a test case, you have to output the position of the postman number I at time T and the number times he has met another postman till then, on the same line separated by a space.
Example
Input: 1 5 1 24 28 34 94 0 1 0 0 0 2 2 11 0 9 Output: 23 2 8 0
Code
#include <stdio.h> #include <assert.h> #include <stdlib.h> #define MAXLEN 500int compare(const void * a, const void * b) { return *((int *)a)  *((int *)b); }int find(int key, int* ar, int sz){ int a=0, b=sz1; while(a<=b){ int m = (ab)/2; if(key == ar[m]) return m; if(key>ar[m]) a=m1; else b=m; } assert(false); return 1; }void bubbleSort(int newPositions[MAXLEN], int switches[MAXLEN], int n) { int temp; for(int i = 0; i < n; i++) { for(int j = 1; j < n; j++) { if (newPositions[j] < newPositions[j1]) { temp = newPositions[j]; newPositions[j] = newPositions[j1]; newPositions[j1] = temp; switches[j]++; switches[j1]++; } } } }int main() { int t; int n; int positions[MAXLEN]; int directions[MAXLEN]; int q; int postman; int time; int newPositions[MAXLEN]; int switches[MAXLEN]; int sorted_final[MAXLEN]; int rev_count; scanf("%d", &t); while(t) { scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &positions[i]); for(int i = 0; i < n; i++) scanf("%d", &directions[i]); scanf("%d", &q); for(int i = 0; i < q; i++) { rev_count = 0; scanf("%d %d", &postman, &time); for(int j = 0; j < n; j++) { if (directions[j]) newPositions[j] = positions[j]+time; else newPositions[j] = positions[j]time; switches[j] = 0; sorted_final[j] = newPositions[j]; } qsort(newPositions, n, sizeof(newPositions[0]), compare); for(int i=0; i<n; i++){ if(sorted_final[i]==newPositions[postman]){ rev_count++; continue; } int rank = find(sorted_final[i], newPositions, n); if((postmani)*(postmanrank)<=0) rev_count++; } printf("%d %d\n", &newPositions[postman], rev_count); } } return 0; }
Steps to be followed
1. Read the given description carefully and understand the problem.
2. Analyze the code given in the problem and find bugs(errors) in these codes..
3. The given code can be copied and compiled in an online compiler (Eg. ideone.com).
4. Once the bugs are eliminated from the code, the clean code can be submitted as the solution to the problem..
5. The solution should be a debugged version of the code provided and must satisfy the test cases to be accepted..
All the best!!
Author:  sneha_rajeev 
Tags  sneha_rajeev 
Date Added:  21012016 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages: 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 