当时第一次见这题的时候我不在现场,没听到题解,今天看到有人过,突然想起,然后闲的蛋疼,过来写一下,1A.
一开始很智障,想着贪心,但是其实这样没有意义,类比背包.
N<18,直接搜索咯.
好像有许多地方可以优化,有一组数据会被卡的很惨,但是懒得想咯,我们又菜又懒的咸鱼是这样的.
一如既往丑陋低效的搜索.
/* xzppp */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
const int MAXN = 10000+17;
const int MAXM = 20;
const int MAXV = 1001+17;
const int INF = 0x7fffffff;
const int MOD = 1e9+7;
int a[18+17],n,m,ans = INF;
void dfs(int p,int num,vector<int > all)
{
if(num>=ans) return;
for (int i = 0; i < all.size(); ++i)
{
if(all[i]>=a[p])
{
all[i]-=a[p];
if(p==n-1)
{
ans = min(ans,num);
return;
}
else
dfs(p+1,num,all);
all[i]+=a[p];
}
}
all.push_back(m-a[p]);
if(p==n-1)
{
ans = min(ans,num+1);
return;
}
else
dfs(p+1,num+1,all);
}
int main()
{
#ifndef ONLINE_JUDGE
FFF
#endif
cin>>n>>m;
for (int i = 0; i < n; ++i)
cin>>a[i];
vector<int > v;
dfs(0,0,v);
cout<<ans<<endl;
return 0;
}