这代码怎么AC的?
查看原帖
这代码怎么AC的?
754437
AlbertCharlieQian楼主2024/6/21 10:28

https://www.luogu.com.cn/record/162661972


这背包的代码时间复杂度不是O(N⋅L2)O(N\cdot L^2)吗?


按照这题的数据来算,1ms1ms内可定过不了啊
能有大佬解释一下我为什么没有TLE


附上我的代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 500 + 10,maxm = 2000 + 10;
int n,m;
int cost[maxn],l[maxn];
int dp[maxn][maxm];

int main(){
    scanf("%d %d",&n,&m);
    memset(dp,0x3f,sizeof(dp));
    for(int i = 1;i <= n;i ++){
        scanf("%d %d",&cost[i],&l[i]);
    }
    for(int i = 0;i <= n;i ++){
        dp[i][0] = 0;
    }
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= m;j ++){
            dp[i][j] = dp[i - 1][j];
            for(int k = 1;k <= l[i] && j - k >= 0;k ++){
                dp[i][j] = min(dp[i][j],dp[i - 1][j - k] + cost[i]);
            }
        }
    }
    if(dp[n][m] == 0x3f3f3f3f)  printf("no solution");
    else    printf("%d",dp[n][m]);
    return 0;
}
2024/6/21 10:28
加载中...