Canna

Tuesday, 25 September 2018

ROW COLUMN ADD

  • Problem Description

    You are given an N N grid initially filled by zeros. Let the rows and columns of the grid be numbered from 1 to N, inclusive. There are two types of operations can be applied to the grid:

    RowAdd R X: all numbers in the row R should be increased by X.
    ColAdd C X: all numbers in the column C should be increased by X.

    Now after performing the sequence of such operations you need to find the maximum element in the grid.
    Input

    The first line of the input contains two space separated integers N and Q denoting the size of the grid and the number of performed operations respectively. Each of the following Q lines describe an operation in the format described above.
    Output

    Output a single line containing the maximum number at the grid after performing all the operations.
    Constraints

    1 <= N <= 314159
    1 <= Q <= 314159
    1 <= X <= 3141
    1 <= R, C <= N

  • Test Case 1

    Input (stdin)
    2 4
    
    RowAdd 1 3
    
    ColAdd 2 1
    
    ColAdd 1 4
    
    RowAdd 2 1
    
    
    Expected Output
    7
  • Test Case 2

    Input (stdin)
    3 2
    
    RowAdd 1 3
    
    ColAdd 2 1
    
    
    Expected Output
    4
  • Program
  • #include<stdio.h>
    int main()
    {
     int n,q;
     scanf("%d%d",&n,&q);
     int rowElement[n],colElement[n];
     int i;
     for(i=0;i<n;i++)
     {
      rowElement[i]=0;
      colElement[i]=0;
     }
     while(q--)
     {
      char s[10];
      int k,x;
      scanf("%s%d%d",s,&k,&x);
      if(s[0]=='R')
      {
       k=k-1;
       rowElement[k]+=x;
      }
      else
      {
       k=k-1;
       colElement[k]+=x;
      }
        }
       int rowMax=0;
     for(i=0;i<n;i++)
     {
      if(rowElement[i]>rowMax)
       rowMax=rowElement[i];
     }
     int colMax=0;
     for(i=0;i<n;i++)
     {
      if(colElement[i]>colMax)
       colMax=colElement[i];
     }
     printf("%d",colMax+rowMax);
      return 0;
    } 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.