input
第一行一个整数n。
第二行一个整数x。表示第一辆自行车的编号。
以下n-1行,每行3个整数x,y,z。
z=0时,表示编号为x的自行车恰停放在编号为y的自行车的左边
z=1时,表示编号为x的自行车恰停放在编号为y的自行车的右边
4
3
1 3 1
2 1 0
5 2 1
output
从左到右输出停车棚里的自行车编号
3 2 5 1
使用stl的链表确实比自己手写快一点
但是依然还是过不了的只有80分
#include<iostream>
#include<list>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N =100010;
int n;
int x,y,z;
list<int> l;
void print(){
list<int>::iterator it=l.begin();
for(;it!=l.end();it++){
printf("%d ",*it);
}
printf("\n");
}
int main(){
cin>>n;
scanf("%d",&x);
list<int>::iterator dic[N];
list<int>::iterator it ;
l.push_back(x);
n--;
while(n--){
scanf("%d%d%d",&x,&y,&z);
it=find(l.begin(),l.end(),y);
if(z)
it++;
l.insert(it,x);
}
print();
return 0;
}
从别人那儿剽窃回来的答案,妙啊!
#include<iostream>
#include<cstdio>
using namespace std;
const int N =100010;
struct Node{
int data;
Node *prior,*next;
};
int n,x,y,z;
int main(){
scanf("%d",&n);
scanf("%d",&x);
Node *first=new Node,*p=NULL,*s=NULL,*d[N];
first->data=x;
first->prior=NULL;
first->next=NULL;
d[x]=first;
n--;
while(n--){
scanf("%d%d%d",&x,&y,&z);
s= new Node;
s->data=x;
d[x]=s;
p=new Node;
p=d[y];//p=first;
/*定位
while(p!=NULL){
if(p->data!=y)
p=p->next;
else
break;
}
*/
if(z==0){
s->prior=p->prior;
s->next=p;
if(p->prior!=NULL)
p->prior->next=s;
p->prior=s;
if(p==first)
first=s;
}
else{
s->prior=p;
s->next=p->next;
if(p->next!=NULL)
p->next->prior=s;
p->next=s;
}
}
while(first!=NULL){
printf("%d ",first->data);
first=first->next;
}
return 0;
}