poj1018解题报告

采用: 枚举+剪枝

诚实的说这题目还没有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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值