Codeforces 585D Lizard Era: Beginning | 折半搜索

参考这个博客

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
typedef long long ll;
using namespace std;
map< pair<int,int> , pair<int,int> > mp;
map< pair<int,int> , pair<int,int> > :: iterator it;
int ans=-92345678,n,a[30][4];
ll front,back;
vector <int> v;
void dfs1(int x,int l,int m,int w,int f)
{
    if (x>n/2)
    {
	it=mp.find(make_pair(l-m,w-m));
	if (it!=mp.end())
	{
	    if (l>it->second.first)
		mp[make_pair(l-m,w-m)]=make_pair(l,f);
	}
	else
	    mp[make_pair(l-m,w-m)]=make_pair(l,f);
	return ;
    }
    dfs1(x+1,l+a[x][1],m+a[x][2],w,f*3+0);
    dfs1(x+1,l+a[x][1],m,w+a[x][3],f*3+1);
    dfs1(x+1,l,m+a[x][2],w+a[x][3],f*3+2);
}
void dfs2(int x,int l,int m,int w,int f)
{
    if (x>n)
    {
	it=mp.find(make_pair(m-l,m-w));
	if (it!=mp.end())
	{
	    if (it->second.first+l>ans)
	    {
		ans=it->second.first+l;
		front=it->second.second;
		back=f;
	    }
	}
	return ;
    }
    dfs2(x+1,l+a[x][1],m+a[x][2],w,f*3+0);
    dfs2(x+1,l+a[x][1],m,w+a[x][3],f*3+1);
    dfs2(x+1,l,m+a[x][2],w+a[x][3],f*3+2);
}
void print(int x)
{
    if (!x) puts("LM");
    else if (x==1) puts("LW");
    else puts("MW");
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
	scanf("%d%d%d",&a[i][1],&a[i][2],&a[i][3]);
    }
    dfs1(1,0,0,0,0);
    dfs2(n/2+1,0,0,0,0);
    if (ans>-92345678)
    {
	for (int i=(n+1)/2;i>=1;i--)
	{
	    v.push_back(back%3);
	    back/=3;
	}
	for (int i=n/2;i>=1;i--)
	{
	    v.push_back(front%3);
	    front/=3;
	}
	for (int i=v.size()-1;i>=0;i--)
	    print(v[i]);
    }
    else puts("Impossible");
    return 0;
}

 

转载于:https://www.cnblogs.com/mrsheep/p/8039605.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值