All possible sub-matrices sums.
Save the nature
https://www.codechef.com/problems/SVNTR (Lunch Time oct 2015)
In this problem , you have to count number of sub-matrices whose sum is less than or equal to k (given).
Approach for this problem : firstly calculate the all possible column sum , i.e for c number of columns ,there will be c*(c+1)/2 columns created.for example : 1 2 3 1 3 6 2 5 3
4 5 6 --->>> 4 9 15 5 11 6
after creating "b" matrix from the given "a" matrix. Calculate all possible sums in each columns
In this all possible sum for 1st column is : 1,4,5.
In this all possible sum for 2nd column is : 3,9,12.
now, that count all the sums which is smaller than or equal to "k".
- #include<bits/stdc++.h>
- using namespace std;
- long long int b[151][12255],a[151][151];
- int main()
- {
- ios_base::sync_with_stdio(0);
- int t;
- cin>>t;
- while(t--)
- {
- long long int r,c,make,i,j,k,l,p,s,cnt=0;
- cin>>r>>c>>make;
- for(i=0;i<r;i++)
- for(j=0;j<c;j++)
- cin>>a[i][j];
- for(p=0;p<r;p++)
- {
- k=0;
- for(i=0;i<c;i++)
- {
- s=0;
- for(j=i;j<c;j++)
- {
- s+=a[p][j];
- b[p][k++]=s;
- }
- }
- }
- long long int sz=((c*(c+1)));
- sz=sz/2;
- for(i=0;i<sz;i++)
- {
- for(j=0;j<r;j++)
- {
- s=0;
- for(k=j;k<r;k++)
- {
- s+=b[k][i];
- if(s<=make)
- cnt++;
- }
- }
- }
- cout<<cnt<<endl;
- }
- return 0;
- }
Comments
Post a Comment