UVa 1149 Bin Packing (二分 + 贪心)
UVA 第一次用 不会用, 输出错误 竟然 发邮箱 , 好贴心的操作
贪心, 按照从小 到大排序, 然后 二分背包数目, 因为 背包 最大就是 n
所以
#include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <math.h> #include <cstring> #include <string> #include <queue> #include <stack> #include <stdlib.h> #include <list> #include <map> #include <set> #include <bitset> #include <vector> #define mem(a,b) memset(a,b,sizeof(a)) #define findx(x) lower_bound(b+1,b+1+bn,x)-b #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w",stdout) #define S1(n) scanf("%d",&n) #define SL1(n) scanf("%I64d",&n) #define S2(n,m) scanf("%d%d",&n,&m) #define SL2(n,m) scanf("%I64d%I64d",&n,&m) #define Pr(n) printf("%d\n",n) using namespace std; typedef long long ll; const double PI=acos(-1); const int INF=0x3f3f3f3f; const double esp=1e-6; const int maxn=1e6+5; const int MOD=1e9+7; const int mod=1e9+7; int dir[5][2]={0,1,0,-1,1,0,-1,0}; int a[maxn]; int b[maxn]; int n; int L; int judge(int x) { int k=1; mem(b,0); for(int i=n;i>=n-x+1;i--) b[k++]=a[i]; k--; for(int i=n-x;i>=1;i--) { if(a[i]+b[k--]>L) return 1; } return 0; } int main() { int t; cin>>t; while(t--) { cin>>n; mem(a,0); cin>>L; for(int i=1;i<=n;i++) { S1(a[i]); } sort(a+1,a+n+1); int l=1,r=n; while(r-l>1) { int mid=(l+r)>>1; if(judge(mid)) l=mid; else r=mid; } printf("%d\n",r); if(t>0) printf("\n"); } return 0; }
123
岂曰无衣?与子同袍。王于兴师,修我戈矛。与子同仇!
岂曰无衣?与子同泽。王于兴师,修我矛戟。与子偕作!
岂曰无衣?与子同裳。王于兴师,修我甲兵。与子偕行!