/home/toolbox/public_html/solutions/121/12150/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16 /*
17 * Author: Isaac Traxler
18 * Date: 2025-04-15
19 * Purpose: fun
20 * Problem: 12150 - Pole Position
21 */
22
23 /*
24 * This template reads data until a terminating value is reached.
25 */
26
27 #define MAX_CARS 1005
28 #define MISSING -1
29
30 int numCars;
31 int cars[MAX_CARS];
32 int passes[MAX_CARS];
33 int final[MAX_CARS];
34
35
36 void init()
37 {
38 /* FUNCTION init */
39 } /* FUNCTION init */
40
41 void dump()
42 {
43 /* FUNCTION dump */
44 } /* FUNCTION dump */
45
46 int getInput()
47 {
48 /* FUNCTION getInput */
49 int dataReadFlag;
50 int i;
51
52 scanf(" %d ", &numCars);
53 dataReadFlag = 0 != numCars;
54
55 for (i=0; i<numCars; i++)
56 {
57 /* get car info */
58 scanf(" %d %d ", &cars[i], &passes[i]);
59 final[i] = MISSING;
60 } /* get car info */
61
62 return (dataReadFlag);
63 } /* FUNCTION getInput */
64
65 void process()
66 {
67 /* FUNCTION process */
68 int i;
69 int valid = TRUE;
70 int start;
71
72 for (i=0; i<numCars; i++)
73 {
74 /* for each car */
75 start = i + passes[i];
76 /* start must meet following criteria
77 * be >= 0 (did not start at a negative spot)
78 * be < numCars (must start within field)
79 * no other car in same spot
80 */
81 valid = valid && (0 <= start) && (numCars > start) && (MISSING == final[start]);
82 if (valid)
83 {
84 /* only do assignment if it is vlaid */
85 final[start] = cars[i];
86 } /* only do assignment if it is vlaid */
87 } /* for each car */
88
89 if (valid)
90 {
91 /* print out starting order */
92 printf("%d", final[0]);
93 for (i=1; numCars>i; i++)
94 {
95 /* print cars */
96 printf(" %d", final[i]);
97 } /* print cars */
98 printf("\n");
99 } /* print out starting order */
100 else
101 {
102 /* invalid start */
103 printf("-1\n");
104 } /* invalid start */
105 } /* FUNCTION process */
106
107 int main()
108 {
109 /* main */
110 int moreToDo;
111
112 init();
113 moreToDo = getInput();
114 while (moreToDo)
115 {
116 /* while */
117 process();
118 moreToDo = getInput();
119 } /* while */
120
121 return EXIT_SUCCESS;
122 } /* main */
123