Guest User

Untitled

a guest
Mar 1st, 2011
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. LANG: C
  3. TASK: company
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define MAX 400000
  9.  
  10. int N, head[MAX], r_m[MAX], r_f[MAX];
  11. char sex[MAX];
  12.  
  13. int main() {
  14.     FILE *file;
  15.     int i, j, D;
  16.  
  17.     file = fopen("company.in", "r");
  18.     fscanf(file, "%d\n", &N);
  19.     for (i=0; i<N; ++i)
  20.         fscanf(file, "%d %c\n", &head[i], &sex[i]);
  21.     fclose(file);
  22.  
  23.     D=0;
  24.     for (i=0; i<N; ++i) {
  25.         r_m[i]=0;
  26.         r_f[i]=0;
  27.         for (j=head[i]-1; j>=0; j=head[j]-1) {
  28.             (sex[j] == 'm')? ++r_m[i] : ++r_f[i];
  29.             if (j<i) {
  30.                 r_m[i] += r_m[j];
  31.                 r_f[i] += r_f[j];
  32.                 break;
  33.             }
  34.         }
  35.         if (sex[i] == 'm')
  36.             D -= r_f[i];
  37.         else
  38.             D += r_m[i];
  39.     }
  40.  
  41.     file = fopen("company.out", "w");
  42.     fprintf(file, "%d\n", D);
  43.     fclose(file);
  44.  
  45.     return 0;
  46. }
RAW Paste Data