QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#328113#8005. Crossing the BorderDualqwqTL 2169ms53976kbC++202.3kb2024-02-15 17:20:252024-02-15 17:20:25

Judging History

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

  • [2024-02-15 17:20:25]
  • 评测
  • 测评结果:TL
  • 用时:2169ms
  • 内存:53976kb
  • [2024-02-15 17:20:25]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 23,Sz = 1 << 22,Sm = 1 << 12,P = 998244353;
int n,V;
struct node { int w,c;} p[N];
int sum[Sz],lb[Sz];
struct Data {
	int v,s;
	Data(){v = 1.5e9;s = 0;}
	Data(const int _v,const int _s):v(_v),s(_s){}
	Data operator + (const int &rhs) const { return Data(v + rhs,s);}
	Data operator * (const Data &rhs) const { 
		if(v < rhs.v) return *this;
		if(v > rhs.v) return rhs;
		return Data(v,(s + rhs.s) % P);
	}
};
Data dp[Sz];
vector<int> sp1[Sm],sp2[Sm];
int main() {
	cin >> n >> V;
	for(int i = 0;i < n;i++) cin >> p[i].w >> p[i].c;
	sort(p,p + n,[&](const node &a,const node &b) { return a.c > b.c;});
	int mid = n / 2,all = (1 << n);
	for(int i = 0;i < n;i++) sum[1 << i] = p[i].w;
	for(int i = 0;i < all;i++) sum[i] = sum[i & (i - 1)] + sum[i & (-i)];
	for(int i = 0;i < all;i++) lb[i] = __lg(i & (-i));
	// for(int i = 0;i < all;i++) if(sum[i] <= V) dp[i] = Data(p[lb[i]].c,1);
	for(int i = 0;i < (1 << mid);i++) {
		for(int j = i;j;j = (j - 1) & i)
			sp1[i].push_back(j);
		sp1[i].push_back(0);
		sort(sp1[i].begin(),sp1[i].end(),[&](const int &x,const int &y) { return sum[x] > sum[y];});
	}
	for(int i = 0;i < (1 << n - mid);i++) {
		for(int j = i;j < (1 << n - mid);j = (j + 1) | i)
			sp2[i].push_back(j << mid);
		sort(sp2[i].begin(),sp2[i].end(),[&](const int &x,const int &y) { return sum[x] > sum[y];});
	}
	dp[0] = Data(0,1);
	for(int i = 1;i < all;i++) 
		if(!(i & ((1 << mid) - 1)))
			for(int j = i;j;j = (j - 1) & i)
				if(((j >> lb[i]) & 1) && sum[j] <= V)
					dp[i] = dp[i] * (dp[i - j] + p[lb[i]].c);
	for(int i = 0;i < all;i++) {  // i 的左半边为等待更新的集合,i 的右半边为更新超集的集合
		int L = i & ((1 << mid) - 1),R = i >> mid;
		if(!L) continue;
		vector<Data> pmn(sp2[R].size()); // 可行的是一段后缀
		for(int p1 = 0,p2 = 0;p1 < sp1[L].size();++p1) {
			while(p2 < sp2[R].size() && sum[sp2[R][p2]] - sum[R << mid] + sum[L] - sum[sp1[L][p1]] > V) ++p2;
			if(p2 < sp2[R].size())
				pmn[p2] = pmn[p2] * dp[(R << mid) | sp1[L][p1]];
		}
		for(int i = 1;i < pmn.size();i++) pmn[i] = pmn[i] * pmn[i - 1];
		for(int i = 0;i < sp2[R].size();i++)
			dp[sp2[R][i] | L] = dp[sp2[R][i] | L] * (pmn[i] + p[lb[L]].c);
	}
	printf("%d\n%d\n",dp[all - 1].v,dp[all - 1].s);
	return 0;
}

详细

Test #1:

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

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: 136ms
memory: 38764kb

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: 834ms
memory: 45604kb

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: 2169ms
memory: 53976kb

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: