Cod sursa(job #2845699)

Utilizator teochess2017Togan Teodor-Bogdan teochess2017 Data 8 februarie 2022 10:49:34
Problema Trapez Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

#define MAXN 1000
#define INF 2000000000

struct fractie{
  long long sus, jos;
};
struct coord{
  int x, y;
};
coord v[MAXN];
fractie a[MAXN * MAXN];

long long mod(long long x){
  if(x < 0){
    return -x;
  }else{
    return x;
  }
}
bool cmp(fractie a, fractie b){
  if((a.jos < 0) && (b.jos < 0)){
    if((a.sus * b.jos) < (a.jos * b.sus)){
      return true;
    }else{
      return false;
    }
  }else if((b.jos < 0) || (a.jos < 0)){
    if((a.sus * b.jos) > (a.jos * b.sus)){
      return true;
    }else{
      return false;
    }
  }else{
    if((a.sus * b.jos) < (a.jos * b.sus)){
      return true;
    }else{
      return false;
    }
  }
}
bool egale(fractie a, fractie b){
  if((a.sus * b.jos) == (a.jos * b.sus)){
    return true;
  }else{
    return false;
  }
}

int main()
{
    FILE *fin, *fout;
    int n, i, j, ind, nre, nrt;
    fin = fopen("trapez.in", "r");
    fscanf(fin, "%d", &n);
    for(i = 0; i < n; i++){
      fscanf(fin, "%d%d", &v[i].x, &v[i].y);
    }
    fclose(fin);
    ind = 0;
    for(i = 0; i < n; i++){
      for(j = i + 1; j < n; j++){
        if(v[i].x == v[j].x){
          a[ind] = {INF + 1, 1};
        }else if(v[i].y == v[j].y){
          a[ind] = {INF + 2, 1};
        }else{
          a[ind] = {v[i].x - v[j].x, v[i].y - v[j].y};
        }
        ind++;
      }
    }
    sort(a, a + ind, cmp);
    nre = 1;
    nrt = 0;
    for(i = 1; i < ind; i++){
      if(egale(a[i - 1], a[i])){
        nre++;
      }else{
        nrt = nrt + (nre * (nre - 1)) / 2;
        nre = 1;
      }
    }
    fout = fopen("trapez.out", "w");
    fprintf(fout, "%d", nrt);
    fclose(fout);
    return 0;
}