LA3971 Assemble
二分答案。
思想:二分枚举答案的可能解,在验算这个解是否满足条件
这题值得思考的地方就是,最后枚举出来的答案,是不是存在于给出的quality中?
#include <map> #include <cstdio> #include <vector> #include <string> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; struct node { int price; int q; }; vector<node>a[maxn]; map<string,int>mm; int n,b,cnt; bool ok(int s) { int sum = 0; for(int i = 1;i<=cnt;++i) { int len = a[i].size(); int minp = b+1; for(int j = 0;j<len;++j)if(a[i][j].q>=s) minp = min(minp,a[i][j].price); if(minp>b)return false; sum+=minp; if(sum>b)return false; } return true; } int main() { // freopen("in.txt","r",stdin); int T;scanf("%d",&T); while(T--) { mm.clear(); memset(a,0,sizeof(a)); scanf("%d%d",&n,&b); cnt = 0; int maxq = 0; for(int i = 1;i<=n;++i) { char type[30],name[30]; int p,q; scanf("%s%s%d%d",type,name,&p,&q); maxq = max(q,maxq); int x; if(!mm[type])mm[type] = ++cnt; x = mm[type]; a[x].push_back((node){p,q}); } int l = 0; while(l<maxq) { int m = (l+maxq)>>1; if(ok(m))l = m; else maxq = m-1; } printf("%d\n",l); } return 0; }
弱者究竟为何而战?!