连号区间数

超时,60分

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector> 
#include<string>
#include<queue>
#include<map>
using namespace std;
int a[50005];
int temp[50005];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    
    int ans=0;
    for(int i=0;i<n;i++)
    {
        int h=0;
        for(int j=i;j<n;j++)
        {
            if(j==i)
            {
                temp[h]=a[j];
                h++;
                ans++;
            }
            else
            {
                temp[h]=a[j];
                h++;
                sort(temp,temp+h);
                if(temp[h-1]-temp[0] == h-1)
                {
                    ans++;
                }
            }
        }
    }
    cout<<ans;
    return 0;
}

剪枝后80,

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector> 
#include<string>
#include<queue>
#include<map>
using namespace std;
int a[50005];
int temp[50005];
int maxn,minn;        //已经不会再出现的数中,最大的数和最小的数 
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    
    int ans=0;
    for(int i=0;i<n;i++)
    {
        int h=0;
        maxn = minn = -1;
        for(int j=i;j<n;j++)
        {
            if(j==i)
            {
                temp[h]=a[j];
                h++;
                ans++;
            }
            else
            {
                temp[h]=a[j];
                h++;
                sort(temp,temp+h);
                if(temp[h-1]-temp[0] == h-1)
                {
                    ans++;
                }
                //temp用来 存放从小到大排列的,可能再将来出现连续的一串数 
                if(temp[0]>maxn)    //但是如果temp中的最小值和最大值之间,存在已经不会再出现的数 ,比如a[0],就可以break 
                {
                    continue; 
                }
                if(temp[h-1]<minn)    
                {
                    continue;
                }
                break;
            }
            if(maxn == -1)    maxn=i;
            if(minn == -1)    minn=i;
            if(maxn < i)    maxn=i;
            if(minn > i)    minn=i;
        }
        
    }
    cout<<ans;
    return 0;
}

 

posted @ 2019-03-19 22:27  萌新上路  阅读(119)  评论(0编辑  收藏  举报