Matrix Land
Problem Statement :
You are playing a matrix-based game with the following setup and rules: You are given a matrix A with n rows and m columns. Each cell contains some points. When a player passes a cell their score increases by the number written in that cell and the number in the cell becomes 0. (If the cell number is positive their score increases, otherwise it decreases.) The player starts from any cell in the first row and can move left, right or down. The game is over when the player reaches the last row and stops moving. image Print the maximum score that the player can get. Input Format The first line contains n and m. The next n lines contain m numbers each, jth number in ith line denotes the number that is written on cell Aij. Constraints 1 <= n*m <= 4*10^6 -250 <= Aij <= 250
Solution :
Solution in C :
In C++ :
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<map>
#include<cstring>
#define rep(i,j,k) for(register int i = j; i <= k; ++i)
#define dow(i,j,k) for(register int i = j; i >= k; --i)
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
using namespace std;
inline int read() {
int s = 0, t = 1; char c = getchar();
while( !isdigit(c) ) { if( c == '-' ) t = -1; c = getchar(); }
while( isdigit(c) ) s = s * 10 + c - 48, c = getchar();
return s * t;
}
const int N = 4e6+5, inf = 1e9+7;
int n, m, maxl, now, pre, f[2][N], v[N], g[N], h[N], sum[N];
int main() {
n = read(), m = read(), now = 0, pre = 1;
rep(i,1,n) {
swap(now,pre);
rep(j,1,m) v[j] = read();
rep(j,1,m) sum[j] = sum[j-1] + v[j];
rep(j,1,m) g[j] = max(g[j-1]+v[j],0);
dow(j,m,1) h[j] = max(h[j+1]+v[j],0);
maxl = -inf;
rep(j,1,m) {
maxl = max(maxl,f[pre][j]-sum[j-1]+g[j-1]);
f[now][j] = maxl+sum[j]+h[j+1];
}
maxl = -inf;
dow(j,m,1) {
maxl = max(maxl,f[pre][j]+sum[j]+h[j+1]);
f[now][j] = max(f[now][j],maxl-sum[j-1]+g[j-1]);
}
}
int ans = 0;
rep(i,1,m) ans = max(ans,f[now][i]);
cout<<ans<<endl;
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[m];
long[] left = new long[m];
long[] right = new long[m];
long[] prev = new long[m];
long[] current = new long[m];
long[] leftplus = new long[m];
long[] rightplus = new long[m];
for(int A_i = 0; A_i < n; A_i++){
for(int A_j = 0; A_j < m; A_j++){
a[A_j] = in.nextInt();
}
for (int i = 1; i < m; i++)
{
left[i] = Math.max(left[i - 1] + a[i - 1], 0);
}
for (int i = m - 2; i >= 0; i--)
{
right[i] = Math.max(right[i + 1] + a[i + 1], 0);
}
leftplus[0] = prev[0] + a[0];
for (int i = 1; i < m; i++)
{
leftplus[i] = Math.max(prev[i] + a[i] + left[i], leftplus[i - 1] + a[i]);
}
rightplus[m - 1] = prev[m - 1] + a[m - 1];
for (int i = m - 2; i >= 0; i--)
{
rightplus[i] = Math.max(prev[i] + a[i] + right[i], rightplus[i + 1] + a[i]);
}
for (int i = 0; i < m; i++)
{
current[i] = Math.max(leftplus[i] + right[i], rightplus[i] + left[i]);
}
long[] temp = current;
current = prev;
prev = temp;
}
long result = Long.MIN_VALUE;
for (int i = 0; i < m; i++)
{
result = Math.max(prev[i], result);
}
System.out.println(result);
in.close();
}
}
In C :
#include <stdio.h>
#include <stdlib.h>
int max(int x,int y);
int N,*table[4000000],*dp,*tdp,*left_sum,*right_sum,*dp_left_tree;
int main(){
int n,m,ma,total,i,j;
scanf("%d%d",&n,&m);
dp=(int*)malloc(m*sizeof(int));
tdp=(int*)malloc(m*sizeof(int));
left_sum=(int*)malloc(m*sizeof(int));
right_sum=(int*)malloc(m*sizeof(int));
dp_left_tree=(int*)malloc(m*sizeof(int));
for(i=0;i<n;i++)
table[i]=(int*)malloc(m*sizeof(int));
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&table[i][j]);
for(i=0;i<n;i++){
for(j=total=0;j<m;j++){
if(j)
left_sum[j]=table[i][j]+left_sum[j-1];
else
left_sum[j]=table[i][j];
total+=table[i][j];
}
for(j=m-1;j>=0;j--)
if(j!=m-1)
right_sum[j]=table[i][j]+right_sum[j+1];
else
right_sum[j]=table[i][j];
for(j=m-2;j>=0;j--)
left_sum[j]=max(left_sum[j],left_sum[j+1]);
for(j=1;j<m;j++)
right_sum[j]=max(right_sum[j],right_sum[j-1]);
if(i){
for(j=0;j<m;j++)
dp_left_tree[j]=dp[j]+left_sum[j];
for(j=m-2;j>=0;j--)
dp_left_tree[j]=max(dp_left_tree[j],dp_left_tree[j+1]);
for(j=0;j<m;j++)
tdp[j]=right_sum[j]+dp_left_tree[j]-total;
for(j=0;j<m;j++)
dp_left_tree[j]=dp[j]+right_sum[j];
for(j=1;j<m;j++)
dp_left_tree[j]=max(dp_left_tree[j],dp_left_tree[j-1]);
for(j=0;j<m;j++){
if(left_sum[j]+dp_left_tree[j]-total>tdp[j])
tdp[j]=left_sum[j]+dp_left_tree[j]-total;
}
for(j=0;j<m;j++)
dp[j]=tdp[j];
}
else
for(j=0;j<m;j++)
dp[j]=left_sum[j]+right_sum[j]-total;
}
for(i=0,ma=-1000000001;i<m;i++)
if(dp[i]>ma)
ma=dp[i];
printf("%d",ma);
return 0;
}
int max(int x,int y){
return (x>y)?x:y;
}
View More Similar Problems
AND xor OR
Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value
View Solution →Waiter
You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the
View Solution →Queue using Two Stacks
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que
View Solution →Castle on the Grid
You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):
View Solution →Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →