题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184
第一次做链表题,有时间多看看
解释的话直接看这篇题解:https://www.liuchuo.net/archives/2125
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int inf=1<<30; 4 typedef long long ll; 5 typedef pair<int,int> P; 6 const double pi=acos(-1); 7 const int mod=1e8+7; 8 const int maxn=1e5+7; 9 const int maxm=6300; 10 struct node{ 11 int adress,val,next,num; 12 }a[maxn]; 13 bool exist[maxn]; 14 int cnt,cnt1,cnt2; 15 bool cmp(const node &a,const node &b){ 16 return a.num<b.num; 17 } 18 int main(){ 19 int begin,n;scanf("%d%d",&begin,&n); 20 for(int i=0;i<maxn;i++){ 21 a[i].num=2*maxn; 22 } 23 for(int i=0;i<n;i++){ 24 int x,y,z;scanf("%d",&x); 25 a[x].adress=x; 26 scanf("%d%d",&a[x].val,&a[x].next); 27 } 28 for(int i=begin;i!=-1;i=a[i].next){ 29 if(!exist[abs(a[i].val)]){ 30 exist[abs(a[i].val)]=1; 31 a[i].num=cnt1; 32 cnt1++; 33 } 34 else{ 35 a[i].num=cnt2+maxn; 36 cnt2++; 37 } 38 } 39 cnt=cnt1+cnt2; 40 sort(a,a+maxn,cmp); 41 for(int i=0;i<cnt;i++){ 42 if(i==cnt1-1||i==cnt-1) printf("%05d %d -1 ",a[i].adress,a[i].val); 43 else printf("%05d %d %05d ",a[i].adress,a[i].val,a[i+1].adress); 44 } 45 return 0; 46 }