1052 Linked List Sorting 25

打印 上一主题 下一主题

主题 897|帖子 897|积分 2691

 


  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. const int maxn = 100005;
  5. struct Node{
  6.     int address,data,next,flag;
  7. }nodes[maxn];
  8. bool cmp(Node a, Node b){
  9.     if(a.flag != b.flag) return a.flag > b.flag;
  10.     else return a.data < b.data;
  11. }
  12. int main() {
  13.     int n,head;
  14.     scanf("%d%d", &n, &head);
  15.     int tmp;
  16.     for(int i = 0; i < n; i++){
  17.         scanf("%d", &tmp);
  18.         scanf("%d%d", &nodes[tmp].data, &nodes[tmp].next);
  19.         nodes[tmp].address = tmp;
  20.         nodes[tmp].flag = 0;
  21.     }
  22.     int valid = 0;
  23.     int cur;
  24.     for(cur = head; cur != -1; cur=nodes[cur].next){
  25.         valid++;
  26.         nodes[cur].flag = 1;
  27.     }
  28.     if(valid == 0){
  29.         printf("0 -1\n");
  30.         return 0;
  31.     }
  32.     sort(nodes, nodes+maxn, cmp);
  33.     printf("%d %05d\n", valid, nodes[0].address);
  34.     for(int i = 0; i < valid; i++){
  35.         if(i != valid - 1){
  36.             printf("%05d %d %05d\n", nodes[i].address, nodes[i].data, nodes[i+1].address);
  37.         }else{
  38.             printf("%05d %d -1\n", nodes[i].address, nodes[i].data);
  39.         }
  40.     }
  41.     return 0;
  42. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

滴水恩情

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表