80TLE求助
查看原帖
80TLE求助
530500
Andy2035楼主2023/9/17 10:04

rt,#8#9 TLE 求优化

#include<bits/stdc++.h>
using namespace std;
int head = 1;
struct List{
    int col,l,sum;
    int L,R;
    List(){};
    List(int x,int y,int z,int w,int k):col(x),l(y),sum(z),L(w),R(k){};
}t[300010];
int tot;
void insert(int p,int k,int col,int l){
    int np = ++ tot;
    int R = t[p].R;
    t[p].R = np;
    t[np] = List(col,l,k,p,R);
    t[R].L = np;
}
void del(int p){
    if(p==head)head = t[p].R;
    int L = t[p].L,R = t[p].R;
    t[L].R = R;
    t[R].L = L;
}
const int N = 300010;
int n,a[N];
int main(){
    cin>>n;
    int l = 1,sum = 0,col = 2;
    for(int i = 1;i<=n;i++){
        scanf("%d",&a[i]);
        if(col==2)col = a[i];
        if(col!=a[i]){
            insert(tot,sum,col,l);
            sum = 0,col = a[i],l = i;
        }
        sum ++;
    }
    if(sum)insert(tot,sum,col,l);
    bool ok = true;
    while(ok){
        ok = false;
        for(int i = head;i;i = t[i].R){
            if(t[i].sum&&(i==head||t[t[i].L].col!=t[i].col)){
                ok = true;
                t[i].sum --;
                printf("%d ",t[i].l);
                t[i].l++;
            }
            if(i!=head&&(!t[t[i].L].sum))del(t[i].L);
        }
        if(ok)printf("\n");
    }
    return 0;
}
2023/9/17 10:04
加载中...