UVALive 3971-Assemble - 최소값 최대화(2점)
이분 품질 인자
매번 종류별 부품 중 품질 개선 인자 X보다 큰 가장 싼 부품을 선택하고, 합쳐서 b를 초과하지 않으면 가능하다. 그렇지 않으면 더욱 작은 품질 인자를 선택해야 한다.
코드:
ac 코드:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std;
const int inf =2147483640;
struct node
{
int q;
int p;
bool operator<(const node &b)const
{
return q<b.q;
}
};
int min(int a,int b)
{return a<b?a:b;}
map <string,int> uu;
vector<node> tm[1005];
vector<node>::iterator it;
node point[1005];
int cun=0;
int n,b;
int main()
{
int ok(int x);
int t;
cin>>t;
while(t--)
{
int i;
for (i=1;i<=n;i++)
tm[i].clear();
uu.clear();
scanf("%d%d",&n,&b);
char tmp[35];
char type[35];
int minn=inf;
int maxx=0-1;
cun=0;
for (i=1;i<=n;i++)
{
scanf("%s %s %d %d",type,tmp,&point[i].p,&point[i].q);
string ss=type;
if (uu.find(ss)==uu.end())
uu[type]=++cun;
int id=uu[type];
tm[id].push_back(point[i]);
if (point[i].q<minn)
minn=point[i].q;
if (point[i].q>maxx)
maxx=point[i].q;
}
for (i=1;i<=cun;i++)
sort(tm[i].begin(),tm[i].end());
int l=minn;
int r=maxx;
while(l<=r)
{
int mid=(l+r)/2;
if (r-l==0) //
break;
if (r-l==1)//2 1
{
if (!ok(r)) r=l;
break;
}
if (ok(mid))// ,
l=mid; //mid
else
r=mid-1; //mid
}
printf("%d
",r);
}
return 0;
}
int ok(int x)
{
int sum=0;
int i;
node tmp; tmp.q=x;
for (i=1;i<=cun;i++)
{
it=lower_bound(tm[i].begin(),tm[i].end(),tmp);
if (it==tm[i].end()) return 0; //**** X ***
int cheapest=it->p;
for (it;it!=tm[i].end();it++)
{
cheapest=min(cheapest,it->p);
}
sum+=cheapest;
}
if (sum>b)
return 0;
else
return 1;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.