AT_abc219_d [ABC219D] Strange Lunchbox Solution

AT_abc219_d [ABC219D] Strange Lunchbox Solution

Luogu Portal

AT Portal

train of thought

At first glance, it is a dynamic programming, but there are two layers of restriction states.

d p i , j , k \mathit{dp}_{i,j,k} dpi,j,kmeans before iiThe first constraint condition of i items isjjThe j second constraints takekkThe smallest solution of k .

ii can then be modeled on the knapsack problemThe i dimension is deleted, but it doesn’t matter whether it is deleted or not.

Note here is at least, so the transfer equation is dpi , j = min ⁡ ( dpi , j , dp max ⁡ ( 0 , j − ai . x ) , max ⁡ ( 0 , k − ai . y ) + 1 ) \mathit{ dp}_{i,j}=\min(\mathit{dp}_{i,j},\mathit{dp}_{\max(0,j-a_{i}.x),\max(0 ,k-a_{i}.y)}+1)dpi,j=min(dpi,j,dpmax(0,jai.x),max(0,kai.y)+1)

the code

#include <bits/stdc++.h>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
    
    
	inline int read() {
    
    
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
    
    
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
    
    
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
int n, ft, sd, sum1 = 0, sum2 = 0, f[355][355];
pair<int, int> a[355];
int main() {
    
    
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
    n = read(), ft = read(), sd = read();
    for(int i = 1; i <= n;i ++) {
    
    
    	a[i].first = read(), a[i].second = read();
    	sum1 += a[i].first, sum2 += a[i].second;
    }
    if(sum1 < ft || sum2 < sd) {
    
    
    	puts("-1");	
	}
    else {
    
    
    	memset(f, 0x3f, sizeof(f));
    	f[0][0] = 0;
    	for(int i = 1; i <= n; i ++) {
    
    
    		for(int j = 300; j >= 0; j --) {
    
    
    			for(int k = 300; k >= 0;k --) {
    
    
    				f[j][k] = min(f[j][k], f[max(0, j - a[i].first)][max(0, k - a[i].second)] + 1);
    			}
			}
		}		
    	int ans = 0x3f3f3f3f;
    	for(int i = ft; i <=300; i ++) {
    
    
    		for(int j = sd; j <= 300; j ++) {
    
    
    			ans = min(ans, f[i][j]);
			}
		}	
    	write(ans);
    }    
	return 0;
}

Guess you like

Origin blog.csdn.net/ZH_qaq/article/details/130462902