Cod sursa(job #1817869)

Utilizator TincaMateiTinca Matei TincaMatei Data 28 noiembrie 2016 16:47:36
Problema Transport Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <cstdio>
#include <algorithm>

const int MAX_N = 16000;
int bw[2][1+MAX_N], sbw[2][1+MAX_N];
int top[2];

int min(int a, int b) {
  if(a < b)
    return a;
  return b;
}

int main() {
  int n, q, c, cost, cap, total, a, b, last;
  FILE *fin = fopen("trans.in", "r");
  fscanf(fin, "%d", &n);
  for(int i = 0; i < n; ++i) {
    fscanf(fin, "%d%d", &c, &cost);
    top[c]++;
    bw[c][top[c]] = cost;
  }

  std::sort(bw[0], bw[0] + top[0]);
  std::sort(bw[1], bw[1] + top[1]);

  for(int c = 0; c < 2; ++c)
    for(int j = 1; j <= top[c]; ++j)
      sbw[c][j] = sbw[c][j - 1] + bw[c][j];

  FILE *fout = fopen("trans.out", "w");

  fscanf(fin, "%d", &q);
  for(int i = 0; i < q; ++i) {
    fscanf(fin, "%d%d", &cap, &cost);
    total = top[0] / cap * cost + top[1] / cap * cost;
    a = top[0] % cap;
    b = top[1] % cap;
    printf("%d %d\n", a, b);

    if(a > 0 && b > 0)
      last = 2 * cost;
    else if(a > 0 || b > 0)
      last = cost;
    else
      last = 0;
    printf("%d\n", last);
    if(a + b <= cap)
      last = min(min(last, sbw[0][a] + cost), sbw[1][b] + cost);
    total = total + last;
    fprintf(fout, "%d\n", total);
  }

  fclose(fin);
  fclose(fout);
  return 0;
}