QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#423058#8005. Crossing the BorderluanmengleiTL 1952ms27292kbC++172.7kb2024-05-27 20:56:582024-05-27 20:56:58

Judging History

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

  • [2024-05-27 20:56:58]
  • 评测
  • 测评结果:TL
  • 用时:1952ms
  • 内存:27292kb
  • [2024-05-27 20:56:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

namespace SOL {

using i64 = long long;
void debug(const char *msg, ...) {
#ifdef CLESIP
    va_list arg;
    static char pbString[512];
    va_start(arg,msg);
    vsprintf(pbString,msg,arg);
    cerr << "[DEBUG] " << pbString << "\n";
    va_end(arg);
#endif    
}
template<typename T, typename L>
bool chkmax(T &x, L y) { if (x < y) return x = y, true; return false; }
template<typename T, typename L>
bool chkmin(T &x, L y) { if (y < x) return x = y, true; return false; }

const int N = 22;
const i64 P = 998244353;
const i64 inf = 1e18;
int n, lim, sum[1 << N];
array<int, 2> a[N], tmp[1 << (N / 2 + 1)];

struct Node {
	i64 cost, cnt;
} f[1 << N], pre[1 << (N / 2 + 1)];

void mod(i64 &x, i64 y) {
	x += y;
	if (x >= P)
		x -= P;
}

void operator += (Node &lhs, const Node &rhs) {
	if (lhs.cost > rhs.cost) {
		lhs = rhs;
	} else if (lhs.cost == rhs.cost) {
		mod(lhs.cnt, rhs.cnt);
	}
}

Node add(const Node &o, int v) {
	return { o.cost + v, o.cnt };
}

void solve() {
	cin >> n >> lim;
	for (int i = 0; i < n; i ++)
		cin >> a[i][1] >> a[i][0];
	sort(a, a + n);
	for (int S = 1; S < (1 << n); S ++) {
		int k = __lg(S);
		sum[S] = sum[S - (1 << k)] + a[k][1];
		if (sum[S] > lim)
			sum[S] = lim + 1;
	}
	for (int S = 0; S < (1 << n); S ++)
		f[S] = { inf, 0 };
	f[0] = { 0, 1 };
	int n1 = (n + 1) / 2, n2 = n - n1;
	for (int S = 0; S + 1 < (1 << n); S ++) {
		int rem = ((1 << n) - 1) - S, T = S;
		int p = __lg(rem);
		T += (1 << p), rem -= (1 << p); 
		if (p < n1) {
			for (int nxt = rem; nxt; nxt = (nxt - 1) & rem)
				if (sum[T + nxt - S] <= lim)
					f[T + nxt] += add(f[S], a[p][0]);
			if (a[p][1] <= lim)
				f[T] += add(f[S], a[p][0]);
		} else {
			int tot = 0;
			int low = S & ((1 << n1) - 1), high = rem - (rem & ((1 << n1) - 1));
			for (int subset = low; subset; subset = (subset - 1) & low)
				tmp[++ tot] = { sum[low - subset], subset };
			tmp[++ tot] = { sum[low], 0 };
			sort(tmp + 1, tmp + 1 + tot);
			pre[0] = { inf, 0 };
			for (int i = 1; i <= tot; i ++) {
				pre[i] = pre[i - 1];
				pre[i] += f[tmp[i][1] + S - low];
			}
			
			for (int nxt = high; nxt >= 0; nxt = (nxt - 1) & high) if (sum[T + nxt - S] <= lim) {
				int pos = lower_bound(tmp + 1, tmp + 1 + tot, (array<int, 2>) { lim - sum[T + nxt - S] + 1, 0 }) - tmp - 1;
				// assert(pos + 1 > tot || tmp[pos + 1][0] > lim - sum[T + nxt - S]);
				f[nxt + T] += add(pre[pos], a[p][0]);
				if (nxt == 0)
					break;
			}
		}
	}
	auto ans = f[(1 << n) - 1];
	cout << ans.cost << " " << ans.cnt << "\n";
}

}
int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	SOL::solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5748kb

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: 277ms
memory: 11116kb

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: 1952ms
memory: 27292kb

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: -100
Time Limit Exceeded

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:


result: