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$的字符串$ ...
随机推荐
- Spring整合hibernate -hibernateTemplate
目录 1 在Spring中初始化hibernateTemplate并注入Sessionfactory 2 DAO里注入hibernateTemplate 3 getHibernateTemplate. ...
- ci日志记录
log_message($level, $message) 参数: $level (string) -- Log level: 'error', 'debug' or 'info' $message ...
- 【Luogu】P3750分手是祝愿(期望DP)
题目链接 这题好喵啊…… 设f[i]是最少用i次才能全关上转移到最少用i-1次才能全关上灯的期望值,那么n个灯里有i个是正确的,剩下的都是不正确的 因此期望是$f[i]=frac{n}{i}+frac ...
- BZOJ3997 [TJOI2015]组合数学 【Dilworth定理】
题目 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走多少 ...
- css3上下翻页效果
翻页效果显示当前时间 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---32
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 通过ansible一键部署集群ntp时间同步
环境准备 [root@server ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@server ~]# uname -r ...
- hdu 4523(大整数)
威威猫系列故事——过生日 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- LeetCode OJ-- Longest Palindromic Substring **
https://oj.leetcode.com/problems/longest-palindromic-substring/ 给一个string,求它的最长回文子串. 定义一个二维数组bool is ...
- springBoot 打成jar包
1.一种方式通过cmd 窗口进行打包 配置maven 环境变量 进入到项目中 运行 mvn clean 然后运行mvn package 进行打包 2.通过idea 进行打包 (1)pom 中安装打包 ...