QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#331018#8005. Crossing the BorderjnzgTL 2357ms147356kbC++144.2kb2024-02-17 22:17:082024-02-17 22:17:08

Judging History

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

  • [2024-02-17 22:17:08]
  • 评测
  • 测评结果:TL
  • 用时:2357ms
  • 内存:147356kb
  • [2024-02-17 22:17:08]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define db double
#define ldb long double
#define lp(i, j, n) for(int i = j; i <= n; ++i)
#define dlp(i, n, j) for(int i = n; i >= j; --i)
#define mst(n, v) memset(n, v, sizeof(n))
#define mcy(n, v) memcpy(n, v, sizeof(v))
#define INF 1e18
#define MAX4 0x3f3f3f3f
#define MAX8 0x3f3f3f3f3f3f3f3f
#define pii pair<int, int>
#define pll pair<ll, ll>
#define co(x) cout << (x) << ' '
#define cod(x) cout << (x) << endl
#define lc(x) ((x) << 1)
#define rc(x) ((x) << 1 ^ 1)
#define ls ch[x][0]
#define rs ch[x][1]
#define fi first
#define se second
#define pb(x) emplace_back(x)

using namespace std;

const int N = 23, M = 12;
const ll MOD = 998244353;

struct node {
    int sum, num, cst;

    bool operator < (const node & t) const { return sum < t.sum; }
};

struct ipt { int a, b; } tmp[N];

int n, m, a[N], b[N], L, R;
int rsm[1 << M], lsm[1 << M];
int f[2][1 << M], g[2][1 << N];
node h[1 << M][1 << M]; int hn[1 << M];
int od[1 << M][1 << M], odn[1 << M];
int od2[1 << M][1 << M], od2n[1 << M], lg[1 << M];
/*
f[0/1][s]: 右边最小代价和方案数
h[s]: 为 s 时左边与(右边所有子集)按 sum{a} 排序后的代价前缀最小值,和对应方案数
g[0/1][s]: 为 s 时全局最小代价和方案数
lsm[s]: 左边为 s 时 a[i] 和
rsm[s]: 右边为 s 时 a[i] 和
*/

signed main() {
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
#ifndef READ
    ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cin >> n >> m;
    lp(i, 1, n) cin >> tmp[i].a >> tmp[i].b;
    ll t1 = clock();
    sort(tmp + 1, tmp + 1 + n, [&] (ipt t1, ipt t2) { return t1.b > t2.b; });
    lp(i, 1, n) a[i] = tmp[i].a, b[i] = tmp[i].b;
    L = n / 2, R = n - L;
    mst(f[0], 0x3f), mst(g[0], 0x3f), f[0][0] = 0, f[1][0] = 1;
    lp(i, 0, R - 1) lg[1 << i] = i;
    lp(s, 0, (1 << R) - 1) {
        lp(i, 1, R) rsm[s] += 1 << i - 1 & s ? a[L + i] : 0;
        for(int i = s; i; i = s & i - 1) {      // 转移 f
            if(rsm[i] <= m && (s & -s) == (i & -i)) {
                ll v = f[0][s ^ i] + b[L + lg[i & -i] + 1];
                if(v < f[0][s]) f[0][s] = v, f[1][s] = f[1][s ^ i];
                else if(v == f[0][s]) (f[1][s] += f[1][s ^ i]) %= MOD;
            }
        }
        g[0][s << L] = f[0][s], g[1][s << L] = f[1][s];
    }
    int kk = 0;
    lp(s, 0, (1 << L) - 1) {
        lp(i, 1, L) lsm[s] += 1 << i - 1 & s ? a[i] : 0;
        for(int i = s; i; i = s & i - 1) {
            if(lsm[i] <= m && (s & -s) == (i & -i)) od[s][++odn[s]] = i;
        }
        sort(od[s] + 1, od[s] + 1 + odn[s], [&] (int t1, int t2) { return lsm[t1] > lsm[t2]; });
        kk += odn[s];
    }
    lp(s, 0, (1 << R) - 1) {
        for(int i = s; ; i = s & i - 1) {
            if(rsm[i] <= m) od2[s][++od2n[s]] = i;
            if(!i) break;
        }
        sort(od2[s] + 1, od2[s] + 1 + od2n[s], [&] (int t1, int t2) { return rsm[t1] < rsm[t2]; });
    }
    lp(s2, 0, (1 << R) - 1) {       // 枚举右边
        lp(s, 0, (1 << L) - 1) {        // 枚举左边
            int S = s2 << L | s, j = 1;
            lp(k, 1, odn[s]) {      // 枚举左边子集并转移 g
                int i = od[s][k];
                while(j < hn[s ^ i] && h[s ^ i][j + 1].sum <= m - lsm[i]) ++j;
                ll v = h[s ^ i][j].cst + b[lg[i & -i] + 1];
                if(v < g[0][S]) g[0][S] = v, g[1][S] = h[s ^ i][j].num;
                else if(v == g[0][S]) (g[1][S] += h[s ^ i][j].num) %= MOD;
            }
            lp(k, 1, od2n[s2]) {     // 处理 h
                int i = od2[s2][k];
                h[s][++hn[s]] = { rsm[i], g[1][S ^ (i << L)], g[0][S ^ (i << L)] };
            }
            lp(i, 2, hn[s]) {    // 做代价前缀最小值并计算方案数前缀和
                if(h[s][i].cst > h[s][i - 1].cst) h[s][i].cst = h[s][i - 1].cst, h[s][i].num = h[s][i - 1].num;
                else if(h[s][i].cst == h[s][i - 1].cst) (h[s][i].num += h[s][i - 1].num) %= MOD;
            }
        }
        lp(s, 0, (1 << L) - 1) { hn[s] = 0; }
    }
    cout << g[0][(1 << n) - 1] << ' ' << g[1][(1 << n) - 1] % MOD << endl;
    // cod(clock() - t1);
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 38588kb

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: 113ms
memory: 82296kb

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: 837ms
memory: 125948kb

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: 2357ms
memory: 147356kb

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: