1133 Splitting A Linked List (25 分)

水~。

const int N=1e5+10;
struct Node
{
    int addr,data,nxt;
}a[N];
int head,n,k;

void add(vector<Node> &res,vector<Node> &v)
{
    for(int i=0;i<v.size();i++)
        res.pb(v[i]);
}

int main()
{
    cin>>head>>n>>k;

    for(int i=0;i<n;i++)
    {
        int addr,data,nxt;
        cin>>addr>>data>>nxt;
        a[addr]={addr,data,nxt};
    }

    vector<Node> neg,range,other;
    int p=head;
    while(p != -1)
    {
        if(a[p].data < 0) neg.pb(a[p]);
        else if(a[p].data >= 0 && a[p].data <= k)
            range.pb(a[p]);
        else
            other.pb(a[p]);
        p=a[p].nxt;
    }

    vector<Node> res;
    add(res,neg);
    add(res,range);
    add(res,other);

    for(int i=0;i<res.size();i++)
        if(i == res.size()-1)
            printf("%05d %d -1\n",res[i].addr,res[i].data);
        else
            printf("%05d %d %05d\n",res[i].addr,res[i].data,res[i+1].addr);
    //system("pause");
    return 0;
}

也可以使用insert函数。

    vector<Node> res;
    res.insert(res.end(),neg.begin(),neg.end());
    res.insert(res.end(),range.begin(),range.end());
    res.insert(res.end(),other.begin(),other.end());
posted @ 2021-03-03 16:52  Dazzling!  阅读(30)  评论(0编辑  收藏  举报