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

Автор AC_AC, история, 6 лет назад, По-английски

I was solving Book Shop of CSES DP section.

  • I am getting TLE even after multiple attempts, Can you tell me how to optimize my TOP-DOWN approach.
  • I have tried replacing 2-d array with 2-d vector .
  • I have also tried swapping the dimensions of dp array ( some caching thing ).
my code
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

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

Everything is fine. Just set dp[index][total] = ans before returning.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

The time constraints in the CSES Problem set are very tight! In some problems using long long instead of int might also give TLE. So its always better to use Iterative method rather than Recursive(for CSES)

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

Surprisingly, my solution with a complexity of O(1e8) was accepted, and it ran in only 0.1 seconds.

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

    can you paste your code here

    • »
      »
      »
      2 года назад, скрыть # ^ |
      ← Rev. 4  
      Проголосовать: нравится 0 Проголосовать: не нравится
      #include<bits/stdc++.h>
      using namespace std;
      #define ll long long
      #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
      ll n,m;
      vector<ll>price,page;
      int32_t main()
      {
          fast
          ll i,j=0,x;
          cin>>n>>x;
          price.resize(n);
          page.resize(n);
          for(auto &it:price){cin>>it;}
          for(auto &it:page){cin>>it;}
          vector<ll>dp(x+1,INT_MIN);
          dp[0]=0;
          for(auto it:price)
          {
              for(i=x;i>=it;i--)
              {
                  dp[i]=max(dp[i],dp[i-it]+page[j]);
              }j++;
          }
          ll ans=0;
          for(i=0;i<=x;i++){ans=max(ans,dp[i]);}
          cout<<ans<<'\n';
      }
      
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I'm surprised that this is for 3 years ago but the latest comments are from 2 weeks ago.

I just got accepted on the task and I had the same problem. I mean technically it should've been an MLE. I dont know if anyone has said this before or not, but here instead of defining a DP[100000][100] I just defined a DP[100000][2] since you only need dp[j][i-1] to update your DP, So you can just store the previous row of your DP table and use parity to go over the loop and store your values.

here's my code:

#include <bits/stdc++.h>
using namespace std;
 
const int mxx = 1e5 + 1;
const int mxn = 1e3 + 1;
int dp[mxx][2];
int s[mxn], h[mxn];
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, x;
    cin >> n >> x;
    for (int i = 0; i < n; i++) cin >> h[i];
    for (int i = 0; i < n; i++) cin >> s[i];
    for (int i = 0; i < x + 1; i++) {
        if (h[0] <= i) dp[i][0] = s[0];
        else dp[i][0] = 0;
    }
 
    for (int i = 1; i < n; i++) {
        for (int j = 0; j <= x; j++) {
            if (h[i] > j) dp[j][i%2] = dp[j][1 - (i%2)];
            else dp[j][i%2] = max(dp[j][1 - (i%2)], dp[j - h[i]][1 - (i%2)] + s[i]);
        }
    }
 
    cout << dp[x][(n-1) % 2];
}