SGU 168 Matrix (DP)

来源:互联网 发布:php redis缓存周期 编辑:程序博客网 时间:2024/05/09 09:06

原题:

      

168. Matrix

time limit per test: 0.5 sec. 
memory limit per test: 16000 KB
input: standard 
output: standard



You are given N*M matrix A. You are to find such matrix B, that B[i,j]=min{ A[x,y] : (y>=j) and (x>=i+j-y) }

Input
On the first line of the input there are two integer numbers, N and M (1<=N,M<=1000). Then matrix A follows: next N lines contains M integers each (not greater than 32000 by absolute value). The j-th number on then i-th of this lines is A[i,j].

Output
Write matrix B in the same format as matrix A, but without N and M.

Sample test(s)

Input
3 3 1 2 3 4 5 6 7 8 9
Output
1 2 3 
2 3 6 
3 6 9
[submit]
[forum]

Author:NNSU #2 teamResource:
Date:







#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <string>#include <cmath>#include <stack>#include <vector>#include <utility>#include <set>#include <map>#include <sstream>#include <climits>#pragma comment(linker, "/STACK:1024000000,1024000000")#define pi acos(-1.0)#define INF 2147483647using namespace std;typedef long long ll;typedef pair <int,int > PP;int N[1005][1005],M[1005][1005],s[1005][1005];int main(){    int n,m;    scanf("%d%d",&n,&m);    int min_=32005;    for(int i=0; i<n; i++)        for(int j=0; j<m; j++)            scanf("%d",&N[i][j]);    for(int j=m-1; j>=0; j--)    {        int min__=32005;        for(int i=n-1; i>=0; i--)        {            min_=min(min_,N[i][j]);            min__=min(min__,N[i][j]);            s[i][j]=min__;        }        M[0][j]=min_;    }    for(int i=1; i<n; i++)        for(int j=0; j<m; j++)        {            min_=s[i][j];            if(j<m-1)            {                min_=min(min_,M[i-1][j+1]);            }            M[i][j]=min_;        }    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            j==0?printf("%d",M[i][j]):printf(" %d",M[i][j]);        }        printf("\n");    }}





Server time: 2017-11-02 12:37:05Online Contester Team © 2002 - 2016. All rights reserved.

原创粉丝点击