1 条题解

  • 0
    @ 2026-3-1 10:44:56
    #include <bits/stdc++.h>
    using namespace std;
    int c[501],l[501],dp[2000*2+10];
    int n,L,x=0;
    
    int main(){
    	cin >> n >> L;
    	for(int i=1;i<=n;i++){
    		cin >> c[i] >> l[i];
            if(l[i]>L) l[i]=L;
    		x+=l[i];
    	}
    	//特判 
    	if(x<L){
    		cout << "no solution";
    		return 0;
    	}
    	//dp[容量] = 最少的钱
    	memset(dp,0x7F,sizeof(dp));
    	dp[0]=0;
    	for(int i=1;i<=n;i++){
    		for(int j=L*2;j>=l[i];j--){
    			dp[j]=min(dp[j],dp[j-l[i]]+c[i]);
    		}
    	} 
    	//找L~2L之间的最小值
    	int ans=1e9;
    	for(int i=L;i<=L*2;i++){
    		ans=min(ans,dp[i]);
    	}
    	//特判 
    	if(ans==1e9){
    		cout << "no solution";
    		return 0;
    	}
    	cout << ans;
    	//
    	return 0;
    } 
    

    小杨买饮料(洛谷 B3873 普及/提高-)

    信息

    ID
    1252
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    6
    已通过
    3
    上传者