码迷,mamicode.com
首页 > 其他好文 > 详细

1097. Deduplication on a Linked List (25)

时间:2017-08-29 18:10:44      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:val   iostream   end   str   turn   nod   color   names   int   

Linkedlist的题目,多给一个index,然后根据要求排序,经常会有奇效。。。大神的思路果然厉害。。。

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
#include<stack>
using namespace std;
const int inf = 100001;

struct node {
    int val;
    int ad, next;
    int index=2*inf;
};

bool cmp1(node n1, node n2) {
    return n1.index < n2.index;
}

int main() {
    
    int root, num;
    cin >> root >> num;
    int r = root;
    node *arr = new node[100001];
    map<int, int> exist;
    for (int i = 0; i < num; i++) {
        int ad, val, next;
        cin >> ad >> val >> next;
        arr[ad].ad = ad;
        arr[ad].val = val;
        arr[ad].next = next;

    }
    int c1 = 0;
    int c2 = 0;
    for (int i = root; i !=-1; i=arr[i].next) {
        //cout << i << endl;
        if (exist[abs(arr[i].val)] == 0) {
            
            exist[abs(arr[i].val)] = 1;
            arr[i].index = c1;
            c1++;
        }

        else {
            arr[i].index = inf + c2;
            c2++;
        }

        
    }

    sort(arr, arr + inf, cmp1);
    int c = c1 + c2;
    for (int i = 0; i < c; i++) {
        if (i != c1 - 1 && i!=c-1)
            printf("%05d %d %05d\n", arr[i].ad, arr[i].val, arr[i + 1].ad);
        else
            printf("%05d %d -1\n", arr[i].ad, arr[i].val);
    }
    
    //int z = num - c;
    

    system("pause");
}

 

1097. Deduplication on a Linked List (25)

标签:val   iostream   end   str   turn   nod   color   names   int   

原文地址:http://www.cnblogs.com/wsggb123/p/7449409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!