hdu-5671 Matrix

题目链接:

Matrix

Time Limit: 3000/1500 MS (Java/Others)   

 Memory Limit: 131072/131072 K (Java/Others)


Problem Description
 
There is a matrix M that has n rows and m columns (1n1000,1m1000).Then we perform q(1q100,000) operations:

1 x y: Swap row x and row y (1x,yn);

2 x y: Swap column x and column y (1x,ym);

3 x y: Add y to all elements in row x (1xn,1y10,000);

4 x y: Add y to all elements in column x (1xm,1y10,000);
 
Input
 
There are multiple test cases. The first line of input contains an integer T(1T20) indicating the number of test cases. For each test case:

The first line contains three integers nm and q.
The following n lines describe the matrix M.(1Mi,j10,000) for all (1in,1jm).
The following q lines contains three integers a(1a4)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
 
题意
 
一个矩阵交换行或者列,或者把某一行或者某一列加一个数;
 
思路
 
用一个数组记录位置,一个记录加的数值;
这题有个奇妙的TLE点就是我开的的t,x,y为全局变量时会TLE,开成局部的就过了,求懂原理的大神留言讲解;
 
AC代码
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const ll inf=1e15;
const int N=1006;
int a[N][N],l[N],r[N],fl[N],fr[N];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,q;
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=n;i++)l[i]=i,fl[i]=0;
        for(int i=1;i<=m;i++)r[i]=i,fr[i]=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&a[i][j]);
        int t,x,y;
        for(int i=1;i<=q;i++)
        {
            scanf("%d%d%d",&t,&x,&y);
            if(t==1)
                swap(l[x],l[y]);
            else if(t==2)
                swap(r[x],r[y]);
            else if(t==3)
                fl[l[x]]+=y;
            else
                fr[r[x]]+=y;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<m;j++)
            {
                printf("%d ",a[l[i]][r[j]]+fl[l[i]]+fr[r[j]]);
            }
            printf("%d
",a[l[i]][r[m]]+fl[l[i]]+fr[r[m]]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5436167.html