题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4415
用贪心来解,开始分为两个集合的方法错了,没有考虑之间的相互影响,正确的姿势应该是这样的,分两种情况考虑:
1.只考虑Bi全为0的集合,排个序,能取多少就取多少。
2.如果Bi不为0的集合中的Ai的最小值low如果大于m,那么就到了 1 的情况。否则两个集合放在一起排个序,Bi不为0的集合中至少选取low,刀的总数为sum,那么除去排序后sum个Ai值大的敌人,然后还剩下m-low个durability。用这m-low去消灭剩下的集合。
1 //STATUS:C++_AC_500MS_1012KB 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 //#include <ext/rope> 6 #include <fstream> 7 #include <sstream> 8 #include <iomanip> 9 #include <numeric> 10 #include <cstring> 11 #include <cassert> 12 #include <cstdio> 13 #include <string> 14 #include <vector> 15 #include <bitset> 16 #include <queue> 17 #include <stack> 18 #include <cmath> 19 #include <ctime> 20 #include <list> 21 #include <set> 22 #include <map> 23 using namespace std; 24 //using namespace __gnu_cxx; 25 //define 26 #define pii pair<int,int> 27 #define mem(a,b) memset(a,b,sizeof(a)) 28 #define lson l,mid,rt<<1 29 #define rson mid+1,r,rt<<1|1 30 #define PI acos(-1.0) 31 //typedef 32 typedef __int64 LL; 33 typedef unsigned __int64 ULL; 34 //const 35 const int N=100010; 36 const int INF=0x3f3f3f3f; 37 const int MOD=100000,STA=8000010; 38 const LL LNF=1LL<<60; 39 const double EPS=1e-8; 40 const double OO=1e15; 41 const int dx[4]={-1,0,1,0}; 42 const int dy[4]={0,1,0,-1}; 43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 44 //Daily Use ... 45 inline int sign(double x){return (x>EPS)-(x<-EPS);} 46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} 47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} 48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;} 49 template<class T> inline T Min(T a,T b){return a<b?a:b;} 50 template<class T> inline T Max(T a,T b){return a>b?a:b;} 51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} 52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} 53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} 54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} 55 //End 56 57 struct Node{ 58 int a,b; 59 }p[N]; 60 int vis[N]; 61 int T,n,m; 62 63 int cmp(Node a,Node b) 64 { 65 return a.a<b.a; 66 } 67 68 int main() 69 { 70 // freopen("in.txt","r",stdin); 71 int ca=1,i,j,a,b,sum,low,flag; 72 int cnt1,ans1,cnt2,ans2; 73 scanf("%d",&T); 74 while(T--) 75 { 76 low=INF; 77 cnt1=ans1=cnt2=ans2=sum=0; 78 scanf("%d%d",&n,&m); 79 for(i=0;i<n;i++){ 80 scanf("%d%d",&p[i].a,&p[i].b); 81 if(p[i].b)sum+=p[i].b; 82 } 83 sort(p,p+n,cmp); 84 for(i=0;i<n;i++){ 85 if(p[i].b)continue; 86 if(ans1+p[i].a>m)break; 87 ans1+=p[i].a; 88 cnt1++; 89 } 90 for(i=0;i<n;i++) 91 if(p[i].b){flag=i;low=p[i].a;break;} 92 if(low<=m){ 93 ans2=low; 94 if(sum+1>=n)cnt2=n; 95 else { 96 cnt2=sum+1; 97 n-=sum; 98 if(flag>=n)n--; 99 for(i=0;i<n;i++){ 100 if(i==flag)continue; 101 if(ans2+p[i].a>m)break; 102 ans2+=p[i].a; 103 cnt2++; 104 } 105 } 106 } 107 if(cnt2>cnt1 || (cnt2==cnt1 && ans2<ans1)){ 108 swap(ans1,ans2);swap(cnt1,cnt2); 109 } 110 111 printf("Case %d: %d %d ",ca++,cnt1,ans1); 112 } 113 return 0; 114 }