UVA - 10895

Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu

[Submit]  [Go Back]  [Status]

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的更多相关文章

  1. [题解]UVa 11082 Matrix Decompressing

    开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...

  2. UVa 11082 Matrix Decompressing - 网络流

    开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...

  3. UVa 11082 - Matrix Decompressing(最大流)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVA 11019 Matrix Matcher(ac自动机)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)

    题意: 知道矩阵的前i行之和,和前j列之和(任意i和j都可以).求这个矩阵.每个格子中的元素必须在1~20之间.矩阵大小上限20*20. 思路: 这么也想不到用网络流解决,这个模型很不错.假设这个矩阵 ...

  6. UVa 11082 Matrix Decompressing(最大流)

    不想吐槽了..sample input 和sample output 完全对不上...调了一个晚上...不想说什么了... -------------------------------------- ...

  7. UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)

    意甲冠军  由于矩阵乘法计算链表达的数量,需要的计算  后的电流等于行的矩阵的矩阵的列数  他们乘足够的人才  非法输出error 输入是严格合法的  即使仅仅有两个相乘也会用括号括起来  并且括号中 ...

  8. UVA - 11082 Matrix Decompressing(最大流+行列模型)

    题目大意:给出一个R行C列的矩阵,如今给出他的前1-R行和 && 前1-C列和,问这个矩阵原来是如何的,要求每一个元素大小在1-20之间 解题思路:将每一行连接到超级源点,容量为该行的 ...

  9. UVa 11019 Matrix Matcher - Hash

    题目传送门 快速的vjudge传送门 快速的UVa传送门 题目大意 给定两个矩阵S和T,问T在S中出现了多少次. 不会AC自动机做法. 考虑一维的字符串Hash怎么做. 对于一个长度为$l$的字符串$ ...

随机推荐

  1. linux批量匹配移动文件的方法

    需求 有需要移动的文件名清单 filename.txt filename.txt 内容如下: 15542842 1582457 1282427 1532158 4542457 1582453 6552 ...

  2. js深度复制

    项目过程遇到需要对一个对象处理,然后独立出来用,结果怎么处理都会影响到原有变量,原来是引用导致,只有深度复制才行. 最终用下面的赋值方法才搞成功 var result=$.extend( true, ...

  3. iOS自定义控件创建原理(持续更新)

    前言 因为如果要创建各种自定义控件根据需求的不同会有很多的差别,所以我就在这里,分析一些自定义控件的创建实现方法 弹出视图 1.把要弹出的视图装在一个控制器里面,自定义转场动画 2.创建一个弹出视图, ...

  4. 【距离GDOI:141天】 滚入数位DP的坑

    作为博客园的第一篇...我都不知道要写什么了 ... 其实今天很没状态,就当吐槽吧... 嗯,被黄神带去写treap+可持久化线段树,然后在可持久化的删除上面跪了两天,真的是一跪不起.我已经连续多久没 ...

  5. linux系统初始化——sysinit文件写法详解

    sysinit文件写法详解 sysinit文件是linux初始化文件系统时执行的第一个脚本文件.它主要做在各个运行级别中进行初始化工作,包括: 启动交换分区;检查磁盘;设置主机名;检查并挂载文件系统; ...

  6. sql2008百万级数据排除重复信息

    --高性能排除重复select userid from table where userid in ( select userid from ( select userid, row_number() ...

  7. python斐波那契数列复杂度

    契数列 概述: 斐波那契数列,又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n ...

  8. MongoDB 查询语法

    转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册:http://www.mongodb.org/di ...

  9. [CODEVS2035]机票打折问题

    题目描述 Description .输入机票原价(3到4位的正整数,单位:元),再输入机票打折率(小数点后最多一位数字).编程计算打折后机票的实际价格(单位:元.计算结果要将个位数四舍五入到十位数“元 ...

  10. 自动合并多个文件如js css等 可以增加效率

    原文发布时间为:2011-01-13 -- 来源于本人的百度文章 [由搬家工具导入] 原文地址:http://www.codeproject.com/KB/aspnet/HttpCombine.asp ...