Superman Celebrates Diwali


Problem Statement :


Superman has been invited to India to celebrate Diwali. Unfortunately, on his arrival he learns that he has been invited mainly to help rescue people from a fire accident that has happened in a posh residential locale of New Delhi, where rescue is proving to be especially difficult. As he reaches the place of the fire, before him there are N buildings, each of the same height H, which are on fire. Since it is Diwali, some floors of the buildings are empty as the occupants have gone elsewhere for celebrations. In his hurry to start the rescue Superman reaches the top of the building, but realizes that his jumping power is depleted and restricted due to change in his geographical setting. He soon understands the restrictions of his jumping power, and they are as follows:

He can use the jumping power any number of times until he reaches the bottom floor, which means he can use the jumping power only until before he reaches the bottom (Ground floor), which means, once he reaches the bottom floor, he cannot move to the top floor again and try to save people. (In one single drop from the top to bottom)

While switching buildings, he loses height I while jumping.

The second restriction is explained below with an example.

Assume I=2. Now Superman is in the 2nd building 5th floor (B=2, F=5). If he wants to switch to the fifth building (B=5), he will lose height (I=2), which means he will be at floor 3 at building 5 (B=5, F=3). He can jump freely from the current floor to the floor below on the same building . That is, suppose if he is at (B=5, F=3) he can go to  without any restrictions. He cannot skip a floor while jumping in the same building. He can go to the floor below the current floor of the same building or use his jumping power, switch building, and lose height I.

Given the information about the occupied floors in each of the N buildings, help Superman to determine the maximum number of people he can save in one single drop from the top to the bottom floor with the given restrictions.

Input Format

Input starts with three values: the number of buildings , the height of the buildings , and the height Superman will lose when he switches buildings .

These are followed by N lines. Each ith line starts with a non negative integer u indicating how many people are in the ith building. Each of the following u integers indicates that a person is at height ui in the ith buiding. Each of the following u integers are given and repetitions are allowed which means there can be more than one person in a floor.

i indicates building number and j indicates floor number. Building number will not be given; since N lines follow the first line, you can assume that the ith line indicates the ith building's specifications.

Constraints
1 <= H,N <= 1900
1 <= I <= 450
0 <= u <= 1900 (for each i, which means the maximum number of people in a particular building will not exceed 1900)
1 <= uij <= H

Output Format

Output the maximum number of people Superman can save.



Solution :



title-img


                            Solution in C :

In C++ :






#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;
typedef long long int ll;

const int inf = 1e9, mod = 1e9+7;
const int N = 1905;

int n, H, d, t, mx[N], dp[N][N], ans ,x, h[N][N];

int main(){

	scanf("%d %d %d",&n,&H,&d);

	FOR(i,1,n){
		
		scanf("%d",&t);

		FOR(j,1,t){
			
			scanf("%d",&x);

			h[i][x]++;
			
		}

	}
	
	FOR(j,0,H)
		FOR(i,1,n){

			dp[i][j] = max(dp[i][j-1],mx[j-d]) + h[i][j];
			
			mx[j] = max(mx[j], dp[i][j]);
			
			if(j == H)
				
				ans = max(ans, dp[i][j]);

		}

	cout << ans << endl;

    return 0;
}








In Java :





import java.io.IOException;
import java.io.InputStream;

public class Solution {

    public static void main(String[] args) throws IOException {
        InputReader reader = new InputReader(System.in);
        int N = reader.readInt();
        int H = reader.readInt();
        int I = reader.readInt();
        int[][] people = new int[N][H];
        for (int n=0; n<N; n++) {
            int no = reader.readInt();
            for (int i=0; i<no; i++) {
                int floor = reader.readInt()-1;
                people[n][floor]++;
            }
        }
        int[][] save = new int[N][H];
        int[] max = new int[H];
        for (int n=0; n<N; n++) {
            int value = people[n][0];
            max[0] = Math.max(max[0], value);
            save[n][0] = value;
        }
        for (int h=1; h<H; h++) {
            int maxPeople = 0;
            for (int n=0; n<N; n++) {
                int value = save[n][h-1];
                if (h >= I) {
                    value = Math.max(value, max[h-I]);
                }
                value += people[n][h];
                maxPeople = Math.max(maxPeople, value);
                save[n][h] = value;
            }
            max[h] = maxPeople;
        }
        int answer = 0;
        for (int n=0; n<N; n++) {
            answer = Math.max(save[n][H-1], answer);
        }
        System.out.println(answer);
    }

    static final class InputReader {
        private final InputStream stream;
        private final byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int read() throws IOException {
            if (curChar >= numChars) {
                curChar = 0;
                numChars = stream.read(buf);
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public final int readInt() throws IOException {
            return (int)readLong();
        }

        public final long readLong() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
                if (c == -1) throw new IOException();
            }
            boolean negative = false;
            if (c == '-') {
                negative = true;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return negative ? -res : res;
        }

        public final int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i=0; i<size; i++) {
                array[i] = readInt();
            }
            return array;
        }

        public final long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i=0; i<size; i++) {
                array[i] = readLong();
            }
            return array;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }

}








In C :






#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int np[1901][1901];
int res[1901][1901];
int ma[1901];

int max(int a,int b)
{
  if(a>b)
   return a;
  return b;
}

int main() {
    int n,h,l,u,val,i,j;
    scanf("%d %d %d",&n,&h,&l);
    for(i=1;i<=n;i++)
     {
        scanf("%d",&u);
        for(j=0;j<u;j++)
         {
          scanf("%d",&val);
          np[i][val]++;
         }
     }

     for(j=1;j<=l;j++)
        {
     
        for(i=1;i<=n;i++)
          {    
          res[i][j] =res[i][j-1]+np[i][j];
          ma[j] =max(ma[j],res[i][j]);
          }
      }
      
        for(j=l+1;j<=h;j++)
        {
           
          for(i=1;i<=n;i++)
           {
             if(res[i][j-l]==ma[j-l])
             {
               res[i][j] =  np[i][j]+res[i][j-1];
             }
             else
             {
               res[i][j] = np[i][j]+max(res[i][j-1],ma[j-l]);
             }
              if(res[i][j]>ma[j])
                ma[j]=res[i][j];
           }
           
        }

       printf("%d\n",ma[h]);
    
               
    return 0;
}








In Python3 :





n, h, drop = map(int, input().split())
a = [[0] * (h + 1) for _ in range(n)]
for i in range(n):
    tmp = list(map(int, input().split()))[1:]
    for j in tmp:
        a[i][j] += 1
        
dp = [[0] * (h + 1) for _ in range(n)]
opt = [0] * (h + 1)
for i in range(1, h + 1):
    for j in range(n):
        dp[j][i] = dp[j][i - 1] + a[j][i]
        if i >= drop:
            dp[j][i] = max(dp[j][i], opt[i - drop] + a[j][i])
        #print(j, i, dp[j][i])
        opt[i] = max(opt[i], dp[j][i])
        
print(opt[h])
                        








View More Similar Problems

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

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 →