Matrix--集合--点二维排序

Description

To ecient calculate the multiplication of a sparse matrix is very useful in industrial  led. Let's consider
this problem:
A is an N*N matrix which only contains 0 or 1. And we want to know the result of A*(AT) .
Formally, we de ne B = A*(AT) , Aij is equal to 1 or 0, and we know the number of 1 in matrix A is M
and your task is to calculate B.

Input Description
The input contains several test cases. The first line of input contains a integer C indicating the number
of the cases.
For each test case, the first line contains two integer N and M.
and each of next M lines contains two integer X and Y , which means Axy is 1.
N <=100000, M <=1000, C <=10
Output Description
For each test case, it should have a integer W indicating how many element in Matrix B isn´t zero in one
line.
Sample Input
2
5 3
1 0
2 1
3 3
3 3
0 0
1 0
2 0
Sample Output
3
9
B=A*(AT).则Bij=A的第i行*AT的第j列。即A的第i行*A的第j行。题意是求矩阵B中非零的Bij有多少个吧。枚举每一个点去跟其他点乘,比如Ai1j1 和 Ai2j2,如果j1==j2.那么Bi1i2就非0.但是这个计数器是个问题啊。比如有a[i].y==a[j].y  还有一对点比如(x1,y1) (x2,y2)。。且(x1==a[i].x)(x2==a[j].x) y1==y2且不等于a[i].y..这样很容易就重复计数啊。也不能用bool型来标记啊。10W*10W的矩阵。果断会MLE啊
解决方法:
二维点排序   把所有的非0点拿粗来排序 排完之后只有相邻的才可能一样啊
也可以用集合来做,方便很多哦。。
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
struct Point
{
	int x,y;
}point[1008];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		set <pair<int,int>>st;
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&point[i].x,&point[i].y);
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(point[i].y==point[j].y)
				{
					st.insert(make_pair(point[i].x,point[j].x));
				}
			}
		}
		printf("%d\n",st.size());
	}
	return 0;
}

### C++ 中二维向量排序方法 对于二维向量而言,在C++中的处理方式可以视为一维向量的集合。当涉及到按照行或列进行升序列时,可以通过自定义比较函数来实现这一目标[^1]。 下面是一个按照每一行元素之和从小到大对整个矩阵(即二维向量)进行排序的例子: ```cpp #include <bits/stdc++.h> using namespace std; // 自定义比较器用于基于每行总和排序 bool compare(const vector<int>& v1, const vector<int>& v2) { int sum_v1 = accumulate(v1.begin(), v1.end(), 0); int sum_v2 = accumulate(v2.begin(), v2.end(), 0); return sum_v1 < sum_v2; } int main() { // 初始化一个随机大小为m*n的整数型二维向量 srand(time(0)); int m = rand()%5 + 3; // 行数范围设为[3,7] int n = rand()%5 + 3; // 列数范围同样设定为[3,7] cout << "Original Matrix:" << endl; vector<vector<int>> matrix(m, vector<int>(n)); for(int i=0;i<m;++i){ for(int j=0;j<n;++j){ matrix[i][j]=rand()%9+1;// 值域设置为[1,9] printf("%d ",matrix[i][j]); } puts(""); } sort(matrix.begin(), matrix.end(), compare); cout << "\nSorted Matrix based on Row Sum:" << endl; for(auto& row : matrix){ for(auto val : row) printf("%d ",val); puts(""); } return 0; } ``` 此程序首先创建了一个指定范围内尺寸变化的二维数组,并填充了一些介于特定区间内的伪随机数值作为初始状态展示给用户;接着通过`std::sort()`算法配合上述提到过的比较逻辑完成了最终的结果输出[^2]。 如果希望依据单个元素而非整体行来进行降序排序,则可以在原有基础上调整比较条件以及调用参数即可满足需求[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值