Cod sursa(job #2814274)

Utilizator ptlsebiptl sebi ptlsebi Data 7 decembrie 2021 21:02:52
Problema Orase Scor 10
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

void read_int32_t(FILE *__restrict stream, int32_t *__restrict nr) {
  uint8_t ch;
  *nr = 0;
  while ((ch = fgetc(stream)) && ('0' <= ch && ch <= '9')) {
    *nr *= 10;
    *nr += ch - '0';
  }
  if (ch == '\r') {
    fgetc(stream);
  }
}

struct oras {
  int32_t pos;
  int32_t d;
};

int comp(const void *o1, const void *o2) {
  return ((struct oras *)(o1))->pos > ((struct oras *)(o2))->pos;
}

struct oras a[50000];

int32_t n, m;

int main(void) {
  {
    FILE *__restrict in = fopen("orase.in", "r");
  
    read_int32_t(in, &m);
    read_int32_t(in, &n);
    {
      int32_t i;
      int32_t x, y;
      for(i = 0; i < n; ++i) {
        read_int32_t(in, &x);
        read_int32_t(in, &y);
        a[i].pos = x;
        a[i].d = y;
      }
    }
  
    fclose(in);
  }

  qsort(a, n, sizeof(struct oras), comp);

  int32_t sm;
  int32_t sl;

  {
    int32_t i;
    if (n == 1) {
      sm = INT32_MIN;
      sl = 0;
    } else {
      sm = INT32_MIN;
      sl = 0;
      for(i = 0; i < n - 1; ++i) {
        if (a[i].d - a[i].pos > sm - sl) { /** a[i].d + a[i + 1].d + (a[i + 1].pos - a[i].pos) > sm + a[i + 1].d + (a[i + 1].pos - sl) **/
          sm = a[i].d;
          sl = a[i].pos;
        } 
      }
    }
  }

  {
    FILE *__restrict out = fopen("orase.out", "w");
  
    if (sm == INT32_MIN) {
      fputs("0", out);
    } else {
      fprintf(out, "%u", sm + a[n-1].d + (a[n-1].pos - sl));
    }
  
    fclose(out);
  }
  return 0;
}