Comments

What website is that?

Modern problems require modern solutions. :clownglasses:

Thanks for your time.

Thanks!

Predict my when you get time stef senpai. (I know I will be stuck in green forever). Aryan-Raina

On Decoder_69CSES Problems sets??, 5 years ago
0

If you feel stuck on some problems. Leave them for now, try again later. Skills develop with time.

Why is this downvoted? I just tried to help this guy. Is it ratism? If no please tell me how can I improve my comments and what is wrong in this one?

If the symbol at (i,j) != '*' we skip as no spruce tree with that as top can be formed else we do the dp thing. The dp thing is actually we are seeing what max height trees can be formed by the (i+1,j)(i+1,j-1),(i+1,j+1) as the top and the max height tree that can formed by (i,j) will be min of them+1. You will get the intuition when you draw a few cases and we can further prove it by induction.

dp[i][j] = hieght of max height tree with (i,j) as top

The main idea here is that the height of max height tree that can be made with (i,j) as the topmost piece == no of trees that can be made with (i,j).

Finally that ans is answer to our problem that is the total no of spruce trees = sum of spruce trees with top (i,j) for all i, j.

My solution to B is a 2D DP. The total complexity is O(NM) and i guess it is easier to understand too.

    int n, m; cin>>n>>m;
    int dp[n+2][m+2];
    mem(dp,0);
    bool block[n+2][m+2];
    REP(i,1,n) {
        REP(j,1,m) {
            char ch; cin>>ch;
            if (ch=='*') block[i][j]=1;
            else block[i][j]=0;
        }
    }
    ll ans=0;
    for (int i = n; i>=1; i--) {
        for (int j = m; j>=1; j--) {
            if (!block[i][j]) continue;
            dp[i][j]=block[i][j]+min(dp[i+1][j],min(dp[i+1][j-1], dp[i+1][j+1]));
            ans+=dp[i][j];
        }
    }
    cout<<ans<<endl;

My answer to B was way simpler than the editorial. I just did this 2D DP

if (!block[i][j]) continue;
dp[i][j]=bock[i][j]+min(dp[i+1][j],min(dp[i+1][j-1], dp[i+1][j+1]));
ans+=dp[i][j];

where block[i][j]==1 if (i,j)=='*' else 0 The complexity of this solution is O(NM) so it is the best complexity you can get.

Thanks. This is the cleanest implementation i found on internet. BTW if some beginner is reading this try 321C - Ciel the Commander, this is basic implementation problem.

Thanks it was helpful.

I converted it to diophantine by doing this: 3x+5y=n-7z for z = 0 to n/7 Then using the extended euclids algorithm I found x0 and y0 st 3x0+5y0=gcd(3, 5)=1 and multiplied x0 and y0 by n-7z. Then I tried to find t such that x0+5t, y0-3t are positive. We can use some inequalities to determine range of t and loop over these t and check if the current is correct if yes return. Actually all values in the range are correct but i looped over some modification of the range because I could not think whether to +1 at each iteration or -1(i am new to CP and CS in general) so i checked the cases. My algorithm was correct(you can check my profile for code) but it was very messy, hard to implement for someone not good in math, a lot of code so I don't suggest this.