鱼C论坛

 找回密码
 立即注册
查看: 2312|回复: 0

[技术交流] PTA A_1097 Deduplication on a Linked List

[复制链接]
发表于 2020-2-6 12:52:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
传送门:https://pintia.cn/problem-sets/994805342720868352/problems/994805369774129152

解:
链表+排序+哈希
  1. #include<cstdio>
  2. #include<algorithm>

  3. using namespace std;

  4. typedef struct Node{
  5.     int add, key, next, rank;
  6. }node;

  7. node a[100010];
  8. bool is_duplicated[10010];

  9. bool cmp(node a, node b)
  10. {
  11.     return a.rank < b.rank;
  12. }

  13. int main(void)
  14. {
  15.     int head, n;
  16.    
  17.     scanf("%d %d", &head, &n);
  18.    
  19.     for (int i = 0; i < n; i++)
  20.     {
  21.         int add;
  22.         
  23.         scanf("%d", &add);
  24.         scanf("%d %d", &a[add].key, &a[add].next);
  25.         a[add].add = add;
  26.     }

  27.     int cnt = 0, rm_cnt = 0,  j = -n * 2;
  28.     for (int i = head; i != -1; i = a[i].next)
  29.     {
  30.         a[i].rank = j++;
  31.         
  32.         if (is_duplicated[abs(a[i].key)])
  33.         {
  34.             rm_cnt++;
  35.             a[i].rank += n;
  36.         }
  37.         else
  38.         {
  39.             cnt++;
  40.             is_duplicated[abs(a[i].key)] = true;   
  41.         }   
  42.     }
  43.    
  44.     sort(a, a + 100010, cmp);
  45.    
  46.     for (int i = 0; i < cnt - 1; i++)
  47.         printf("%05d %d %05d\n", a[i].add, a[i].key, a[i + 1].add);
  48.    
  49.     printf("%05d %d -1\n", a[cnt - 1].add, a[cnt - 1].key);
  50.    
  51.     if (rm_cnt)
  52.     {
  53.         int last = cnt + rm_cnt - 1;
  54.         
  55.         for (int i = cnt; i < last; i++)
  56.             printf("%05d %d %05d\n", a[i].add, a[i].key, a[i + 1].add);
  57.         
  58.         printf("%05d %d -1", a[last].add, a[last].key);        
  59.     }
  60.         
  61.     return 0;   
  62. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 14:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表