采用: 枚举+剪枝
诚实的说这题目还没有AC过,不过测试了很多都通过了,但就是AC不过。把源代码帖出来,希望能有朋友帮我找找问题所在。
通过这道题目我大致了解了枚举+剪枝的方式
#include<iostream>
#include<algorithm>
#include<map>
#include<iomanip>
#include<cstring>
using namespace std;
struct Node
{
Node(int b,int p,int cat);
int B;
int P;
int category;
};
Node::Node(int b = 0,int p = 0,int cat = 0)
{
this->B = b;
this->P = p;
this->category = cat;
}
/*
int cmp(const void* a,const void* b)
{
Node * x = (Node*)a;
Node * y = (Node*)b;
if(x->B == y->B)
{
if(x->P == y->P)
{
return (x->category - y->category);
}
return (x->P - y->P);
}
return (x->B - y->B);
}
*/
bool cmp(Node& a,Node& b)
{
if(a.B == b.B)
{
if(a.P == b.P)
{
return a.category < b.category;
}
return a.P < b.P;
}
return a.B < b.B;
}
float Max(float x,float y)
{
return (x>=y)?x:y;
}
int Min(int x,int y)
{
return (x<=y)?x:y;
}
int main()
{
int n=0, devices = 0,manufacturer = 0,bb = 0 , pp = 0;
cin>>n;
for(int c = 1; c <= n;c++)
{
Node dev[10000];
map<int,int> MaxB;
int total =0;
cin>>devices;
for(int i = 1 ; i <= devices ; i++)
{
cin>>manufacturer;
for(int j = 1; j <= manufacturer ; j++)
{
cin>>bb>>pp;
Node NodeTemp(bb,pp,i);
dev[++total] = NodeTemp;
MaxB[i] = Max(MaxB[i],bb);
}
}
//qsort(dev,total+1,sizeof(Node),cmp);
sort(dev+1,dev+total,cmp);
map<int,int> storage;
map<int,int>::iterator map_it;
float MAX = 0.0;
for(int i = 1; i <= total-(devices-1) ; i++)
{
storage.clear();
bool flag = false;
storage.insert(make_pair(dev[i].category,dev[i].P));
for(int k = i+1; k <= total ;k++)
{
if(dev[i].B > MaxB[dev[k].category])
{
flag = true;
break;
}
map_it = storage.find(dev[k].category);
if(map_it != storage.end() )
{
map_it->second = Min(map_it->second,dev[k].P);
}
else
{
storage.insert(make_pair(dev[k].category,dev[k].P));
}
}
if(flag || storage.size() < devices)
break;
int nPriceSum = 0;
for(map<int,int>::const_iterator it = storage.begin() ;it != storage.end(); it ++)
{
nPriceSum += it->second;
}
MAX = Max(MAX,(float)dev[i].B/nPriceSum);
}
cout<< setprecision(3)<<MAX<<endl;
}
//system("pause");
return 0;
}