2019 cattle off more school eighth field

A All-one Matrices

The meaning of problems

All great demand of the whole number of a matrix.

analysis

  • Pretreatment height value of each point extends downwardly, and then the same row pre-stack monotonically as the minimum height of each of the left and right boundaries can extend.
  • 1 point for each enumeration, first determine the height of the point about whether the section has been extended to cover the front cover over (ie have statistics), and if not, then the line is determined corresponding to whether the whole of this cover section 1, If it is, then that is already on line statistics, otherwise ++ to answer.
  • Plus each row is determined whether to repeat covered with a map, the complexity is probably \ (O (nmlog3000) \) .

Code

//
// Created by keane on 2019/8/10.
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3050;
int a[N][N];
int n,m;
char s[N];
//每个1向下延伸
int dw[N][N];
int pre[N][N];
//每个dw作为最小值延伸
int le[N][N],ri[N][N];
stack<int> ss;
map<pair<int,int>,bool> vis;
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        for(int j=1;j<=m;j++){
            a[i][j]=s[j]-'0';
            pre[i][j]=pre[i][j-1]+a[i][j];
        }
    }
    for(int i=n;i>=1;i--){
        for(int j=1;j<=m;j++){
            if(a[i][j]==0){
                continue;
            }
            if(a[i+1][j]==1){
                dw[i][j]=dw[i+1][j]+1;
            }else{
                dw[i][j]=1;
            }
        }
        while(!ss.empty()){
            ss.pop();
        }
        for(int j=1;j<=m;j++){
            while(ss.size()>0 && dw[i][j]<=dw[i][ss.top()]){
                ss.pop();
            }
            if(ss.size()>0){
                le[i][j]=ss.top()+1;
            }else{
                le[i][j]=1;
            }
            ss.push(j);
        }
        while(!ss.empty()){
            ss.pop();
        }
        for(int j=m;j>=1;j--){
            while(ss.size()>0 && dw[i][j]<=dw[i][ss.top()]){
                ss.pop();
            }
            if(ss.size()>0){
                ri[i][j]=ss.top()-1;
            }else{
                ri[i][j]=m;
            }
            ss.push(j);
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        vis.clear();
        for(int j=1;j<=m;j++){
            if(a[i][j]==0){
                continue;
            }
            int l=le[i][j];
            int r=ri[i][j];
            if(vis[{l,r}]){
                continue;
            }
            vis[{l,r}]=true;
            if(pre[i-1][r]-pre[i-1][l-1]!=r-l+1){
                ans++;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

B Beauty Values

The meaning of problems

Given a sequence of sections which require that all the different number and the number.

analysis

  • Apparently the problem is the number of statistics for each contribution, for an interval, each number is the first time my son certainly contribute to the answer, so consider the same number of separate into multiple sections, each of which is a record number the position of the first occurrence, then left for multiplying the current number of sides can be chosen to obtain the number of the counted number of the interval containing the number.
  • Sample example, \ ([1,2,1,3] \) , each contributing to the number of each interval \ ({1: [1,12,121,1213], 2: [12,121,1213,2, 21,213], 1: [21,213,1,13] 3: [1213,213,13,3]} \) .

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+50;
int n,a[N],p[N];
int main(void){
    // freopen("in.txt","r",stdin);
    scanf("%d",&n);
    ll ans=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        //每个数只在区间第一次出现才有贡献
        ans+=1ll*(i-p[a[i]])*(n-i+1);
        p[a[i]]=i;
    }
    printf("%lld\n",ans);
    return 0;
}

C CDMA

The meaning of problems

Given a \ (n-(n-K = 2 ^) \) , construct a matrix containing only 1 and -1 so that any two points by the row vector is zero.

analysis

  • n is a power of 2, suggesting the need for a similar ideological divide and conquer, constructed from the smallest matrix (2 * 2).
  • when n = 2, the matrix is \ ((\ the begin {smallmatrix}. 1 &. 1 \\. 1 & -1 \ End {smallmatrix}) \) , now configured matrix 4 * 4, it is clear matrix replication if we 2 * 2 again to the right, get two lines are clearly qualified.
  • Configuration of the lower half, the first half of the copy down, and then the right half negated, like this for the same portion of the upper and lower rows, left and right portions just a positive and negative, and 0, for different rows, if it is the same in the upper part or the lower part of the same, as we have determined upper part are satisfactory, and the lower half just copied, so it can meet the requirements, if it is the part that is different, since the submatrix (n / 2 * n / 2) has been satisfied, it will be adjusted to the same portion of the two rows, in fact, is divided into two left and right small dot matrix are 0's.
  • In fact xjb construction, recycling, invert, flip, and so give it a try, the 8 * 8 constructed generally on the right.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=2e3+50;
int n,a[N][N];
int main(void){
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    a[0][0]=1;
    a[0][1]=1;
    a[1][0]=1;
    a[1][1]=-1;
    for(int k=2;k<n;k<<=1){
        for(int i=0;i<k;i++){
            for(int j=0;j<k;j++){
//                a[i][k+j]=a[k-1-i][k-1-j];
                a[i][k+j]=a[i][j];
            }
        }
        for(int i=k;i<k+k;i++){
            for(int j=0;j<k;j++){
                a[i][j]=-1*a[i-k][j];
            }
            for(int j=k;j<k+k;j++){
                a[i][j]=a[i-k][j];
            }
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            printf("%d%c",a[i][j],j==n-1?'\n':' ');
        }
    }
    return 0;
}

G Gemstones

The meaning of problems

To a string, each character can be selected three consecutive identical erasing, erasing most often required.

analysis

Stack simulation.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+50;
char s[N];
stack<char> st;
int main(void){
    scanf("%s",s);
    int n=strlen(s);
    int ans=0;
    for(int i=0;i<n;i++){
        st.push(s[i]);
        if(st.size()<3){
            continue;
        }
        int a=st.top();
        st.pop();
        int b=st.top();
        st.pop();
        int c=st.top();
        st.pop();
        if(a!=b || b!=c){
            st.push(c);
            st.push(b);
            st.push(a);
        }else{
            ans++;
        }
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zxcoder/p/11332614.html