一元多项式的加法 码蹄集

题目来源:码蹄集

题目描述:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

C++代码实现:

参考链接:https://yxsmarter.blog.csdn.net/article/details/128211350?spm=1001.2014.3001.5502

#include <cstdio>
#include <algorithm>
using namespace std;

int n, m;
struct chj {
    int c, e;
    bool operator <(const chj b)const {
        return e < b.e;
    }
} a[2000005];

int main() {
    scanf("%d%d", &n, &m);
    n += m;
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &a[i].c, &a[i].e);
    }

    sort(a + 1, a + n + 1);

    int i = 1;
    while (i <= n) {
        int j = i + 1;
        while (a[i].e == a[j].e) {
            a[i].c += a[j].c;
            j++;
        }
        if (a[i].c != 0) {
            printf("%d %d\n", a[i].c, a[i].e);
        }
        i = j;
    }
    return 0;
}

参考B站老师:https://www.bilibili.com/video/BV1Ua4y1V7qX/?t=2708.0&vd_source=3ae2a916df1bc5c1114c2bf3e95a2118

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

const int N = 2e6 + 7;

struct NODE {
    ll nex, coef, expn;
} node[N];

int n, m, head, tail, pos;
ll coefA[N], expnA[N], coefB[N], expnB[N];

void insert(int curr, ll val1, ll val2) {
    node[++pos].coef = val1;
    node[pos].expn = val2;
    node[pos].nex = node[curr].nex;
    node[curr].nex = pos;
    if (!node[pos].nex)
        tail = pos;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%lld%lld", &coefA[i], &expnA[i]);
    for (int i = 1; i <= m; i++)
        scanf("%lld%lld", &coefB[i], &expnB[i]);

    int l = 1, r = 1;
    while (l <= n && r <= m) {
        if (expnA[l] == expnB[r]) {
            insert(tail, coefA[l] + coefB[r], expnA[l]);
            l++, r++;
        } else {
            if (expnA[l] < expnB[r]) {
                insert(tail, coefA[l], expnA[l]);
                l++;
            } else {
                insert(tail, coefB[r], expnB[r]);
                r++;
            }
        }
    }
    while (l <= n) {
        insert(tail, coefA[l], expnA[l]);
        l++;
    }
    while (r <= m) {
        insert(tail, coefB[r], expnB[r]);
        r++;
    }

    // output result
    for (int i = node[head].nex; i != 0; i = node[i].nex)
        if (node[i].coef != 0)
            printf("%lld %lld\n", node[i].coef,node[i].expn);
    return 0;
}

Python代码实现(会超内存):

import sys

n, m = map(int, sys.stdin.readline().split())
n += m
a = [(0, 0)] * n
for i in range(n):
    c, e = map(int, sys.stdin.readline().split())
    a[i] = (c, e)

a.sort(key=lambda x: x[1])
i = 0
while i < n:
    j = i + 1
    while j < n and a[i][1] == a[j][1]:
        a[i] = (a[i][0] + a[j][0], a[i][1])
        j += 1
    if a[i][0] != 0:
        print(a[i][0], a[i][1])
    i = j

Java代码实现(会超时):

import java.io.*;
import java.util.*;

class Main {
    static class chj implements Comparable<chj> {
        int c, e;

        public chj(int c, int e) {
            this.c = c;
            this.e = e;
        }

        @Override
        public int compareTo(chj o) {
            return Integer.compare(this.e, o.e);
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int n = Integer.parseInt(input[0]);
        int m = Integer.parseInt(input[1]);
        n += m;
        int[][] a = new int[n][2];
        for (int i = 0; i < n; i++) {
            input = br.readLine().split(" ");
            a[i][0] = Integer.parseInt(input[0]);
            a[i][1] = Integer.parseInt(input[1]);
        }
        Arrays.sort(a, Comparator.comparingInt(x -> x[1]));
        int i = 0;
        while (i < n) {
            int j = i + 1;
            while (j < n && a[i][1] == a[j][1]) {
                a[i][0] += a[j][0];
                j++;
            }
            if (a[i][0] != 0) {
                System.out.println(a[i][0] + " " + a[i][1]);
            }
            i = j;
        }
    }
}

代码提交测试结果:

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Magneto_万磁王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值