1133 Splitting A Linked List (25分)

静态链表的题需要考虑有些输入的结点是不在由头结点引出的链表中的,因此需要注意输出的结点的数目可能跟输入的结点数不一致。如代码中注释的地方,修改将导致部分样例出错。

#include<cstdio>
#include<algorithm>
#include<vector>
#define maxn 1001000
using namespace std;

struct node {
    int address, data, type, place;
    node(int ad, int da, int ty, int pl): address(ad), data(da), type(ty), place(pl){}
};
int N, head, K;
int data[maxn], nex[maxn];
vector<node> ve;

void write_place(int head) {
    int i = 1;
    int temp = head;
    while(temp != -1) {
        int type = 0;
        if (data[temp] < 0) type = 1;
        else if (data[temp] <= K) type = 2;
        else type = 3;
        ve.push_back(node(temp, data[temp], type, i));
        i++;
        temp = nex[temp];
    }
    return;
}

bool compare(node a, node b) {
    if (a.type != b.type) return a.type < b.type;
    else return a.place < b.place;
}

int main() {
    scanf("%d %d %d", &head, &N, &K);
    int a, b, c;
    for (int j = 0; j < N; j++) {
        scanf("%d %d %d", &a, &b, &c);
        data[a] = b;
        nex[a] = c;
    }
    write_place(head);
    sort(ve.begin(), ve.end(), compare);
    int i;
    for (i = 0; i < ve.size() - 1; i++) {  // 换成i < N - 1就会在其中一个样例结果出错。
        printf("%05d %d %05d\n", ve[i].address, ve[i].data, ve[i + 1].address);
    }
    printf("%05d %d -1\n", ve[i].address, ve[i].data);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值