求助ABC F
  • 板块学术版
  • 楼主快乐的大童
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/10/28 22:11
  • 上次更新2023/11/2 10:59:44
查看原帖
求助ABC F
448884
快乐的大童楼主2023/10/28 22:11

如题,虽经卡常得以AC,求助为什么我的一份复杂度正确的代码会跑的如此之慢。

思路是折半搜索,复杂度 O(2klog⁡k),k=n4O(2^k\log k),k=\frac{n}{4}。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define reprange(a,b,c,d) for(int a=b;a<=c;a+=d)
#define perrange(a,b,c,d) for(int a=b;a>=c;a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x);
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
bool smallingll(long long x,long long y){return x<y;}
inline int rd(){
	int x=0,f=1;int ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
const int maxn=1048576,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int N,n,m,X,Y,a[45],b[45];
bool okx,oky;
string wx,wy;
namespace X_coordinate{
	map<int,bool>vis;
	map<int,string>way;
	void dfs1(int step,int val,string sol){
		if(step==n/2+1){
			if(!vis[val])vis[val]=1,way[val]=sol;
			return;
		}
		dfs1(step+1,val+a[step],sol+"1");
		dfs1(step+1,val-a[step],sol+"3");
	}
	void dfs2(int step,int val,string sol){
		if(step==n+1){
			if(vis[X-val]){
				okx=1;
				wx=way[X-val]+sol;
			}
			return;
		}
		dfs2(step+1,val+a[step],sol+"1");
		dfs2(step+1,val-a[step],sol+"3");
	}
	void solve(){
		dfs1(1,0,"");dfs2(n/2+1,0,"");
	}
}
namespace Y_coordinate{
	map<int,bool>vis;
	map<int,string>way;
	void dfs1(int step,int val,string sol){
		if(step==m/2+1){
			if(!vis[val])vis[val]=1,way[val]=sol;
			return;
		}
		dfs1(step+1,val+b[step],sol+"0");
		dfs1(step+1,val-b[step],sol+"2");
	}
	void dfs2(int step,int val,string sol){
		if(step==m+1){
			if(vis[Y-val]){
				oky=1;
				wy=way[Y-val]+sol;
			}
			return;
		}
		dfs2(step+1,val+b[step],sol+"0");
		dfs2(step+1,val-b[step],sol+"2");
	}
	void solve(){
		dfs1(1,0,"");dfs2(m/2+1,0,"");
	}
}
void solve_the_problem(){
	N=rd(),X=rd(),Y=rd();
	rep(i,1,N){
		if(i&1)b[++m]=rd();
		else a[++n]=rd();
	}
	X_coordinate::solve();
	Y_coordinate::solve();
	if(!okx||!oky)PN;
	else{
		PY;
		int pos=1,l=0,ll=0;
		int lena=wy.size(),lenb=wx.size();
		reprange(qwq,1,N,2){
			if(l<lena){
				int nowpos=wy[l]-'0';
				if((pos+3)%4==nowpos)pc('L');
				else pc('R');
				pos=nowpos;
				l++;
			}
			if(ll<lenb){
				int nowpos=wx[ll]-'0';
				if((pos+3)%4==nowpos)pc('L');
				else pc('R');
				pos=nowpos;
				ll++;
			}
		}
	}
}
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
	int _=1;while(_--)solve_the_problem();
}
/*

*/

2023/10/28 22:11
加载中...