Pro
Codeforces Round #748 (Div. 3)
Sol
D2. Half of Same
I thought of the positive solution during the game, but it took more space than the positive solution, so I didn't write it out.
It's almost the same as D1, but it's not necessarily the smallest one. Therefore, the number of enumerations at each location should be the minimum.
Then subtract the minimum value from the numbers in other positions and put it into the B array. The answer is to find the maximum value of the common factor of all numbers with b > 0.
During the competition, I thought of saving the factors of each number, and then enumerating them from large to small to see if they have each.
But this will take a lot of space, so consider finding all the factors of a number and enumerating each factor.
For each factor, enumerate whether the numbers in other positions can be divided and counted. If greater than n 2 \frac{n}{2} 2n and ans take max.
F. Red-Black Number
It must be a search. Search the results of B and R in each position, and then judge whether the conditions are met.
I defined a variable d to represent the difference between the numbers of B and R, so D loops from 0 to n.
For each d, judge whether the final answer meets the requirements (i.e. divide A or divide B).
Of course, there is not only explosive search, but also pruning: memory search.
We note that when "the number of bits currently processed, the number composed of B, the number composed of R, and the number of B" are determined, a state is determined.
Because the quantity of R can be obtained from "number of bits currently processed" and "quantity of B". Therefore, the vis array can be used to determine whether the current state has been searched. vis is a four-dimensional array.
Simple proof: the latter state is only related to the "B" obtained from the previous state ® If equal, add "current digits" and "B" ® In fact, the back state has nothing to do with the front. The front is the same state, so you don't have to search for what you have searched. So there is memory search.
However, for each d, the vis array must be reset to 0, so this part is very time-consuming, especially the four-dimensional array.
Therefore, using the dynamic array to save the modified position, you only need to reset the points in the dynamic array each time, and then empty the dynamic array, which reduces a lot of time.
G. Changing Brackets
It seems to be prefix and, not yet, wait more
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; }