Cod sursa(job #2807073)

Utilizator teochess2017Togan Teodor-Bogdan teochess2017 Data 23 noiembrie 2021 12:51:38
Problema Lupul Urias si Rau Scor 16
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <iostream>
#include <stdio.h>

using namespace std;

#define MAXN 100000
#define CLOSESTPOW 131072

int v[MAXN][2];

void swap(int &a, int &b){
  int aux;
  aux = a;
  a = b;
  b = aux;
}
void quicksort(int min, int max){
  int piv, b, e;
  piv = v[(min + max) / 2][0];
  b = min;
  e = max;
  while(piv > v[b][0]){
    b++;
  }
  while(piv < v[e][0]){
    e--;
  }
  while(b < e){
    swap(v[b][0], v[e][0]);
    swap(v[b][1], v[e][1]);
    do{
      b++;
    }while(piv > v[b][0]);
    do{
      e--;
    }while(piv < v[e][0]);
  }
  if(min < e){
    quicksort(min, e);
  }
  if((e + 1) < max){
    quicksort(e + 1, max);
  }
}

int main()
{
    FILE *fin, *fout;
    int n, x, l, i, actint, max;
    long long s;
    fin = fopen("lupu.in", "r");
    fscanf(fin, "%d%d%d", &n, &x, &l);
    for(i = 0; i < n; i++){
      fscanf(fin, "%d%d", &v[i][0], &v[i][1]);
    }
    fclose(fin);
    quicksort(0, n - 1);
    actint = x;
    max = 0;
    s = 0;
    i = n - 1;
    while(x < v[i][0]){
      i--;
    }
    for(; 0 <= i; i--){
      if(v[i][0] < (actint - l + 1)){
        actint = actint - ((actint - v[i][0]) / l) * l;
        s += max;
        max = 0;
      }
      if(max < v[i][1]){
        max = v[i][1];
      }
    }
    s += max;
    fout = fopen("lupu.out", "w");
    fprintf(fout, "%lld", s);
    fclose(fout);
    return 0;
}