93pts,TLE on #12,求调,玄关
查看原帖
93pts,TLE on #12,求调,玄关
554576
yueluoxingchen楼主2024/8/7 11:12

rt

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<stack>
#include<queue>
#define debug() cout<<"qwq"<<endl
#define int long long
using namespace std;

const int N=25;
int w[N],a[N];
int n,W,res=0x3f3f3f;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

inline void write(int x)
{
    if (!x)
    {
        putchar('0');
        return;
    }
    int len = 0, k1 = x, c[40];
    if (k1 < 0) k1 = -k1, putchar('-');
    while (k1) c[len ++ ] = k1 % 10 ^ 48, k1 /= 10;
    while (len -- ) putchar(c[len]);
}

void dfs(int idx,int cnt)
{
	if(idx>res) return;
	if(cnt>n)
	{
		res=idx;
		return;
	}
	for(int i=1;i<=idx;i++)
	{
		if(a[i]+w[cnt]<=W)
		{
			a[i]+=w[cnt];
			dfs(idx,cnt+1);
			a[i]-=w[cnt];
		}
	}
	int x=idx+1;
	a[x]=w[cnt];
	dfs(x,cnt+1);
	a[x]=0;
}

signed main()
{
	n=read();
	W=read();
	for(int i=1;i<=n;i++) w[i]=read();
	dfs(1,1);
	write(res);
	return 0;
}
2024/8/7 11:12
加载中...