UVA - 10895 Matrix Transpose
UVA - 10895
Time Limit:3000MS | Memory Limit:Unknown | 64bit IO Format:%lld & %llu |
Description
A: Matrix Transpose |
A matrix is a rectangular array of elements, most commonly numbers. A matrix with
rows and
columns is said to be an
-by-
matrix. For example,
is a 4-by-3 matrix of integers.
The individual elements of a matrix are usually given lowercase symbols and are distinguished by subscripts. The
th row and
th column of matrix
is usually referred to as
. For example,
. Matrix subscripts are 1-based.
The transpose of a matrix , denoted
, is formed by interchanging the rows and columns of
. That is, the
th element of
is the
th element of
. For example, the transpose of matrix
above is:
A matrix is said to be sparse if there are relatively few non-zero elements. As a
-by-
matrix has number of elements, storing all elements of a large sparse matrix may be inefficient as there
would be many zeroes. There are a number of ways to represent sparse matrices, but essentially they are all the same: store only the non-zero elements of the matrix along with their row and column.
You are to write a program to output the transpose of a sparse matrix of integers.
Input
You are given several sparse matrix in a row, each of them described as follows. The first line of the input corresponds to the dimension of the matrix,
and
(which are the number of rows and columns, respectively, of the matrix). You are then given
sets of numbers, which represent the rows of the matrix. Each set consists of two lines which represents a
row of the matrix. The first line of a set starts with the number
, which is the number of non-zero elements in that row, followed by
numbers which correspond to the column indices of the non-zero elements in that row, in ascending order;
the second line has integers which are the matrix elements of that row. For example, matrix
above would have the following representation:
4 3
3 1 2 3
1 3 2
2 2 3
4 -1
0 3 1 2 3
5 -2 11
Note that for a row with all zero elements, the corresponding set would just be one number, `0', in the first line, followed by a blank line.
You may assume:
- the dimension of the sparse matrix would not exceed 10000-by-10000,
- the number of non-zero element would be no more than 1000,
- each element of the matrix would be in the range of -10000 to 10000, and
- each line has no more than 79 characters.
Output
For each input case, the transpose of the given matrix in the same representation.
Sample Input
4 3
3 1 2 3
1 3 2
2 2 3
4 -1
0 3 1 2 3
5 -2 11
Sample Output
3 4
2 1 4
1 5
3 1 2 4
3 4 -2
3 1 2 4
2 -1 11
题意:首先给你n*m的矩阵,然后给出每行的情况。
第一个数r代表该行有几个非0的数,位置是哪里,然后给出每一个位置的值,求矩阵的倒置
思路:用两个vector。一个记录该列有效值所相应的行,还一个记录该位置的值
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN = 10010; vector<int> row[MAXN];
vector<int> val[MAXN];
int n, m, arr[MAXN]; void print() {
printf("%d %d\n", m, n);
for (int i = 1; i <= m; i++) {
int len = row[i].size();
printf("%d", len);
for (int j = 0; j < len; j++)
printf(" %d", row[i][j]);
if (len == 0)
printf("\n\n");
else {
printf("\n%d", val[i][0]);
for (int j = 1; j < len; j++)
printf(" %d", val[i][j]);
printf("\n");
}
}
} int main() {
while (scanf("%d%d", &n ,&m) != EOF) {
for (int i = 0; i < MAXN; i++) {
row[i].clear();
val[i].clear();
}
int r, x;
for (int i = 1; i <= n; i++) {
scanf("%d", &r);
for (int j = 1; j <= r; j++)
scanf("%d", &arr[j]);
for (int j = 1; j <= r; j++) {
scanf("%d", &x);
row[arr[j]].push_back(i);
val[arr[j]].push_back(x);
}
}
print();
}
return 0;
}
UVA - 10895 Matrix Transpose的更多相关文章
- [题解]UVa 11082 Matrix Decompressing
开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...
- UVa 11082 Matrix Decompressing - 网络流
开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...
- UVa 11082 - Matrix Decompressing(最大流)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11019 Matrix Matcher(ac自动机)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
题意: 知道矩阵的前i行之和,和前j列之和(任意i和j都可以).求这个矩阵.每个格子中的元素必须在1~20之间.矩阵大小上限20*20. 思路: 这么也想不到用网络流解决,这个模型很不错.假设这个矩阵 ...
- UVa 11082 Matrix Decompressing(最大流)
不想吐槽了..sample input 和sample output 完全对不上...调了一个晚上...不想说什么了... -------------------------------------- ...
- UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error 输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中 ...
- UVA - 11082 Matrix Decompressing(最大流+行列模型)
题目大意:给出一个R行C列的矩阵,如今给出他的前1-R行和 && 前1-C列和,问这个矩阵原来是如何的,要求每一个元素大小在1-20之间 解题思路:将每一行连接到超级源点,容量为该行的 ...
- UVa 11019 Matrix Matcher - Hash
题目传送门 快速的vjudge传送门 快速的UVa传送门 题目大意 给定两个矩阵S和T,问T在S中出现了多少次. 不会AC自动机做法. 考虑一维的字符串Hash怎么做. 对于一个长度为$l$的字符串$ ...
随机推荐
- DOM的相关概念
[前面的话]DOM全称是Document Object Model,即文档对象模型.我们常说的html文档其实就是一个DOM树,DOM操作就是在内存中找到DOM树上我们想要的DOM对象,对它的属性进行 ...
- js获取触发事件的元素
//获取事件 var e = window.event; //获取元素 obj = e.target || e.srcElement; console.log(e); checkRepeat(e.ta ...
- Mysql升级ORACLE 记录
自增主键问题 php和mysql不写主键mysql可以自动生成主键; 想用pdo批量向mysql插入数据只能每条一个pdostarment->execute 看tp5.1的源码提供的方案是 IN ...
- 论文笔记(一)Re-ranking by Multi-feature Fusion with Diffusion for Image Retrieval
0x00 预备知识 $\DeclareMathOperator{\vol}{vol}$ 无向图上的随机游走 无向图 $G=(V,E)$,边权函数 $w\colon V\times V \to R_+$ ...
- SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from ...
- 查找String中出现最多字符的次数和个数
Sting 的charAt方法返回相应位置的字符,使用该方法遍历String,将每个字符存入对象属性,遍历属性得到最多字符个数 <!DOCTYPE html> <html> & ...
- 面试:ios 批量上传图片
这几天面试,被问到关于GCD的用法,想了想,之前项目好像确实用的比较少,只是知道怎么用,有思路,但是却从来没有试过,回来之后,就尝试写了下: 封装图片上传的方法 /**批量上传图片*/ + (NSUR ...
- poj 1795 DNA Laboratory
DNA Laboratory Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 2892 Accepted: 516 Des ...
- 只能运行一个程序,禁止运行多个相同的程序 C#
原文发布时间为:2009-04-06 -- 来源于本人的百度文章 [由搬家工具导入] Program.cs 里面改成如下: static void Main() { ...
- unbuntu 矫正电脑系统时间
sudo tzconfig,如果命令不存在请使用 dpkg-reconfigure tzdata