Coodeforces 585D Lizard Era: Beginning (折半搜索)

题目链接:Lizard Era: Beginning

题意:n此输入,每次三个元素,每次选择ab,ac,bc三种组合中一种,并把对应权值加给A,B,C,求最终组合中A==B==C且最大的方案

题解:直接搜索的话无疑是O(3^{n}),肯定是过不去的,因此可以选择折半搜索,复杂度降到O(3^{\frac{n}{2}}),具体操作如下:设上半部分最终搜索结果为x1.x2.x3,下半部分为y1,y2,y3,因为要求最终三者相等 

所以满足x1+y1==x2+y2==x3+y3;我们可以分项拆解后发现这个等式可以化成x1-x2==y2-y1,x1-x3==y3-y1.因此我们可以用一个pair来储存x1-x2,x1-x3,因为要求结果最大,我们可以用map映射pair,map储存对应的最大的x,在上半部分搜索中我们dfs查找并用map储存所有方案(相同结果的更新x为较大的),在下半部分就可以直接查找对应的上半部分(即满足等式x1-x2==y2-y1,x1-x3==y3-y1)并更新答案,在路径的储存中我们其实可以直接储存其对应的满三叉树叶子节点下标,因为我们知道树深度,所以可以不断取余确定走的是哪个分叉,储存下路径倒序输出即可

代码可能比较长(头文化长。。。。),但是注释很详细,而且写的简单易懂

#include<iostream>
#include<stack>
#include<list>
#include<set>
#include<vector>
#include<algorithm>
#include<math.h>
#include<numeric>
#include<map>
#include<cstring>
#include<queue>
#include<iomanip>
#include<cmath>
#include<queue>
#include <bitset>
#include<unordered_map>
	#ifndef local
	#define endl '\n'
#endif */
#define mkp make_pair
using namespace std;
using std::bitset;
typedef long long ll;
typedef long double ld;
const int inf=0x3f3f3f3f;
const ll MAXN=2e6+10;
const ll N=1e6+100;
const ll mod=1e9+7;
const ll hash_p1=1610612741;
const ll hash_p2=805306457;
const ll hash_p3=402653189;
//-----------------------------------------------------------------------------------------------------------------*/
// ll head[MAXN],net[MAXN],to[MAXN],edge[MAXN]/*流量*/,cost[MAXN]//费用;
/* 
void add(ll u,ll v,ll w,ll s){
	to[++cnt]=v;net[cnt]=head[u];edge[cnt]=w;cost[cnt]=s;head[u]=cnt;
	to[++cnt]=u;net[cnt]=head[v];edge[cnt]=0;cost[cnt]=-s;head[v]=cnt;
}
struct elemt{
	int p,v;
};
struct comp{
	public:
		bool operator()(elemt v1,elemt v2){
			return v1.v<v2.v;
		}
};
-----------------------------------
求[1,MAXN]组合式和逆元 
ll mi(ll a,ll b){
	ll res=1;
	while(b){
		if(b%2){
			res=res*a%mod;
		}	
		a=a*a%mod;
	}
	return res;
}
ll fac[MAXN],inv[MAXN]
fac[0]=1;inv[0]=1;
for(int i=1;i<=MAXN;i){
	fac[i]=(fac[i-1]*i)%mod;
	inv[i]=mi(fac[i],mod-2);
}
ll C(int m,int n){//组合式C(m,n); 
	if(!n){
		return 1;
	}
	return fac[m]*(inv[n]*inv[m*-n]%mod)%mod;
}
---------------------------------
 unordered_map<int,int>mp;
//优先队列默认小顶堆 , greater<int> --小顶堆  less<int> --大顶堆  
priority_queue<elemt,vector<elemt>,comp>q;
	set<int>::iterator it=st.begin();
*/
// vector<vector<int>>edge; 二维虚拟储存坐标 
//-----------------------------------------------------------------------------------------------------------------*/
map<pair<int,int>,int>mp; //pair三维分别为x1-x2,x1-x3,mp映射的是x1
map<pair<int,int>,int>idx; //这里映射的是对应满三叉树下标 
int l[30],m[30],w[30];
string s[3]={"LM","LW","MW"};//对应三种深搜选择
int ans=-1e9;//存储答案 
int ansx,ansy; //存储答案对应的两个三叉树下标,用于后面求路径 
void dfs1(int now,int end,int x,int y,int z,int rt){//xyz对应当前三者总和,rt对应三叉树节点下标 
	if(now>end){
		if(mp.count({x-y,x-z})){
			if(x>mp[{x-y,x-z}]){//取较大的一个 
				mp[{x-y,x-z}]=x;
				idx[{x-y,x-z}]=rt;
			} 
		}
		else{
			mp[{x-y,x-z}]=x;
			idx[{x-y,x-z}]=rt;
		} 
		return ;
	} 
	dfs1(now+1,end,x+l[now],y+m[now],z,3*rt);//对应s中三种状态
	dfs1(now+1,end,x+l[now],y,z+w[now],3*rt+1);
	dfs1(now+1,end,x,y+m[now],z+w[now],3*rt+2);
}
void dfs2(int now,int end,int x,int y,int z,int rt){//同上 
	if(now>end){//只有这里和上面不同,这里查找答案 
		if(mp.count({y-x,z-x})){//若有解 
			int tmp=mp[{y-x,z-x}];
			if(x+tmp>ans){//取最大的可行解 
				ans=x+tmp;
				ansx=idx[{y-x,z-x}];
				ansy=rt;
			}
		}
		return;
	}
	dfs2(now+1,end,x+l[now],y+m[now],z,3*rt);//对应s中三种状态
	dfs2(now+1,end,x+l[now],y,z+w[now],3*rt+1);
	dfs2(now+1,end,x,y+m[now],z+w[now],3*rt+2);
} 
vector<int>p;//储存路径的倒序 
void getload(int k,int len){//根据满三叉树性质,我们可以不断取余知道路径 
	for(int i=1;i<=len;i++){
		p.push_back(k%3);//具体走的哪个分叉 
		k/=3;
	}
}
int main(){
/*cout<<setiosflags(ios::fixed)<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(不含整数部分)*/
/*cout<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(含整数部分)*/
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//同步流
int n;
cin>>n;
for(int i=1;i<=n;i++){
	cin>>l[i]>>m[i]>>w[i];
}
int half=n/2;//分割点
dfs1(1,half,0,0,0,0);//搜上半部分并储存答案 
dfs2(half+1,n,0,0,0,0);//搜下半部分并求解答案
if(ans==-1e9){//无解 
	cout<<"Impossible"<<endl; 
}
else{
	getload(ansy,n-half);//先获取下半段路径 
	getload(ansx,half);//获取上半段路径 
	for(int i=p.size()-1;i>=0;i--){//倒序输出 
		cout<<s[p[i]]<<endl;
	}
}
	return 0;
}

如有不正希望大佬们指出

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值