2021 Spring Individual Competition-3 Supplement

D.XOR Permutations

The meaning of the question: Given three strings of length 10 consisting of 01, you can move the position of 01 arbitrarily to make their final XOR sum the largest.
Analysis: Count the number of 01 in 3 strings. When it encounters "001" and "111", it outputs 1; otherwise, it outputs 0. Need to sort every time.

Code:

#include<bits/stdc++.h>
using namespace std;
const int N=15;
int t;
char a[N],b[N],c[N];
int main(){
    
    
    cin>>t;
    while(t--){
    
    
        int a1=0,a0=0,b1=0,b0=0,c1=0,c0=0;
        cin>>a>>b>>c;
        for(int i=0;i<10;i++){
    
    
            if(a[i]=='1') a1++;
            else a0++;
        }
        for(int i=0;i<10;i++){
    
    
            if(b[i]=='1') b1++;
            else b0++;
        }
        for(int i=0;i<10;i++){
    
    
            if(c[i]=='1') c1++;
            else c0++;
        }
        for(int i=0;i<10;i++){
    
    
            if(a1<b1){
    
    
                swap(a1,b1);
                swap(a0,b0);
            }
            if(b1<c1){
    
    
                swap(b1,c1);
                swap(b0,c0);
            }
            if(a1<b1){
    
    
                swap(a1,b1);
                swap(a0,b0);
            }
            if(a1&&b0&&c0){
    
    
                a1--,b0--,c0--;
                cout<<"1";
            }
            else if(a1&&b1&&c1){
    
    
                a1--,b1--,c1--;
                cout<<"1";
            }
            else cout<<"0";
        }
        cout<<endl;
    }
}

K.Subarrays OR

Meaning of the title: Count the number of all the sums of all or operations in all intervals of the array
: violence + set optimization

Code:

#include<bits/stdc++.h>
using namespace std;
int t,n,a;
int main(){
    
    
    cin>>t;
    while(t--){
    
    
        set<int> s,res;
        cin>>n;
        for(int i=0;i<n;i++){
    
    
            set<int> now;
            cin>>a;
            for(auto i:s){
    
    
                res.insert(i|a);
                now.insert(i|a);
            }
            now.insert(a);
            res.insert(a);
            s=now;
        }
        cout<<res.size()<<endl;
    }
}

Guess you like

Origin blog.csdn.net/messywind/article/details/114659277