As I was going through problems with structs
on pbinfo.ro, I found a problem in which I'm given the dimensions of two matrices, the number of nonzero
elements for each matrix (N1, respectively N2) and then N1 + N2 groups of 3 numbers as follows:
- The coordinates, X and Y
- The value located at those coordinates
What I have to do is write a program that does the sum of these two matrices (they both have the same dimensions) and outputs the sum as given in the input (X, Y, and the value at the coordinates).
Here is the full text of the problem:
Matrice-Rara | www.pbinfo.ro
Requirement
Two rare matrices are given and you are asked to calculate their sum.
A matrix
A (n, m)
is called rare if most of its elements are equal to zero (at least half). Because of the small number of nonzero numbers, a rare matrixA (n, m)
withk
nonzero elements can be stored using an arrayX
containingk
triplets of form (line, column, value) corresponding to the nonzero values of the matrix. Elements of the arrayX
are stored in lexicographical order byline
andcolumn
.For example, the matrix with
n = m = 3
:1 0 2 0 0 5 0 2 0
will be saved in
X
this way:{(1,1,1), (1,3,2), (2,3,5), (3,2,2)}
.Input data
The input file
matrice_rara.in
contains on the first line the dimensions of the two matricesn m
, representing the number of lines and columns, andN1 N2
, the number of nonzero elements of matrixA
and matrixB
. Then the followingN1
lines will contain triplets for the matrixA
in lexicographical order, and the lastN2
lines will contain triplets representing the nonzero elements of matrixB
, also in lexicographical order.Output data
The output file
matrice_rara.out
will contain on the first line the number of nonzero elements in the matrixC
and then the matrix itself in the form of triplets in lexicographical order, one per line.Restrictions and clarifications
1 ≤ n, m ≤ 1,000,000
1 ≤ N1, N2 ≤ 300,000
-1,000,000,000 ≤ A[i][j], B[i][j] ≤ 1,000,000,000
Time Limit: 1 second
Memory Limit: 64 MB / 8 MB
What I tried is I read all the triplets in one array of triplets, sorted the array and then added the values of the elements that have the same coordinates into the first element in the array that has the repeating coordinates.
Here is my code:
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("matrice_rara.in");
ofstream cout("matrice_rara.out");
struct matrix
{
int coord1, coord2;
long long val;
} data[600001];
bool operator<(matrix const &a, matrix const &b)
{
if (a.coord1 == b.coord1) {
if (a.coord2 == b.coord2) {
return a.val < b.val;
}
return a.coord2 < b.coord2;
}
return a.coord1 < b.coord1;
}
int main()
{
int lin, col, unNul1, unNul2;
cin >> lin >> col >> unNul1 >> unNul2;
for (int i = 1; i <= unNul1 + unNul2; i++)
cin >> data[i].coord1 >> data[i].coord2 >> data[i].val;
sort(data + 1, data + unNul1 + unNul2 + 1);
int unNull = 0;
for (int i = 1; i <= unNul1 + unNul2; i++)
{
int start = i;
while (data[i + 1].coord1 == data[i].coord1 && data[i + 1].coord2 == data[i].coord2 && i + 1 <= unNul1 + unNul2)
i++, data[start].val += data[i].val;
if (data[start].val)
unNull++;
}
cout << unNull << "\n";
for (int i = 1; i <= unNul1 + unNul2; i++)
{
if (data[i].val)
cout << data[i].coord1 << " " << data[i].coord2 << " " << data[i].val << "\n";
while (data[i + 1].coord1 == data[i].coord1 && data[i + 1].coord2 == data[i].coord2 && i + 1 <= unNul1 + unNul2)
i++;
}
}
The code above gets 65 points out of 100, getting correct answers for 6 of the 9 tests, and Wrong Answer for the rest (no TLE).
Any help?