This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#define M 200000
#define K 200000
#define INF 0x3f3f3f3f3f3f3f3fLL
int min(int a, int b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int *cc[M], nn[M], hh[M], ll[M], rr[M], m; long long dd[M], ss[K];
void append(int a, int c) {
int i = nn[a]++;
if (i >= 2 && (i & i - 1) == 0)
cc[a] = (int *) realloc(cc[a], i * 2 * sizeof *cc[a]);
cc[a][i] = c;
}
int compare_c(int a, int b) { return a - b; }
int compare_d(int h1, int h2) { return dd[h1] != dd[h2] ? (dd[h1] < dd[h2] ? -1 : 1) : 0; }
int compare_s(int h1, int h2) { return ss[h1] != ss[h2] ? (ss[h1] < ss[h2] ? -1 : 1) : 0; }
int (*compare)(int, int);
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(hh[j], h);
if (c == 0)
j++;
else if (c < 0) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
}
sort(hh, l, i);
l = k;
}
}
int k, k_;
int solve(long long s, long long s_, int h, int i, int j) {
int h_, i_;
long long s1;
ss[k++] = s;
if (k == k_)
return 1;
while (h < m) {
h_ = hh[h];
if (i == 0 && j == ll[h_] && s + dd[h_] > s_)
break;
if (i == 0 && j < rr[h_] && s + cc[h_][j] <= s_ && solve(s + cc[h_][j], s_, h, i, j + 1))
return 1;
if (j < nn[h_])
for (i_ = j; i_ > i && (s1 = s - cc[h_][i_ - 1] + cc[h_][j]) <= s_; i_--)
if (solve(s1, s_, h, i_, j + 1))
return 1;
h++, i = 0, j = h == m ? 0 : ll[hh[h]];
}
return 0;
}
int main() {
int n, h, i, c;
long long s, lower, upper;
scanf("%d%d%d", &n, &m, &k_);
for (h = 0; h < m; h++)
cc[h] = (int *) malloc(2 * sizeof *cc[h]);
while (n--) {
scanf("%d%d", &h, &c), h--;
append(h, c);
}
for (h = 0; h < m; h++)
compare = compare_c, sort(cc[h], 0, nn[h]);
s = 0;
for (h = 0; h < m; h++) {
scanf("%d%d", &ll[h], &rr[h]);
rr[h] = min(rr[h], nn[h]);
if (ll[h] > rr[h]) {
while (k_--)
printf("-1\n");
return 0;
}
for (i = 0; i < ll[h]; i++)
s += cc[h][i];
if (ll[h] == 0)
dd[h] = rr[h] == 0 ? INF : cc[h][0];
else if (ll[h] < nn[h])
dd[h] = cc[h][ll[h]] - cc[h][ll[h] - 1];
else
dd[h] = -1;
}
for (h = 0; h < m; h++)
hh[h] = h;
compare = compare_d, sort(hh, 0, m);
solve(s, 7, 0, 0, ll[hh[0]]);
lower = s - 1, upper = INF;
while (upper - lower > 1) {
long long s_ = (lower + upper) / 2;
k = 0;
if (solve(s, s_, 0, 0, ll[hh[0]]))
upper = s_;
else
lower = s_;
}
k = 0;
if (lower >= s)
solve(s, lower, 0, 0, ll[hh[0]]);
while (k < k_)
ss[k++] = upper;
for (h = 0; h < k; h++)
hh[h] = h;
compare = compare_s, sort(hh, 0, k);
for (h = 0; h < k; h++)
printf("%lld\n", ss[hh[h]] == INF ? -1 : ss[hh[h]]);
return 0;
}
Compilation message (stderr)
Main.c: In function 'append':
Main.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
21 | if (i >= 2 && (i & i - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:82:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d%d%d", &n, &m, &k_);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:86:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d%d", &h, &c), h--;
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:93:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf("%d%d", &ll[h], &rr[h]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |