HDU 5671 Matrix

Matrix

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 502    Accepted Submission(s):
215

Problem Description

There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000).Then we perform q(1≤q≤100,000) operations:
1 x y: Swap row x and row y (1≤x,y≤n) ;
2 x y: Swap column x and column y (1≤x,y≤m) ;
3 x y: Add y to all elements in row x (1≤x≤n,1≤y≤10,000) ;
4 x y: Add y to all elements in column x (1≤x≤m,1≤y≤10,000) ;

Input

There are multiple test cases. The first line of input
contains an integer T(1≤T≤20) indicating the number of test cases. For each test case:
The first line
contains three integers n , m and q .
The following n lines describe the matrix M.(1≤M,i,j≤10,000) for all (1≤i≤n,1≤j≤m) .
The following q lines contains three integers a(1≤a≤4) , x and y .

Output

For each test case, output the matrix M after all q operations.

Sample Input

2

3 4 2

1 2 3 4

2 3 4 5

3 4 5 6

1 1 2

3 1 10

2 2 2

1 10

10 1

1 1 2

2 1 2

Sample Output

12 13 14 15

1 2 3 4

3 4 5 6

1 10

10 1

Hint

Recommand to use scanf and printf

Recommend

wange2014

题目大意:有一个n行m列的矩阵,在这个矩阵上进行q个操作,输出经过所有q个操作以后的矩阵M

思路:用4个数组,对于交换行、交换列的操作,分别记录当前状态下每一行、每一列是原始数组的哪一行、哪一列即可。

对每一行、每一列加一个数的操作,也可以两个数组分别记录。

注意当交换行、列的同时,也要交换增量数组。

输出时通过索引找到原矩阵中的值,再加上行、列的增量。

#include <iostream>
#include <string.h>
using namespace std;
int h[1010],l[1010], zh[1010], zl[1010];
int a[1010][1010];
int main()
{
    int T, n, m, q, i, j, c, x, y, t;
    cin>>T;
    while(T--)
    {
    memset(zh,0,sizeof(zh));
    memset(zl,0,sizeof(zl));
    for(i=1;i<=1005;i++)
    h[i]=l[i]=i;
    scanf("%d%d%d", &n, &m, &q);
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
    scanf("%d", &a[i][j]);
    while(q--)
    {
    scanf("%d%d%d", &c, &x, &y);
    if(c==1) {t=h[x];h[x]=h[y];h[y]=t;}
    else if(c==2) {t=l[x];l[x]=l[y];l[y]=t;}
    else if(c==3) zh[h[x]] += y;
    else if(c==4) zl[l[x]] += y;
    }
    for(i=1;i<=n;i++)
        {
        for(j=1;j<=m;j++)
        if(j==1)
        printf("%d", a[h[i]][l[j]]+zh[h[i]]+zl[l[j]]);
        else
        printf(" %d", a[h[i]][l[j]]+zh[h[i]]+zl[l[j]]);
        printf("\n");
        }
    }
    return 0;
}
时间: 2024-10-29 19:07:29

HDU 5671 Matrix的相关文章

hdu 5671 Matrix(BC——思维题)

题目链接:acm.hdu.edu.cn/showproblem.php?pid=5671 Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 891    Accepted Submission(s): 371 Problem Description There is a matrix M that has n rows

HDU 5671 Matrix (BestCoder Round #81 (div.2) 1002)

传送门 Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 311    Accepted Submission(s): 142 Problem Description There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000).Then we

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o

HDU 2686 Matrix(最大费用最大流+拆点)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(因为有两个方向的选择)即可 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int ma

HDU多校赛第9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学小知识】

难度上,,,确实,,,不算难 问题是有个矩阵运算的优化 题目是说给个N*K的矩阵A给个K*N的矩阵B(1<=N<=1000 && 1=<K<=6),先把他们乘起来乘为C矩阵,然后算C^(N*N) 相当于 ABABABABABABAB...=(AB)^(N*N) 不如 A(BA)^(N*N-1)B 因为BA乘得K*K的矩阵,K是比较小的 #include <cstdio> #include <cstdlib> #include <cstr

HDU 2830 Matrix Swapping II (DP,最大全1矩阵)

题意  给你一个n*m矩阵  每列都可以随便交换位置   求最优交换后最大的全1子矩阵 又是HDU 1505 1506的变种  但这个更容易了  因为每列都可以交换位置了   那么这一行中所有比i高的都可以与i相邻了  只需要统计这一行有多少个比i高就行了   可以在算出每一行后  把高度大的放前面去  用num[i]记录排序后的列原来的数  这样就有j列比h[i][num[j]]高了  最后的答案也就是max(j*h[i][num[j]]) #include<cstdio> #include

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640 

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr