天梯赛 - L2-002 链表去重

GG思密达,第二个测试点的三分怎么也拿不上,我还是比较熟悉指针,用指针来写~,写完去上概率论

题目链接:https://www.patest.cn/contests/gplt/L2-002

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 1e5+5;
 7 struct _NODE_
 8 {
 9     int addr;
10     int key;
11     int abskey;
12     int Next;
13     _NODE_  *NextNode;
14 };
15 _NODE_ node[maxn];
16 int vis[maxn];
17 bool cmp(_NODE_ A,_NODE_ B)
18 {
19     return A.addr<B.addr;
20 }
21 _NODE_  *Head1 = NULL,*Travel1 = NULL;
22 _NODE_  *Head2 = NULL,*Travel2 = NULL;
23 void Print(_NODE_ *v1)
24 {
25     while(v1->NextNode!=NULL)
26     {
27         v1 = v1->NextNode;
28         if(v1->NextNode==NULL)
29         {
30             printf("%05d %d %d\n",v1->addr,v1->key,v1->Next);
31         }
32         else
33         {
34             printf("%05d %d %05d\n",v1->addr,v1->key,v1->Next);
35         }
36     }
37 }
38 int main()
39 {
40     int HeadAddr,n;
41     scanf("%d %d",&HeadAddr,&n);
42     for(int i=0;i<n;i++)
43     {
44         scanf("%d %d %d",&node[i].addr,&node[i].key,&node[i].Next);
45         node[i].abskey = abs(node[i].key);
46         node[i].NextNode = NULL;
47     }
48     sort(node,node+n,cmp);
49     int tt = n;
50     int NextAddr = HeadAddr;
51     Head1 = Travel1 = new _NODE_;
52     Head1->NextNode = NULL;
53     Head2 = Travel2 = new _NODE_;
54     Head2->NextNode = NULL;
55     while(tt--)
56     {
57         int l = 0;
58         int r = n-1;
59         while(l<=r)
60         {
61             int mid = (l+r)/2;
62             if(node[mid].addr<NextAddr)
63             {
64                 l = mid+1;
65             }
66             else if(node[mid].addr>NextAddr)
67             {
68                 r = mid;
69             }
70             else
71             {
72                 if(vis[node[mid].abskey])
73                 {
74                     Travel2->Next = node[mid].addr;
75                     Travel2->NextNode = &node[mid];
76                     Travel2 = Travel2->NextNode;
77                     NextAddr = node[mid].Next;
78                 }
79                 else
80                 {
81                     Travel1->Next = node[mid].addr;
82                     vis[node[mid].abskey] = 1;
83                     Travel1->NextNode = &node[mid];
84                     Travel1 = Travel1->NextNode;
85                     NextAddr = node[mid].Next;
86                 }
87                 break;
88             }
89         }
90     }
91     Travel1->Next = -1;
92     Travel2->Next = -1;
93     Print(Head1);
94     Print(Head2);
95     return 0;
96 }

 

转载于:https://www.cnblogs.com/littlepear/p/6489117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值