Splitting A Linked List

版本1

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
struct node{
	int add,value,index,next,tag;
}E[maxn];
bool cmp(node a, node b){
	if(a.tag != b.tag) return a.tag < b.tag;
	else return a.index < b.index;

}

int main(){
	int p,n,add,value,next,t=0,k;
	cin>>p>>n>>k;
	for(int i=0;i<maxn;i++){
		E[i].index = maxn;
		E[i].tag = maxn;
	}
	for(int i=0;i<n;i++){
		cin>>add>>value>>next;
		E[add].add = add;
		E[add].value = value;
		E[add].next = next;
	}
	
	while(p!=-1){
		if(E[p].value < 0) E[p].tag=0;
		else if(E[p].value <= k) E[p].tag=1;
		else E[p].tag=2;
		E[p].index=t;
		p = E[p].next;
		t++; 
	}
	sort(E,E+maxn,cmp); 
	
	//输出 
	for(int i=0;i<t;i++){
		printf("%05d %d",E[i].add,E[i].value);
		if(i!=t-1) printf(" %05d\n",E[i+1].add);
		else printf(" -1\n");
	}
	return 0;
}