Skip to content

Instantly share code, notes, and snippets.

@ABHIINAV12
Created April 30, 2021 13:29
Show Gist options
  • Save ABHIINAV12/a3bea610f969a655fac7a8174dd4b3cc to your computer and use it in GitHub Desktop.
Save ABHIINAV12/a3bea610f969a655fac7a8174dd4b3cc to your computer and use it in GitHub Desktop.
April challenge upsolving
--- first question :
conider the criteria of minimum surface area, for that all the cubes must be placed adjacent with side.
with number of faces visible, allot the maximum possible number to them
void solve(){
int n; cin>>n;
int four=n/4;
int rem=n%4;
int ans=four*44;
if(four!=0){
if(rem==0){
ans+=4*4;
}else if(rem==1){
ans+=3*4;
ans+=20;
}else if(rem==2){
ans+=2*4;
ans+=36;
}else{
ans+=4;
ans+=51;
}
}else{
if(rem==0){
ans+=0;
}else if(rem==1){
ans+=20;
}else if(rem==2){
ans+=36;
}else{
ans+=51;
}
}
cout<<ans<<endl;
}
--- second question :
The array is kind of non decreasing in nature. Hence for each index as starting point, find the shortest length square that has the sum
better than required and rest of them will include in ans. Use binary search for this. for calculating sum use prefix sums.
void solve(){
int n,m,k; cin>>n>>m>>k;
int a[n+1][m+1]; f(i,0,n+1) f(j,0,m+1) a[i][j]=0;
f(i,1,n+1) f(j,1,m+1) cin>>a[i][j];
int pre[n+1][m+1]; f(i,0,n+1) f(j,0,m+1) pre[i][j]=0;
f(i,1,n+1)
f(j,1,m+1) pre[i][j]+=pre[i][j-1]+a[i][j];
f(j,1,m+1)
f(i,1,n+1) pre[i][j]+=pre[i-1][j];
int cnt=0;
f(i,1,n+1) f(j,1,m+1) {
int low=0,high=min(n-i,m-j),poss=-1;
while(low<=high){
int mid=low+high; mid/=2;
int sum_in_range=pre[i+mid][j+mid]-pre[i-1][j+mid]-pre[i+mid][j-1]+pre[i-1][j-1];
if(sum_in_range >= k*(mid+1)*(mid+1)) {
poss=mid;
high=mid-1;
}else low=mid+1;
}
if(poss!=-1)
cnt+=min(n-i,m-j)-poss+1;
}
cout<<cnt<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment