【补题日记】CF#748 Div.3

Pro

Codeforces Round #748 (Div. 3)

Sol

D2. Half of Same

比赛的时候想到正解了,但是比正解花费的空间要大,所以没写出来。

和D1差不多,不过这里不一定是最小的最好了,所以要枚举每一个位置的数为最小值。

然后用其他位置的数字减去最小值放到b数组中,答案就是求所有b>0的数的共同的因子的最大值。

比赛的时候想到的是把每个数的因子存下来,然后从大到小枚举看看是不是每一个都有。

但是这样会花费很大的空间,所以考虑对一个数字求他的所有因子,然后枚举每个因子。

对于每个因子,枚举其他位置上的数字是否能够整除,计数,如果大于 n 2 \frac{n}{2} 2n就和ans取max。

F. Red-Black Number

肯定是搜索了,搜索每一个位置放B和R的结果,然后判断是否满足条件。

我是定义了一个变量d用来表示B和R数量的差值,所以d从0循环到n。

对于每一个d,判断最后的答案是否满足要求(即整除A或者整除B)。

当然,不仅仅是爆搜,还是有剪枝的:记忆化搜索。

我们注意到,当“当前处理的位数,B组成的数字,R组成的数字,B的数量”确定时,一个状态也就确定了。

因为R的数量可以由“当前处理的位数”和“B的数量”求得。所以可以用vis数组判断当前状态是否已经被搜过,vis是四维数组。

简单证明:后一个状态只与前面状态求出来的“B®组成的数字”有关,如果相等,再加上“当前位数”和“B®的数量”,其实后面的状态就和前面没有关系了,前面就是同一个状态,所以:搜过的就不用再搜了。所以有记忆化搜索。

不过对于每一个d都要重新将vis数组重置为0,所以这一部分是很耗费时间的,尤其是四维数组。

所以用动态数组存下修改过的位置,每次只需要把动态数组中的点重置,然后清空动态数组就减少了很多时间。

G. Changing Brackets

好像是前缀和,还不会,待更……

Code

D2. Half of Same

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define mk(_,__) make_pair(_,__)
#define pii pair<int,int>
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
#define debug ______
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 45;
int T,n,a[maxn],ans,b[maxn],c,d;

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>T;
	while(T--) {
		map<int,int>vis;
		cin>>n;
		map<int,int>res;
		Fo(i,1,n) {
			cin>>a[i];
			res[a[i]]++;
		}
		auto it = res.begin();
		int flag=0;
		while(it!=res.end()) {
			if((it->second)>=n/2) {
				flag=1;
				break;
			}
			it++;
		}
		if(flag) {
			cout<<"-1"<<endl;
			continue;
		}
	//	cout<<(it->first)<<" "<<(it->second)<<endl;
		ans=-1;
		Fo(i,1,n) {
			c=d=0;
			if(vis[a[i]]) continue;
			vis[a[i]]=1;
			Fo(j,1,n) {
				b[j]=a[j]-a[i];
				if(b[j]) c++;
				if(!b[j]) d++;
			}
			if(c<n/2-d) continue;
			map<int,int>mp;
			Fo(j,1,n) {
				vector<int>e;
				if(b[j]<=0) continue;
				if(mp[b[j]]) continue;
				mp[b[j]]=1;
				Fo(k,1,sqrt(b[j]))
					if(b[j]%k==0) {
						e.push_back(k);
						if(k!=sqrt(b[j]))
							e.push_back(b[j]/k);
					}
				sort(e.begin(),e.end());
				for(int k=e.size()-1; k>=0; k--) {
					int cnt=0;
					int debug = e[k];
					Fo(l,1,n) {
						if(b[l]<=0) continue;
						if(b[l]%e[k]==0) cnt++;
					}
					if(cnt+d>=n/2) {
						ans=max(ans,e[k]);
						break;
					}
				}
 			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

F. Red-Black Number

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define mk(_,__) make_pair(_,__)
#define pii pair<int,int>
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 45;
int T,n,A,B,flag;
struct Node {
	short a,b,c,d;
	Node(){};
	Node(short aa, short bb , short cc , short dd) {
		a=aa , b=bb , c=cc , d=dd;
	}
};
vector<Node>e;
short vis[maxn][maxn][maxn][maxn];
char a[maxn],ans[maxn];

void dfs(int x , int c1 , int c2 , int d , int s1 , int s2 , char ch[]) {
	//if(flag||abs(c1-c2)>d) return ;
	if(flag) return ;
	if(vis[x][s1][s2][c1]) return ;
	vis[x][s1][s2][c1] = 1;
	e.push_back(Node(x,s1,s2,c1));
	if(x==n+1) {
		if(abs(c1-c2)>d||!c1||!c2) return ;
		if(s1%A==0&&s2%B==0) {
			flag=1;
			Fo(i,1,n) cout<<ans[i];
			cout<<endl;
		}
		return ;
	}
	ch[x]='R';
	dfs(x+1,c1+1,c2,d,(s1*10+a[x]-'0')%A,s2%B,ch);
	ch[x]='B';
	dfs(x+1,c1,c2+1,d,s1%A,(s2*10+a[x]-'0')%B,ch);
	return ;
}

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>T;
	while(T--) {
	//	Ms(vis,0);
		cin>>n>>A>>B;
		Fo(i,1,n) cin>>a[i];
		flag=0;
		Fo(i,0,n) {
			for(int j=0; j<e.size(); j++)
				vis[e[j].a][e[j].b][e[j].c][e[j].d]=0;
			e.clear();
			//Ms(vis,0);
			dfs(1,0,0,i,0,0,ans);
			if(flag) break;
		}
		if(!flag) cout<<"-1"<<endl;
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cls1277

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值