QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#401509#8005. Crossing the BorderMade_in_CodeTL 2423ms141600kbC++142.2kb2024-04-28 20:56:062024-04-28 20:56:06

Judging History

你现在查看的是最新测评结果

  • [2024-04-28 20:56:06]
  • 评测
  • 测评结果:TL
  • 用时:2423ms
  • 内存:141600kb
  • [2024-04-28 20:56:06]
  • 提交

answer

#include <algorithm>
#include <iostream>
#include <vector>
#define PII pair<int, int>

using namespace std;

const int kMaxN = 22, kInf = 2e9, kMod = 998244353;
int n, m, k, w[1 << kMaxN], d[1 << kMaxN];
PII a[kMaxN], f[1 << kMaxN];
vector<int> p[1 << kMaxN];

PII Add(PII x, int y) { return {x.first + y, x.second}; }

void Merge(PII &x, PII y) {
  if (x.first > y.first) {
    x = y;
  } else if (x.first == y.first) {
    x.second = (x.second + y.second) % kMod;
  }
}

bool C(int s, int t) { return (s & -s) ^ (t & -t); }

int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  m = n >> 1, sort(a, a + n, [](PII i, PII j) {
    return i.second > j.second;
  });
  for (int i = 0; i < n; i++) {
    w[1 << i] = a[i].first, d[1 << i] = a[i].second;
  }
  for (int s = 0; s < 1 << n; s++) {
    f[s] = {s ? kInf : 0, 1};
    if (s & s - 1 ^ s) {
      w[s] = w[s & s - 1] + w[s & -s];
      d[s] = d[s & -s];
    }
  }
  for (int s = 0; s < 1 << m; s++) {
    for (int t = s; t; t = t - 1 & s) {
      p[s].push_back(t - 1 & s);
    }
    sort(p[s].begin(), p[s].end(), [](int i, int j) {
      return w[i] > w[j];
    });
  }
  for (int s = 0; s < 1 << n - m; s++) {
    for (int t = s; t < 1 << n - m; t = t + 1 | s) {
      p[s << m].push_back(t << m);
    }
    sort(p[s << m].begin(), p[s << m].end(), [](int i, int j) {
      return w[i] > w[j];
    });
  }
  for (int s = 0; s < 1 << n; s++) {
    int l = s & ~(-1 << m), r = s >> m << m;
    pair<int, int> g = {kInf, 1};
    for (int i = 0, j = 0; j < p[r].size(); j++) {
      for (; l && i < p[l].size(); i++) {
        if (w[p[l][i]] + w[r] + k >= w[l] + w[p[r][j]]) {
          if (C(p[l][i], l)) {
            Merge(g, Add(f[p[l][i] | r], d[l]));
          }
        } else {
          break;
        }
      }
      if ((l | p[r][j]) == 4) {
        l++, l--;
      }
      Merge(f[l | p[r][j]], g);
      if (C(p[r][j], r) && w[r] + k >= w[p[r][j]]) {
        Merge(f[l | p[r][j]], Add(f[l | r], d[p[r][j]]));
      }
    }
  }
  cout << f[~(-1 << n)].first << ' ' << f[~(-1 << n)].second << '\n';
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 27ms
memory: 106700kb

input:

5 5
3 5
1 4
2 3
2 2
2 1

output:

9 4

result:

ok 2 number(s): "9 4"

Test #2:

score: 0
Accepted
time: 143ms
memory: 109424kb

input:

18 10000000
956231 904623
1692946 1796774
1081323 1170319
3218792 2542661
3183376 3037270
1869132 1442561
35436 35018
1564635 1939950
1847344 2006043
755870 899310
1671882 2057413
1369264 1338951
3132483 3504034
2056224 1825640
1840949 1562071
1514040 1405352
2300821 2421801
2466540 3004920

output:

9391997 70

result:

ok 2 number(s): "9391997 70"

Test #3:

score: 0
Accepted
time: 904ms
memory: 123412kb

input:

20 10000000
1289384 1416015
1692778 1966748
1747794 1708650
2885387 2925290
2516650 2410838
2202363 2092667
368691 407497
1897764 1902790
180541 224758
1089173 1075924
2005212 1743637
702568 566295
465783 369143
2722863 2902398
174068 150211
513930 519657
1634023 1313239
1133070 1040937
961394 11066...

output:

6331196 89

result:

ok 2 number(s): "6331196 89"

Test #4:

score: 0
Accepted
time: 2423ms
memory: 141600kb

input:

21 10000000
1432782 1230128
1693282 1456826
605524 521515
2742745 3427204
2231114 2129928
2345527 2397808
511783 521160
2041234 2313965
2323807 2603481
1232121 1410811
719508 850004
416942 495559
2180169 2579591
1580089 1786914
2317568 2292171
1514260 1143717
1348703 1495001
562052 525544
2818854 23...

output:

9336572 5

result:

ok 2 number(s): "9336572 5"

Test #5:

score: -100
Time Limit Exceeded

input:

22 10000000
1562592 1176882
1693226 1513484
2293770 2757728
2612851 3010518
1971354 2392268
2475363 2035487
641627 684375
2171036 2181775
1544541 1633457
1361981 1060447
2277948 2792254
157192 141039
1011327 1139897
541119 577682
1538276 1451191
2423314 2061841
1088919 1154927
42526 43789
1779858 16...

output:


result: