QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#465952#8005. Crossing the Borderucup-team052#TL 4283ms16144kbC++233.6kb2024-07-07 14:03:112024-07-07 14:03:11

Judging History

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

  • [2024-07-07 14:03:11]
  • 评测
  • 测评结果:TL
  • 用时:4283ms
  • 内存:16144kb
  • [2024-07-07 14:03:11]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
void print(vector<int> x){for(int i=0;i<(int)x.size();i++) printf("%d%c",x[i]," \n"[i==(int)x.size()-1]);}
inline int add(int x,int y) {return x+y>=mod?x+y-mod:x+y;}
inline int add(int x,int y,int z) {return add(add(x,y),z);}
inline int sub(int x,int y) {return x-y<0?x-y+mod:x-y;}
inline int mul(int x,int y) {return 1LL*x*y%mod;}
inline int mul(int x,int y,int z) {return mul(mul(x,y),z);}
#define inc(x,y) x=add(x,y)
#define dec(x,y) x=sub(x,y)
#define N 20
struct Node
{
	int w,c;
};
Node a[N+2];
bool cmp(Node x,Node y) {return x.c<y.c;}
int n,W,mn[1<<N],cnt[1<<N];
int sum[1<<N];
void bf()
{
	sort(a,a+n,cmp);
	for(int i=1;i<1<<n;i++)
	{
		int u=__builtin_ctz(i);
		sum[i]=sum[i^(1<<u)]+a[u].w;
	}
	mn[0]=0,cnt[0]=1;
	int ful=(1<<n)-1;
	for(int i=1;i<1<<n;i++)
	{
		mn[i]=inf;
		int u=31-__builtin_clz(i);
		for(int st=i;st>=(1<<u);st=i&(st-1))
		{
			if(sum[st]<=W)
			{
				if(mn[i^st]+a[u].c<mn[i]) mn[i]=mn[i^st]+a[u].c,cnt[i]=0;
				if(mn[i^st]+a[u].c==mn[i]) inc(cnt[i],cnt[i^st]);
			}
		}
	}
	printf("%d %d\n",mn[ful],cnt[ful]);
}
void solve()
{
	int l=20;
	sort(a,a+n,cmp);
	{
		int ss=0;
		for(int i=0;i<n;i++) ss+=a[i].w;
		if(ss<=W)
		{
			printf("%d %d\n",a[n-1].c,1);
			return ;
		}
	}
	for(int i=1;i<1<<l;i++)
	{
		int u=__builtin_ctz(i);
		sum[i]=sum[i^(1<<u)]+a[u].w;
	}
	mn[0]=0,cnt[0]=1;
	int ful=(1<<l)-1;
	for(int i=1;i<1<<l;i++)
	{
		mn[i]=inf;
		int u=31-__builtin_clz(i);
		for(int st=i;st>=(1<<u);st=i&(st-1))
		{
			if(sum[st]<=W)
			{
				if(mn[i^st]+a[u].c<mn[i]) mn[i]=mn[i^st]+a[u].c,cnt[i]=0;
				if(mn[i^st]+a[u].c==mn[i]) inc(cnt[i],cnt[i^st]);
			}
		}
	}
	// for(int i=1;i<1<<n;i++) printf("%d %d %d\n",i,mn[i],cnt[i]);
	// printf("%d %d\n",mn[ful],cnt[ful]);
	int amn=inf,acnt=0;
	if(n==21)
	{
		for(int i=0;i<1<<l;i++)
		{
			if(sum[i]+a[n-1].w<=W)
			{
				if(a[n-1].c+mn[i^ful]<amn) amn=a[n-1].c+mn[i^ful],acnt=0;
				if(a[n-1].c+mn[i^ful]==amn) inc(acnt,cnt[i^ful]);
			}
		}
	}
	else
	{
		for(int i=0;i<1<<l;i++)
		{
			if(sum[i]+a[n-1].w+a[n-2].w<=W)
			{
				if(a[n-1].c+mn[i^ful]<amn) amn=a[n-1].c+mn[i^ful],acnt=0;
				if(a[n-1].c+mn[i^ful]==amn) inc(acnt,cnt[i^ful]);
			}
		}
		for(int i=0;i<1<<l;i++)
		{
			int ok1=a[n-1].w+sum[i]<=W,ok2=a[n-2].w+sum[i]<=W;
			if(!ok1&&!ok2) continue;
			int r=ful^i;
			
			for(int j=r;j>i;j=r&(j-1))
			{
				int to1=a[n-1].w+sum[j]<=W,to2=a[n-2].w+sum[j]<=W;
				int cntok=(to1&&ok2)+(to2&&ok1);
				if(cntok==1)
				{
					if(a[n-1].c+a[n-2].c+mn[r^j]<amn) amn=a[n-1].c+a[n-2].c+mn[r^j],acnt=0;
					if(a[n-1].c+a[n-2].c+mn[r^j]==amn) inc(acnt,cnt[r^j]);
				}
				else if(cntok==2)
				{
					if(a[n-1].c+a[n-2].c+mn[r^j]<amn) amn=a[n-1].c+a[n-2].c+mn[r^j],acnt=0;
					if(a[n-1].c+a[n-2].c+mn[r^j]==amn) inc(acnt,add(cnt[r^j],cnt[r^j]));
				}
			}
		}
		int ful=(1<<l)-1;
		if(a[n-1].c+a[n-2].c+mn[ful]<amn) amn=a[n-1].c+a[n-2].c+mn[ful],acnt=0;
		if(a[n-1].c+a[n-2].c+mn[ful]==amn) inc(acnt,cnt[ful]);
	}
	printf("%d %d\n",amn,acnt);
}
signed main()
{
	cin>>n>>W;
	for(int i=0;i<n;i++) a[i].w=read(),a[i].c=read();
	if(n<=20) bf();
	else solve();
	
	return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 228ms
memory: 14768kb

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: 2272ms
memory: 16060kb

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: 2128ms
memory: 16124kb

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: 0
Accepted
time: 4283ms
memory: 16052kb

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:

8019829 516

result:

ok 2 number(s): "8019829 516"

Test #6:

score: 0
Accepted
time: 3428ms
memory: 16144kb

input:

22 50000000
9900000 50000000
9800000 50000000
9700000 50000000
9600000 50000000
9500000 50000000
9400000 50000000
9300000 50000000
9200000 50000000
9100000 50000000
9190000 50000000
9180000 50000000
9170000 50000000
9160000 50000000
9150000 50000000
9140000 50000000
9130000 50000000
9120000 50000000...

output:

250000000 659827482

result:

ok 2 number(s): "250000000 659827482"

Test #7:

score: 0
Accepted
time: 1428ms
memory: 16124kb

input:

22 49989999
9515787 13633636
7804670 14201704
4490825 15337840
10846676 15905908
4649834 16473976
13564408 17042044
26330177 17610112
31079612 18178180
9508811 18746248
11012937 19314316
9221231 19882384
35630511 20450452
33570222 21018520
33987302 22154656
28961982 22722724
29610359 23290792
342743...

output:

297099616 239005143

result:

ok 2 number(s): "297099616 239005143"

Test #8:

score: -100
Time Limit Exceeded

input:

22 49889999
4418358 4535448
7690530 4724425
3667793 4913402
8304776 5102379
2539846 5291356
2404119 5480333
2368750 5669310
3896314 5858287
6349833 6047264
10839169 6425218
10867512 6614195
9018761 6803172
5396983 6992149
2026994 7181126
6093366 7370103
10403853 7559080
5578332 7748057
13492459 7937...

output:


result: