2020暑假杭电 Lead of Wisdom(数据离散化)

2020暑假杭电 Lead of Wisdom(数据离散化)

题目

http://acm.hdu.edu.cn/showproblem.php?pid=6772

题意

给n个装备信息,最多有k种装备。每个装备有a,b,c,d四种属性,每个类型的装备只能拥有一个,求:在这里插入图片描述

题解

可以直接bfs暴力,但是需要先数据离散化,把没有装备的类型去除掉。具体原因:
在这里插入图片描述

AC代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define maxn 100005
#define inf 2147483647
using namespace std;
typedef long long ll;
struct node
{
	ll a,b,c,d;
} ans[55][55];

ll A=100,B=100,C=100,D=100,res=0;
int n,k;
int tong[100];

void dfs(int x)
{
	if(x>k)
	{
		res = max(res,A*B*C*D);
		return;
	}
	for(int i=0; i<tong[x]; i++)
	{
		A+=ans[x][i].a;
		B+=ans[x][i].b;
		C+=ans[x][i].c;
		D+=ans[x][i].d;
		dfs(x+1);
		A-=ans[x][i].a;
		B-=ans[x][i].b;
		C-=ans[x][i].c;
		D-=ans[x][i].d;
	}
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		memset(tong,0,sizeof(tong));
		A=100,B=100,C=100,D=100,res=0;
		cin>>n>>k;
		for(int i=0; i<n; i++)
		{
			int t;
			scanf("%d",&t);
			scanf("%lld %lld %lld %lld",&ans[t][tong[t]].a,&ans[t][tong[t]].b,&ans[t][tong[t]].c,&ans[t][tong[t]].d);
			tong[t]++;
		}
		int cnt=0;
		for(int j=1; j<=k; j++)
			if(tong[j]!=0) cnt++;
		ll maxx = A*B*C*D;
		int sum = k;
		for(int i=1; i<=sum; i++)
		{
			if(tong[i]==0)
			{
				for(int j=i+1; j<=sum; j++)
				{
					if(tong[j]!=0)
					{
						for(int z=0; z<=tong[j]; z++)
						{
							ans[i][z]=ans[j][z];
						}
						tong[i] = tong[j];
						tong[j] = 0;
						break;
					}
				}
			}
		}
		k = cnt;
		dfs(1);
		printf("%lld\n",res);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43911945/article/details/107550301
今日推荐