数据结构算法——1049. 三元组稀疏矩阵相加

题目

在这里插入图片描述

思路

真绷不住了 一开始用课本的十字链表超时,拿STLmap来储存超时,然后再试把所有数据先记录下来排序顺序输出也超时。我估计只有基数排序才能不超时了。

后续学了树再慢慢思考这题吧,本菜鸡学业不精

PS:突然想到这玩意输入的时候时有序的,可以用归并排序将排序降为O(n)的时间复杂度(然而最后一个点还是超时)

破案了,不要用cincout老老实实printf scanf

拉跨的代码

课本的十字表

#include<iostream>
using namespace std;

struct node
{
    int row,col,val;
    node *right, *down;
    node()
    {
        right = down = NULL;
        row = col = val = 0;
    }
};

struct table
{
    node *head;
    table(int m, int n)
    {
        head = new node;
        head->row = m;
        head->col = n;
        head->val = 0;
        head->right = head->down = head;

        node *record = head;
        node *temp;
        for(int i = 0; i < m; i++)//竖直部分
        {
            temp = new node;
            temp->col = n;
            temp->right = temp;
            temp->down = record->down;

            record->down = temp;
            record = temp;
        }
        record = head;
        for(int i = 0; i < n; i++)//横的部分
        {
            temp = new node;
            temp->row = m;
            temp->down = temp;
            temp->right = record->right;

            record->right = temp;
            record = temp;
        }
    }
    node* search_row_last(int n)
    {
        node* record = head;
        for(int i = 0; i <= n; i++)
            record = record->down;

        node* check = record;
        while(record->right != check)
            record = record->right;
        return record;
    }
    node* search_col_last(int n)
    {
        node* record = head;
        for(int i = 0; i <= n; i++)
            record = record->right;

        node* check = record;
        while(record->down != check)
            record = record->down;
        return record;
    }
    void insert(int row, int col, int val)
    {
        node *a = search_row_last(row);
        node *b = search_col_last(col);
        node* dot = new node;
        dot->row = row;
        dot->col = col;
        dot->val = val;
        dot->right = a->right;
        a->right = dot;

        dot->down = b->down;
        b->down = dot;
        head->val++;
    }
    void add(int row, int col, int val)
    {
        
    }

    table add(table B)
    {
        table c(head->row, head->col);
        node *p = head->down;
        node *u = B.head->down;

        while(p != head)
        {
            node *q = p->right;
            node *v = u->right;
            while(q != p || v != u)
            {
                if(q->col == v->col)
                {
                    if(q->val + v->val)
                        c.insert(q->row, q->col, q->val + v->val);
                    q = q->right;
                    v = v->right;
                }
                else if(q->col < v->col)
                {
                    c.insert(q->row, q->col, q->val);
                    q = q->right;
                }
                else
                {
                    c.insert(v->row, v->col, v->val);
                    v = v->right;
                }
            }
            p = p->down;
            u = u->down;
        } 
        return c;
    }

    void print()
    {
        node* row = head->down;
        while(row != head)
        {
            node* col = row->right;
            while(col != row)
            {
                cout << col->row << " " << col->col <<" " << col->val << endl;
                col = col->right;
            }

            row = row->down;
        }
    }
};

int main()
{
    int a,b,n;
    cin >> a >> b >> n;
    table A(a,b);
    for(int i = 0; i < n; i++)
    {
        int a,b,c;
        cin >> a >> b >> c;
        A.insert(a,b,c);
    }

    cin >> a >> b >> n;
    table B(a,b);
    for(int i = 0; i < n; i++)
    {
        int a,b,c;
        cin >> a >> b >> c;
        B.insert(a,b,c);
    }

    table c = A.add(B);
    c.print();
}

