Submission #503412

#TimeUsernameProblemLanguageResultExecution timeMemory
503412rainboyAkcija (COCI21_akcija)C11
110 / 110
2546 ms47472 KiB
/* https://codeforces.com/blog/entry/97754#comment-866029 (errorgorn) */ #include <stdio.h> #define N 2000 #define K 2000 #define S 2000000000002LL unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } int ww[N], tt[N], ii[N]; int cc[K], hh[K], k, k_; long long ss[K]; int compare_t(int i, int j) { return tt[i] - tt[j]; } int compare_cs(int h1, int h2) { return cc[h1] != cc[h2] ? cc[h2] - cc[h1] : (ss[h1] == ss[h2] ? 0 : (ss[h1] < ss[h2] ? -1 : 1)); } int (*compare)(int, int); void sort(int *ii, int l, int r) { while (l < r) { int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp; while (j < k) { int c = compare(ii[j], i_); if (c == 0) j++; else if (c < 0) { tmp = ii[i], ii[i] = ii[j], ii[j] = tmp; i++, j++; } else { k--; tmp = ii[j], ii[j] = ii[k], ii[k] = tmp; } } sort(ii, l, i); l = k; } } int cc_[N + 1][N + 1]; long long ss_[N + 1][N + 1]; int branch(int n, int i, int c, long long s, int c_, long long s_) { if (cc_[i][c] < c_ || cc_[i][c] == c_ && s + ss_[i][c] > s_) return 0; if (i == n) { cc[k] = c, ss[k] = s, k++; return k == k_; } if (branch(n, i + 1, c, s, c_, s_)) return 1; if (tt[ii[i]] > c && branch(n, i + 1, c + 1, s + ww[ii[i]], c_, s_)) return 1; return 0; } int main() { int n, h, i, c; long long lower, upper, cs, s; scanf("%d%d", &n, &k_); for (i = 0; i < n; i++) { scanf("%d%d", &ww[i], &tt[i]); ii[i] = i; } compare = compare_t, sort(ii, 0, n); for (c = 0; c <= n; c++) cc_[n][c] = c; for (i = n - 1; i >= 0; i--) for (c = 0; c <= n; c++) { cc_[i][c] = cc_[i + 1][c], ss_[i][c] = ss_[i + 1][c]; if (tt[ii[i]] > c) { int c_ = cc_[i + 1][c + 1]; long long s_ = ss_[i + 1][c + 1] + ww[ii[i]]; if (cc_[i][c] < c_ || cc_[i][c] == c_ && ss_[i][c] > s_) cc_[i][c] = c_, ss_[i][c] = s_; } } lower = -1, upper = (n + 2) * S; while (upper - lower > 1) { cs = (lower + upper) / 2, c = n - cs / S, s = cs % S; k = 0; if (branch(n, 0, 0, 0, c, s)) upper = cs; else lower = cs; } if (lower >= 0) { cs = lower, c = n - cs / S, s = cs % S; k = 0, branch(n, 0, 0, 0, c, s); } cs = upper, c = n - cs / S, s = cs % S; while (k < k_) cc[k] = c, ss[k] = s, k++; for (h = 0; h < k; h++) hh[h] = h; compare = compare_cs, sort(hh, 0, k); for (h = 0; h < k; h++) printf("%d %lld\n", cc[hh[h]], ss[hh[h]]); return 0; }

Compilation message (stderr)

Main.c: In function 'branch':
Main.c:52:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   52 |  if (cc_[i][c] < c_ || cc_[i][c] == c_ && s + ss_[i][c] > s_)
      |                        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.c: In function 'main':
Main.c:84:43: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   84 |     if (cc_[i][c] < c_ || cc_[i][c] == c_ && ss_[i][c] > s_)
      |                           ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
Main.c:69:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |  scanf("%d%d", &n, &k_);
      |  ^~~~~~~~~~~~~~~~~~~~~~
Main.c:71:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |   scanf("%d%d", &ww[i], &tt[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...