• LuoguP1858 多人背包(DP)


    (K)优解这类问题可在(DP)过程中通过添维解决。归并出当前前(K)大的解。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int a = (b); a <= (c); ++a)
    #define nR(a,b,c) for(register int a = (b); a >= (c); --a)
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
    #define QWQ
    #ifdef QWQ
    #define D_e_Line printf("
    ---------------
    ")
    #define D_e(x) cout << (#x) << " : " << x << "
    "
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt", "r", stdin)
    #define FileSave() freopen("out.txt", "w", stdout)
    #define TIME() fprintf(stderr, "
    TIME : %.3lfms
    ", clock() * 1000.0 / CLOCKS_PER_SEC)
    #else
    #define D_e_Line ;
    #define D_e(x) ;
    #define Pause() ;
    #define FileOpen() ;
    #define FileSave() ;
    #define TIME() ;
    #endif
    struct ios {
    	template<typename ATP> inline ios& operator >> (ATP &x) {
    		x = 0; int f = 1; char c;
    		for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
    		while(c >= '0' && c <='9') x = x * 10 + (c ^ '0'), c = getchar();
    		x *= f;
    		return *this;
    	}
    }io;
    using namespace std;
    template<typename ATP> inline ATP Max(ATP a, ATP b) {
    	return a > b ? a : b;
    }
    template<typename ATP> inline ATP Min(ATP a, ATP b) {
    	return a < b ? a : b;
    }
    template<typename ATP> inline ATP Abs(ATP a) {
    	return a < 0 ? -a : a;
    }
    
    const int N = 207;
    
    long long f[5007][51], tmp[5007];
    
    struct nod {
    	long long cost, val;
    } a[N];
    int main() {
    	int n, K, V;
    	io >> K >> V >> n;
    	R(i,1,n){
    		io >> a[i].cost >> a[i].val;
    	}
    	
    	
    	Fill(f, 0xcf);
    	f[0][1] = 0;
    	
    	R(i,1,n){
    		nR(j,V,a[i].cost){
    			int tot1 = 1, tot2 = 1, tot3 = 0;
    			while(tot3 <= K){
    				if(f[j][tot1] > f[j - a[i].cost][tot2] + a[i].val){
    					tmp[++tot3] = f[j][tot1++];
    				}
    				else{
    					tmp[++tot3] = f[j - a[i].cost][tot2++] + a[i].val;
    				}
    			}
    			R(k,1,K) f[j][k] = tmp[k];
    		}
    	}
    	
    	long long ans = 0;
    	R(i,1,K){
    		ans += f[V][i];
    	}
    
    	printf("%lld", ans);
    	return 0;
    }
    

  • 相关阅读:
    骗子网站,X毛都没有,骗我九十九
    xssgame挑战wp
    某xss挑战赛闯关笔记
    committed与urgent的区别
    KahnProcessNetwork的Python实现
    网络层数
    使用matlab用优化后的梯度下降法求解达最小值时参数
    使用matlab画半透明椭圆
    关系代数(Relation Algebra)与SQL语句的对应关系
    one vs all -- 将01分类器用于多类分类问题
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11748282.html
Copyright © 2020-2023  润新知