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$的字符串$ ...
随机推荐
- java-dispose方法
今天在编一道JAVA例题时,看到 dispose 这个方法,在程序中是用来关闭一个GUI页面的.这也让我想到了setVisible(false)方法.在网上查了意思,dispose()即释放所有本机屏 ...
- iOS属性文字NSAttributedString
它本身是一个Foundation框架的类, 但如果要使用它主要用到了UIKit框架中的NSAttributedString中的一些常量字符串 ----------------------------- ...
- JSP与JavaBeans
JavaBeans简介 JavaBeans是一种符合一定标准的普通java类,需要满足下面几点: 1 类是public 2 属性私有 3 空的public构造方法 4 通过getter setter操 ...
- NOIP 2016 滚粗记
Day -∞ 听说要去晋城一中去考试. MMP,我在省会城市,为什么要去一个偏远的小城市去考NOIP 就是因为几年前它们那里出了一个金牌吗?都怪我们太菜了. Day 0 坐着长途大巴车去考试,其他人都 ...
- bzoj2338数矩形(rectangle)
数矩形(rectangle) 计算几何 把所有点两两连线,把这些线按长度排序,再按中点排序 能组成矩形的线一定是连续的 最坏就是所有点围成一个圆,交于圆心 复杂度O(可以过) 要用叉积,不然会被卡精 ...
- 洛谷P1908 逆序对
P1908 逆序对 2.2K通过 4.4K提交 题目提供者该用户不存在 标签云端 难度普及/提高- 时空限制1s / 128MB 提交 讨论 题解 最新讨论更多讨论 归并排序党注意了!数组要开… ...
- SQL只获取字段中的中文字符
原文发布时间为:2010-10-28 -- 来源于本人的百度文章 [由搬家工具导入] 新建标量函数 set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER f ...
- .NET结束外部进程 C#结束外部进程
原文发布时间为:2011-02-15 -- 来源于本人的百度文章 [由搬家工具导入] using System;namespace ConsoleApplication2{ class Prog ...
- c language compile process.
- Linux 一个sysv 脚本参考模板
说明: 1.很多时候我们的服务都是通过源码包编译安装,但是有的源码包编译完成后并不提供该服务的sysv风格脚本,我们只能手动执其二进制程序+配置文件 2.如果服务器宕机或重启,就不能自动完 ...