首页 > 其他 > 详细

连号区间数

时间:2019-03-19 22:34:26      阅读:241      评论:0      收藏:0      [点我收藏+]

超时,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;
}

 

连号区间数

原文:https://www.cnblogs.com/fzuhyj/p/10561941.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!