uva 12124 - Assemble
最大值最小的题;
直接用二分,比较简单;
不过我的二分老是不用好。有时间总结一下!
1 #include<cstdio> 2 #include<map> 3 #include<vector> 4 #include<string> 5 #include<algorithm> 6 #define maxn 1006 7 using namespace std; 8 int t,n,b,x,y,cnt; 9 map<string,int>mp; 10 struct node 11 { 12 int p; 13 int v; 14 }; 15 vector<node>com[maxn]; 16 char s[23],ss[23]; 17 bool check(int a) 18 { 19 int sum=0; 20 for(int i=0;i<cnt;i++) 21 { 22 int cheap=b+1,m=com[i].size(); 23 for(int j=0;j<m;j++) 24 if(com[i][j].v>=a)cheap=min(cheap,com[i][j].p); 25 if(cheap==b+1)return 0; 26 sum+=cheap; 27 if(sum>b)return 0; 28 } 29 return 1; 30 } 31 32 int main() 33 { 34 scanf("%d",&t); 35 while(t--) 36 { 37 mp.clear(); 38 int l=0,r=0; 39 cnt=0; 40 scanf("%d%d",&n,&b); 41 for(int i=0;i<n;i++)com[i].clear(); 42 for(int i=0;i<n;i++) 43 { 44 scanf("%s%s%d%d",s,ss,&x,&y); 45 r=max(r,y); 46 if(!mp.count(s))mp[s]=cnt++; 47 com[mp[s]].push_back((node){x,y}); 48 } 49 while(l<r) 50 { 51 int mid=(l+r+1)>>1; 52 if(check(mid))l=mid; 53 else r=mid-1; 54 } 55 printf("%d\n",r); 56 } 57 return 0; 58 }
这种二分方法也行;
1 while(l<=r) 2 { 3 int mid=(l+r)>>1; 4 if(check(mid))l=mid+1; 5 else r=mid-1; 6 }