1002 A+B for Polynomials

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  4. const int N = 1005;
  5. double a[N];
  6. class Solution {
  7. public:
  8. //1,009,999
  9. void slove() {
  10. int m, x;
  11. double y;
  12. cin >> m;
  13. for (int i = 0; i < m; ++i) {
  14. cin >> x;
  15. cin >> y;
  16. a[x] += y;
  17. }
  18. cin >> m;
  19. for (int j = 0; j < m; ++j) {
  20. cin >> x;
  21. cin >> y;
  22. a[x] += y;
  23. }
  24. int count = 0;
  25. for (int k = 0; k < 1001; ++k) {
  26. if (a[k] != 0) {
  27. count += 1;
  28. }
  29. }
  30. cout << count;
  31. for (int l = 1001; l >= 0; l--) {
  32. if (a[l] != 0) {
  33. //cout << " " << l << " " << a[l];
  34. printf(" %d %.1f", l, a[l]);
  35. }
  36. }
  37. }
  38. };
  39. int main() {
  40. FIO;
  41. Solution sl;
  42. sl.slove();
  43. }