yolila

USACO Job Processing

Mar 14th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define NJOB    1000
  4. #define NA      30
  5. #define NB      30
  6. #define MAX_TIME  40001
  7. #define MAX(a, b) ((a)>(b)?(a):(b))
  8.  
  9. int njob, na, nb;
  10. int rate_a[NA] = {0};
  11. int rate_b[NB] = {0};
  12. int done_a[NJOB] = {0};
  13. int done_b[NJOB] = {0};
  14. int fini_a, fini_b;
  15.  
  16. int job(int done_x[], int rate_x[], int size)
  17. {
  18.     int free_time[MAX(NA, NB)] = {0};
  19.     int _idx_mach = 0;
  20.     int _end_time = 0;
  21.     int lv, lv2;
  22.  
  23.     for (lv = 0; lv < njob; lv++) {
  24.         _end_time = MAX_TIME;
  25.         _idx_mach = -1;
  26.         for (lv2 = 0; lv2 < size; lv2++) {
  27.             if (_end_time > free_time[lv2] + rate_x[lv2]) {
  28.                 _end_time = free_time[lv2] + rate_x[lv2];
  29.                 _idx_mach = lv2;
  30.             }
  31.         }
  32.         free_time[_idx_mach] = _end_time;
  33.         done_x[lv] = _end_time;
  34.     }
  35.     return _end_time;
  36. }
  37.  
  38. void init(void)
  39. {
  40.     FILE *fin;
  41.     int lv;
  42.  
  43.     fin = fopen("job.in", "r");
  44.     fscanf(fin, "%d %d %d ", &njob, &na, &nb);
  45.     for (lv = 0; lv < na; lv++) {
  46.         fscanf(fin, "%d ", &rate_a[lv]);
  47.     }
  48.     for (lv = 0; lv < nb; lv++) {
  49.         fscanf(fin, "%d ", &rate_b[lv]);
  50.     }
  51.     fclose(fin);
  52. }
  53.  
  54. int main()
  55. {
  56.     FILE *fout;
  57.     int lv;
  58.  
  59.     init();
  60.     fini_a = job(done_a, rate_a, na);
  61.     job(done_b, rate_b, nb);
  62.     fini_b = 0;
  63.     for (lv = 0; lv < njob; lv++) {
  64.         if (fini_b < done_a[lv] + done_b[njob-lv-1]) {
  65.             fini_b = done_a[lv] + done_b[njob-lv-1];
  66.         }
  67.     }
  68.  
  69.     fout = fopen("job.out", "w");
  70.     fprintf(fout, "%d %d\n", fini_a, fini_b);
  71.     fclose(fout);
  72.     return 0;
  73. }
  74.  
  75. /*
  76. INPUT FORMAT
  77.  
  78. Line 1:  Three space-separated integers:
  79. N, the number of jobs (1<=N<=1000).
  80. M1, the number of type "A" machines (1<=M1<=30)
  81. M2, the number of type "B" machines (1<=M2<=30)
  82. Line 2..etc:     M1 integers that are the job processing times of each type "A" machine (1..20) followed by M2 integers, the job processing times of each type "B" machine (1..20).
  83. SAMPLE INPUT (file job.in)
  84.  
  85. 5 2 3
  86. 1 1 3 1 4
  87. OUTPUT FORMAT
  88.  
  89. A single line containing two integers: the minimum time to perform all "A" tasks and the minimum time to perform all "B" tasks (which require "A" tasks, of course).
  90. SAMPLE OUTPUT (file job.out)
  91.  
  92. 3 5
  93.  
  94. */
Advertisement
Add Comment
Please, Sign In to add comment