PAT 1133. 链表元素分类
原题链接
简单
作者:
YAX_AC
,
2024-11-26 21:17:57
,
所有人可见
,
阅读 3
//Splitting分裂,分割 rearrange重新安排 the negative values负值
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
const int N = 100010;
int n,k;
int h,e[N],ne[N];
int st[N];
int main()
{
cin>>h>>n>>k;
for(int i = 0; i<n; i++)
{
int address,data,next;
cin>>address>>data>>next;
e[address] = data,ne[address] = next;
}
vector<int> a,b,c;//负值,值大于k,正值
for(int i = h; i!=-1; i = ne[i])
{
int v = e[i];
if(v<0) a.push_back(i);
else if(v<=k) b.push_back(i);
else c.push_back(i);
}
a.insert(a.end(),b.begin(),b.end());
a.insert(a.end(),c.begin(),c.end());
for(int i = 0; i<a.size(); i ++)
{
printf("%05d %d ",a[i],e[a[i]]);
if(i+1 == a.size()) puts("-1");
else printf("%05d\n",a[i+1]);
}
return 0;
}