PACKING(揹包+O2/O3氧化)


问题 K: PACKING

问题 K: PACKING

时间限制: 1 Sec  内存限制: 128 MB
提交: 233  解决: 19
[提交] [状态] [命题人:admin]

 

题目描述

It was bound to happen.  Modernisation has reached the North Pole.  Faced with escalating costs for feeding Santa Claus and the reindeer, and serious difficulties with security, NP Management has decided to do away with the traditional sleigh and adopt delivery by drone (magic, superfast drone).  
Lack of investment capital means that the new system will start small, and hopefully grow in the years to come.  For the first test run in 2017 there will be only two drones and they will have limited carrying capacity.  PR is, of course, all important.  There will be disappointment, and NP Management has decided to focus on delivering only the most expensive toys to the richest children, so as to focus the worst of the disappointment on those who have the greatest experience of coping (the poor). 
Choosing the presents to deliver is your problem.  You are being asked to develop an algorithm to select the cargo to deliver, given weight limits for each of the drones and a list of candidate presents with weights and values.  Your goal is to maximise the value of gifts delivered. 

 

输入

Input will consist of a series of problems.  The first line of the input holds a single integer P being the number of problems.  Then for each problem there will be three lines of input.  The first line holds three integers:  N (1 <= N <= 100) being the number of candidate presents;  W1 and W2 (1 <= W1, W2 <= 1000) being the weight limits of the two drones respectively.  The second line holds N integers (1 <= wi  <= 100) being the weights of each of the candidate presents and the third line holds N integers (1 <= vi  <= 100) being the values of the presents (in thousand dollar units).  All lines are formatted with single spaces between numbers and no leading or trailing spaces. 

 

输出

For each problem your program should output one line with the text “Problem “ and the number of the problem (counting from 1) followed by a colon, a space and the total value of presents shipped by the drone pair. 

 

样例输入

复制样例数据

2
4 9 4
3 4 5 6
5 7 9 10
6 9 11
3 4 5 6 3 4
2 3 4 5 3 3

样例输出

Problem 1: 22
Problem 2: 16

 两个揹包,体积分别为w1,w2,求最大价值

最简单的思路就是搜索呗,每件物品只有三种决策:放到1,放到2,不放

 

int dfs(int id,int x,int y){
    if(id>n) return 0;
    int ans=dfs(id+1,x,y);
    if(x>=a[id].w) ans=max(ans,a[id].v+dfs(id+1,x-a[id].w,y));
    if(y>=a[id].w) ans=max(ans,a[id].v+dfs(id+1,x,y-a[id].w));
    return ans;
} 

但是没办法记忆化啊,数组开不了那么大,那就改写成递推,第一维滚动优化

还是TLE

dyt大佬学弟说max用三目运算符加O2(O3)优化就能过 ,果然三目运算符和吸氧一个也不能少(快读没用)

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
int dp[2][1005][1005];
int w[105],v[105];
int n,w1,w2;
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
int main(){
    int T;
    int cas=0;
    T=read(); 
    while(T--){
    	n=read();
    	w1=read();
    	w2=read();
		for(int j=0;j<=w1;j++){
			for(int k=0;k<=w2;k++){
				dp[0][j][k]=0;
			}
		}
    	for(int i=1;i<=n;i++) w[i]=read();
    	for(int i=1;i<=n;i++) v[i]=read();
    	int p=0;
    	for(int i=1;i<=n;i++){
    		p^=1;
    		for(int j=0;j<=w1;j++){
    			for(int k=0;k<=w2;k++){
    				dp[p][j][k]=dp[p^1][j][k];
    				if(j>=w[i]) dp[p][j][k]=max(dp[p][j][k],dp[p^1][j-w[i]][k]+v[i]);
    				if(k>=w[i]) dp[p][j][k]=max(dp[p][j][k],dp[p^1][j][k-w[i]]+v[i]);
				}
			}
		}
		printf("Problem %d: %d\n",++cas,dp[p][w1][w2]);
	}
	return 0;
}

用java那比C++长的时长也能过 

import java.util.*;
import java.math.*;
 
public class Main {
    static int n,w1,w2; 
    static int w[]=new int[105];
    static int v[]=new int[105];
    static int dp[][][]=new int[2][1005][1005];
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        for(int cas=1;cas<=T;cas++) {
            n=sc.nextInt();
            w1=sc.nextInt();
            w2=sc.nextInt();
            for(int j=0;j<=w1;j++){
                for(int k=0;k<=w2;k++){
                    dp[0][j][k]=0;
                }
            }
            for(int i=1;i<=n;i++) w[i]=sc.nextInt();
            for(int i=1;i<=n;i++) v[i]=sc.nextInt();
            int p=0;
            for(int i=1;i<=n;i++){
                p^=1;
                for(int j=0;j<=w1;j++){
                    for(int k=0;k<=w2;k++){
                        dp[p][j][k]=dp[p^1][j][k];
                        if(j>=w[i]) dp[p][j][k]=Math.max(dp[p][j][k],dp[p^1][j-w[i]][k]+v[i]);
                        if(k>=w[i]) dp[p][j][k]=Math.max(dp[p][j][k],dp[p^1][j][k-w[i]]+v[i]);
                    }
                }
            }
            System.out.println("Problem "+cas+": "+dp[p][w1][w2]);
        }
    }  
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章