最后两个tle,谁帮我看看那里可以优化一下,谢谢大佬!
查看原帖
最后两个tle,谁帮我看看那里可以优化一下,谢谢大佬!
1250563
Celestinefly楼主2024/4/5 11:08
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

const int N=1e6+10;

int a,b,p[N];

struct foma{
    int first,last,length;
}ma[N];

priority_queue<int,vector<int>,greater<int>>length;

queue <int> num;
int main(){
    scanf("%d %d",&a,&b);
    for(int i=1;i<=a;i++){
        scanf("%d",&p[i]);
    } 
    for(int i=1;i<=a;i++){
        int sta[b+1]={0};
        num.push(p[i]);
        sta[p[i]]=1;
        ma[i].first=i;
        for(int j=i+1;j<=a;j++){
            if(sta[p[j]]==0){
                num.push(p[j]);
                sta[p[j]]=1;
            }
            if((int)(num.size())==b){
                ma[i].last=j;
                ma[i].length=ma[i].last-ma[i].first+1;
                length.push(ma[i].length);
                break;
            }
        }
        num=queue<int>();
    }
    /*
    while(!length.empty()){
        cout<<length.top()<<endl;
        length.pop();
    }
    */
    for(int i=1;i<=a;i++){
        //检验:
        //printf("%d %d %d %d\n",i,ma[i].last,ma[i].length,length.top());
        if(ma[i].length==length.top()){
            printf("%d %d",i,ma[i].last);
            break;
        }
    }
    return 0;
}

2024/4/5 11:08
加载中...