Блог пользователя Legendary_45

Автор Legendary_45, история, 20 часов назад, По-английски
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
20 часов назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I am not aware of the optimal solution and I am new to DP. but, my thoughts are one element could be the left most element indicating size or rightmost indicating size or inner element.

dp[i][j] -> Bool segment possible index i the block count j

The states can be, if(j=0) -> it can be starting of left most or it can be right most. if(j>0) -> it can be right most or part of segment.

Once I code the above I'll send it here , if it works :)

  • »
    »
    19 часов назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    https://codeforces.me/contest/1741/submission/311736788

    The above gave a TLE, Now I need help in Tabulation

»
20 часов назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I have one approach. but it will give you tle.

int n;cin>>n;
        vector<int> arr(n);
        scan(arr,n);
        vector<int> dp(n+1,-1);
        auto sol = [&](int i,auto &&sol){
            if(i == n) return 1;
            int &flag = dp[i];
            if(flag != -1) return flag;
            flag = 0;
            for(int j=i+1;j<n;j++){
                const int si = j-i;
                if(arr[i] == si || arr[j] == si){
                    flag |= sol(j+1,sol);
                }
            }
            return flag;
        };
        cout<<(sol(0,sol)? "Yes":"No");