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;
}
比较基础的贪心题,不过很容易超时,我是采用计数排序的方式来查找。
#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;
}
相关文章推荐
- UVa - 1149 Bin Packing( 经典背包 贪心 )
- UVA 1149 bin packing (贪心)
- UVa 1149 Bin Packing (二分 + 贪心)
- Uva1149 Bin Packing【贪心】
- UVA 1149 - Bin Packing ( 贪心 + 二分 & STL )
- UVA 1149 Bin Packing 装箱 经典贪心 (SWERC 2005)
- uva 1149 Bin Packing(贪心)
- UVa 1149 - Bin Packing [贪心]
- UVA - 1149 Bin Packing 贪心水题
- UVa 1149 - Bin Packing [贪心]
- UVa 1149 Bin Packing 【贪心】
- UVa 1149 (贪心) Bin Packing
- UVA 1149 Bin Packing(贪心)
- UVA-1149 Bin Packing (贪心)
- 【uva】1149-Bin Packing(贪心 + STL )
- UVA 1149 Bin Packing 装箱(简单贪心)
- UVA 1149 Bin Packing 装箱(贪心)
- UVa 11292 - Dragon of Loowater(排序贪心)
- uva-10905Children's Game(贪心)
- uva 11292 The Dragon of Loowater(贪心)