我下载了测试点1,发现我的程序输出的是正确的,但不知道为啥交上去会TLE。。
代码:
#include<iostream>
#define N 200005
using namespace std;
int n, a, tot, las[N];
struct node{
int head, nxt, id, sum, l;
} lst[N];
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9'){
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x;
}
int main(){
int i, st = 1;
n = read();
for(i = 1; i <= n; i++){
a = read();
if(a != lst[tot].id){
lst[tot].nxt = ++tot, lst[tot].id = a, lst[tot].sum++;
lst[tot].l = i, lst[tot].head = tot - 1;
}
else lst[tot].sum++;
las[i] = i + 1;
}
lst[0].id = 2, lst[tot + 1].id = 3, lst[tot].nxt = tot + 1;
while(n > 0){
i = st;
while(!lst[i].sum) i = lst[i].nxt, st = i;
while(0 < i && i <= tot){
printf("%d ", lst[i].l);
lst[i].l = las[lst[i].l], lst[i].sum--, n--;
if(!lst[i].sum){
if(lst[lst[i].head].id == lst[lst[i].nxt].id){
las[lst[lst[i].head].l + lst[lst[i].head].sum - 1] = lst[lst[i].nxt].l + 1;
lst[lst[i].head].nxt = lst[lst[i].nxt].nxt;
lst[lst[i].head].sum += lst[lst[i].nxt].sum - 1;
lst[lst[lst[i].nxt].nxt].head = lst[i].head;
}
else lst[lst[i].head].nxt = lst[i].nxt, lst[lst[i].nxt].head = lst[i].head;//printf("lj: %d - %d\n", lst[i].head, lst[i].nxt);
}
i = lst[i].nxt;
}
printf("\n");
}
return 0;
}
第一次遇到这种情况,不知道为啥。。