SGU 168 Matrix (DP)

原题:

      

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

Author:NNSU #2 team
Resource:
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 2147483647
using 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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值