正睿七连测 DAY5 T2

题是水题,也不难想,本来是想打暴力先过个小数据,

本来就想再搞搞优化试试能不能过,毕竟这个题理论上 O( $n^2$ ) 是能过的

题干

主要是觉得这个优化很有可取之处,本来超时,一加这个优化就好很多了,一下就过了

改前的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define NUM 10010
using namespace std;
int n;
int a[NUM],dui[NUM];
struct dian{
    int l,r;
    char da;
};
dian ans[NUM];
int main(){
    cin >> n;
    for( int i = 1;i <= n;i++ ){
        int t;
        cin >> t;
        a[i] = t + '0';
        dui[t] = i;
    }
    ans[1].da = '1';ans[n].da = '1';
    if( abs( dui[1] - dui[2] ) == 1 ){
        ans[2].da = '1';
        ans[2].l = min( dui[1],dui[2] );
        ans[2].r = max( dui[1],dui[2] );
    }
    else ans[2].da = '0';
    for( int i = 3;i < n;i++ ){
        if( ans[i-1].da == '1' ){
            if( a[ans[i-1].l-1] == i+'0' ){
                ans[i].da = '1';
                ans[i].l = ans[i-1].l - 1;
                ans[i].r = ans[i-1].r;
                continue;
            }
            if( a[ans[i-1].r+1] == i+'0' ){
                ans[i].da = '1';
                ans[i].l = ans[i-1].l;
                ans[i].r = ans[i-1].r + 1;
                continue;
            }
            ans[i].da = '0';
            continue;
        }
        int ok = 1;
        for( int j = 1;j <= n-i+1;j++ ){
            int k;ok = 1;
            for( k = 0;k <= i-1;k++ ){
                if( a[k+j] > i+'0' ){
                    ok = 0;
                    break;
                }
            }
            if( ok ){
                ans[i].da = '1';
                ans[i].l = j;
                ans[i].r = j+i-1;
                ok = 2;
                break;
            }
        }
        if( ok == 2 ) continue;
        else ans[i].da = '0';
    }
    for( int i = 1;i <= n;i++ ){
        cout << ans[i].da;
    }
    return 0;
} 
View Code

 

改后的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define NUM 10010
using namespace std;
int n;
int a[NUM],dui[NUM];
struct dian{
    int l,r;
    char da;
};
dian ans[NUM];
int main(){
    cin >> n;
    for( int i = 1;i <= n;i++ ){
        int t;
        cin >> t;
        a[i] = t + '0';
        dui[t] = i;
    }
    ans[1].da = '1';ans[n].da = '1';
    if( abs( dui[1] - dui[2] ) == 1 ){
        ans[2].da = '1';
        ans[2].l = min( dui[1],dui[2] );
        ans[2].r = max( dui[1],dui[2] );
    }
    else ans[2].da = '0';
    for( int i = 3;i < n;i++ ){
        if( ans[i-1].da == '1' ){
            if( a[ans[i-1].l-1] == i+'0' ){
                ans[i].da = '1';
                ans[i].l = ans[i-1].l - 1;
                ans[i].r = ans[i-1].r;
                continue;
            }
            if( a[ans[i-1].r+1] == i+'0' ){
                ans[i].da = '1';
                ans[i].l = ans[i-1].l;
                ans[i].r = ans[i-1].r + 1;
                continue;
            }
            ans[i].da = '0';
            continue;
        }
        int ok = 1;
        for( int j = 1;j <= n-i+1; ){
            int k;ok = 1;
            for( k = 0;k <= i-1;k++ ){
                if( a[k+j] > i+'0' ){
                    ok = 0;
                    break;
                }
            }
            if( ok ){
                ans[i].da = '1';
                ans[i].l = j;
                ans[i].r = j+i-1;
                ok = 2;
break; } if( k > 0 ) j = k+j; else j++; } if( ok == 2 ) continue; else ans[i].da = '0'; } for( int i = 1;i <= n;i++ ){ cout << ans[i].da; } return 0; }

以后还可以用这个优化,大大滴好使  (*^▽^*)

posted @ 2021-10-02 19:47  little_sheep_xiaoen  阅读(31)  评论(0编辑  收藏  举报