您的位置:首页 > 其它

UVA 10162 Bin Packing(贪心)

2014-07-25 18:20 281 查看
解题思路:

   比较基础的贪心题,不过很容易超时,我是采用计数排序的方式来查找。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#define lson l , m , rt<<1
#define rson m+1 , r , rt<<1|1
using namespace std;
const int maxn = 10000 + 10;
int H[maxn];
int N , M;
int find(int x)
{
for(int i=x;i>0;i--)
{
if(H[i]) { H[i]-- ;return i;}
}
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(H,0,sizeof(H));
scanf("%d%d",&N,&M);
int tmp;
for(int i=1;i<=N;i++)
{
cin>>tmp;
H[tmp]++;
}
int ans = 0;
while(1)
{
int t = find(M);
if(!t) break;
ans ++;
int tmp = M - t;
t = find(tmp);
}
cout<<ans<<endl;
if(T>0) cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM UVA 贪心