试过了 但40分
  • 板块P1160 队列安排
  • 楼主Hewen_
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/10/29 15:18
  • 上次更新2023/10/27 05:10:28
查看原帖
试过了 但40分
801102
Hewen_楼主2022/10/29 15:18

Oh no我实在解决不了了……哪个大佬谁能管管初学者,看了下大家的讨论,40分或者说TLE的基本上都是时间复杂度的问题,那这个怎么优化啊喂!蒟蒻沉默……


#include<iostream>
using namespace std;
int n,x,k,p,m;

struct Node
{
	int ID;
	Node *next;
};

Node *h,*u,*r,*head;

void add(int i,int k,int p)
{
	Node *pre,*h,*s;
	
	h=head;
	
	if(p==0){
		while(h->ID!=k&&h->next!=NULL){
			pre=h;
			h=h->next;
		}
		
		s=new Node;
		s->ID=i;
		s->next=pre->next;
		pre->next=s;
	}
	
	if(p==1)
	{
		while(h->ID!=k&&h->next!=NULL){
			h=h->next;
		}
		
		s=new Node;
		s->ID=i;
		s->next=h->next;
		h->next=s;
	}
}

void del(int x)
{
	Node *pre,*h,*s;
	
	h=head->next;
	
	while(h->ID!=x&&h->next!=NULL){
		pre=h;
		h=h->next;}

	if(h->next!=NULL){
		pre->next=h->next;	
	}
	else{
		if(h->ID==x)
		pre->next=NULL;
	}

}

int main()
{
	cin>>n;
	
	head=new Node;
	head->ID=0;
	head->next=NULL;
	
	add(1,0,1);
	
	for(int i=2;i<=n;i++){
		cin>>k>>p;

		add(i,k,p);
	}
	
	cin>>m;
	
	for(int i=1;i<=m;i++){
		cin>>x;
		
		del(x);
	}
	
	h=head->next;
	while(h->next!=NULL){
		cout<<h->ID<<" ";
		h=h->next;
	}
	cout<<h->ID;
	
	return 0;
}
2022/10/29 15:18
加载中...