快排再顺序输出

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m,c;
    cin >> n >> m >> c;
    
    map<int,map<int,int>> ma;

    for(int i = 0; i < n; i++)
    {
        map<int,int>mm;
        map<int,int>p;
        ma[i] = p;
    }

    for(int i = 0; i < c; i++)
    {
        int x,y,d;
        cin >> x >> y >> d;
        ma[x][y] = d;
    }

    cin >> n >> m >> c;
    for(int i = 0; i < c; i++)
    {
        int x,y,d;
        cin >> x >> y >> d;
        if(ma[x][y])
            ma[x][y] += d;
        else
            ma[x][y] = d;
    }

    //cout << endl;
    
    {
        for(map<int,map<int,int>>::iterator it = ma.begin(); it != ma.end(); it++)
            for(map<int,int>::iterator iit = it->second.begin(); iit != it->second.end(); iit++)
            {
                if(iit->second == 0)
                    continue;
                if(iit->second)
                    cout << it->first << " " << iit->first << " " << iit->second << endl;
            }
        
    }
}

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

struct ele
{
    int x = 0,y = 0,d = 0;
};
struct arr
{
    ele* arr;
    int size = 0;
};

bool cmp(ele a, ele b)
{
    return (a.y < b.y);
}

int main()
{
    int n,m,c;
    cin >> n >> m >> c;
    arr* p;
    p = new arr[5000];

    for(int i = 0; i < n; i++)
    {   
        p[i].arr = new ele[5000];

        // for(int j = 0; j < m; j++)
        //     cout << (p[i]+j)->d << endl;
    }

    for(int i = 0; i < c; i++)
    {
        int x,y,d;
        cin >> x >> y >> d;
        p[x].arr[p[x].size].d = d;
        p[x].arr[p[x].size].y = y;
        p[x].size++;
    }

    cin >> n >> m >> c;
    for(int i = 0; i < c; i++)
    {
        int x,y,d;
        cin >> x >> y >> d;
        p[x].arr[p[x].size].d = d;
        p[x].arr[p[x].size].y = y;
        p[x].size++;
    }

    //cout << endl;
    for(int i = 0; i < n; i++)
    {
        sort(p[i].arr, p[i].arr + p[i].size, cmp);
        // for(int j = 0; j < p[i].size; j++)
        // {
        //     if(p[i].arr[j].d)
        //     {
        //         cout << i << " " << p[i].arr[j].y << " " << p[i].arr[j].d << endl;
        //     }
        // }
        //     cout << endl;

        for(int j = 0; j < p[i].size; j++)
        {
            if(j < p[i].size - 1)
            {
                if(p[i].arr[j].y == p[i].arr[j + 1].y)
                {
                    {cout << i << " " << p[i].arr[j].y << " " 
                    << p[i].arr[j].d + p[i].arr[j + 1].d << endl;}
                    j++;
                }
                else
                {
                    cout << i << " " << p[i].arr[j].y << " " << p[i].arr[j].d << endl;
                }
            }
            else
            {
                cout << i << " " << p[i].arr[j].y << " " << p[i].arr[j].d << endl;
            }
        }
    }
}

归并排序思路

(把cin cout换成printf scanf就能过了

#include<iostream>
using namespace std;

struct ele
{
    int x = 0,y = 0,d = 0;
};

int main()
{
    int n,m,c;
    cin >> n >> m >> c;
    ele *p  = new ele[2000000];
    for(int i = 0; i < c; i++)
    {
        cin >> p[i].x >> p[i].y >> p[i].d;
    }
    int size = c;
    cin >> n >> m >> c;

    int j = 0;
    for(int i = 0; i < c; i++)
    {
        int x,y,d;
        cin >> x >> y >> d;
        int flag = 1;
        while(j < size)
        {
            if(p[j].x > x)
            {
                cout << x << " " << y <<" "<< d << endl;
                flag = 0;
                break;
            }
            if(p[j].x == x)
            {
                if(p[j].y > y)
                {
                    cout << x << " " << y <<" "<< d << endl;flag = 0;
                    break;
                }
                else if(p[j].y == y)
                {
                    if(p[j].d + d)
                    {    
                        cout << x << " " << y <<" "<< p[j].d + d << endl;flag = 0;
                    }
                    j++;
                    break;
                }
                else if(p[j].y < y)
                    cout << p[j].x << " " << p[j].y << " " << p[j++].d << endl;
            }
            else if(p[j].x < x)
            {
                cout << p[j].x << " " << p[j].y << " " << p[j++].d << endl;
            }
        }
        if(flag) cout << x << " " << y <<" "<< d << endl;
    }
    while(j < size)
        cout << p[j].x << " " << p[j].y << " " << p[j++].d << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值