2021 Spring Individual Tournament-8 Supplement

G.Magical Indices

Meaning of the title: count an array aaa elementsxxx满足:
1.1 < x < n 1.1<x<n 1.1<x<n
2. ay ≤ ax 2.a_y \ le a_x2.aandaxFor any 1 ≤ y <x 1 \le y <x1Y<x
3.ax ≤ az 3.a_x \ le a_z3.axafromFor any x <z ≤ n x<z\le nx<withn
analysis: O (n) O(n)O ( n ) Find each subscript corresponding to the maximum value of all previous numbers and the minimum value of all subsequent numbers, and then scan it at the end.

Code:

#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=1e6+5;
int t,n,a[N],pre[N],suf[N];
int main(){
    
    
    scanf("%d",&t);
    while(t--){
    
    
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        int res=0;
        pre[0]=a[0],suf[n-1]=a[n-1];
        for(int i=1;i<n;i++) pre[i]=max(pre[i-1],a[i]);
        for(int i=n-2;i>=0;i--) suf[i]=min(suf[i+1],a[i]);
        for(int i=1;i<n-1;i++){
    
    
            if(a[i]>=pre[i]&&a[i]<=suf[i]) res++;
        }
        printf("%d\n",res);
    }
}

J.The Hell Boy

Meaning: Given a length of nnarray of n aaa , Calculate the sum of the products of all sub-arrays and
analyze:
expansion1 11 ( x + 1 ) ( y + 1 ) = x y + x + y + 1 (x+1)(y+1)=xy+x+y+1 (x+1 ) ( and+1)=xy+x+Y+1
Expansion2 22 ( x + 1 ) ( y + 1 ) ( z + 1 ) = x y z + x y + y z + x z + x + y + z + 1 (x+1)(y+1)(z+1)=xyz+xy+yz+xz+x+y+z+1 (x+1 ) ( and+1 ) ( z+1)=xyz+xy+yz+xz+x+Y+with+1
According to the above two formulas, the sum of the products of all subsets is
(a 1 + 1) (a 2 + 1)… (an + 1) − 1 (a_1+1)(a_2+1)\cdots( a_n+1)-1(a1+1)(a2+1)(an+1)1

Code:

#include<stdio.h>
const int mod=1e9+7;
int t,n;
int main(){
    
    
    scanf("%d",&t);
    while(t--){
    
    
        long long res=0;
        scanf("%d",&n);
        for(int i=0,x;i<n;i++){
    
    
            scanf("%d",&x);
            res=(res+res*x+x)%mod;
        }
        printf("%lld\n",res);
    }
}

K.Palindromes Building

Meaning: Given a length of nnFor the string of n , find all its palindrome strings (arbitrary arrangement)
analysis:if there are two letters with an odd number of times, then it is impossible to form a palindrome, and output 0. In other cases, divide the length of this string by2 22. Just analyze half of the arrangement.
The number of permutations of all letters isn 2! \Frac{n}{2}!2n! , But there will be repeated letters, so divide the factorial of each letter.

Code:

#include<bits/stdc++.h>
using namespace std;
const int N=15;
int t,n,x[127],jc[10];
char a[N];
int main(){
    
    
    cin>>t;
    jc[0]=1;
    for(int i=1;i<=10;i++) jc[i]=i*jc[i-1];
    while(t--){
    
    
        memset(x,0,sizeof x);
        int flag=0;
        cin>>n>>a;
        for(int i=0;i<n;i++) x[a[i]]++;
        for(int i='a';i<='z';i++){
    
    
            if(x[i]!=0&&x[i]%2==1) flag++;
            if(flag==2) break;
            x[i]/=2;
        }
        if(flag==2) cout<<0<<endl;
        else{
    
    
            n/=2;
            int res=jc[n];
            for(int i='a';i<='z';i++){
    
    
                if(x[i]!=0) res/=jc[x[i]];
            }
            cout<<res<<endl;
        }
    }
}

Guess you like

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