Pagini recente » Cod sursa (job #2686724) | Cod sursa (job #1933853) | Cod sursa (job #534427) | Cod sursa (job #1216106) | Cod sursa (job #1817869)
#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;
}