当前位置:网站首页>Codeforces 1637 B. mex and array - reading, violence

Codeforces 1637 B. mex and array - reading, violence

2022-06-12 13:32:00 Tianyi City*

This way

The question :

Give you a length of n Array of a. Ask you the value and maximum of all its sub segments .
value :
Divide an array into sections , The value of each interval is the length of the interval + In this range MEX. The value of an array is the sum of the values of all intervals .

Answer key :

this tnd I read the topic for half a day .
Since we need to find the maximum , Let's see if it's better to divide into multiple intervals , Let's let the MEX Bigger and better . for instance :
0 1 2 3 4
Does this look like an interval MEX The largest is probably the best ? The answer is interval numbers 1+ Section MEX5=6
What if an interval has only one number ? That is interval number 5+ Section MEX{1,0,0,0,0}=6
It looks the same .
Let's think about this problem : Suppose the current interval length is x, If you move one space to the right , Is it better to become a new section or let MEX May be +1 good .
We can find out ,MEX once +1, That means interval numbers don't +1, If the interval number +1,MEX Won't +1.
however MEX We have to verify it , Very tired, . So it's better to direct a number to an interval .

#include<bits/stdc++.h>
using namespace std;
const int N=105;
int a[N];
int main()
{
    
    int t;
    scanf("%d",&t);
    while(t--){
    
        int n,ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
    
            scanf("%d",&a[i]);
            int z=0;
            for(int j=i;j;j--){
    
                z+=a[j]==0;
                ans+=i-j+1+z;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

原网站

版权声明
本文为[Tianyi City*]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010516109552.html