完美世界_2018828

0-1背包问题

0-1 背包问题:给定 n 种物品和一个容量为 C 的背包,物品 i 的重量是 wi,其价值为 vi 。

问:应该如何选择装入背包的物品,使得装入背包中的物品的总价值最大?

分析一波,面对每个物品,我们只有选择拿取或者不拿两种选择,不能选择装入某物品的一部分,也不能装入同一物品多次。

解决办法:声明一个 大小为  m[n][c] 的二维数组,m[ i ][ j ] 表示 在面对第 i 件物品,且背包容量为  j 时所能获得的最大价值 ,那么我们可以很容易分析得出 m[i][j] 的计算方法,

(1). j < w[i] 的情况,这时候背包容量不足以放下第 i 件物品,只能选择不拿

   m[ i ][ j ] = m[ i-1 ][ j ]

(2). j>=w[i] 的情况,这时背包容量可以放下第 i 件物品,我们就要考虑拿这件物品是否能获取更大的价值。

如果拿取,m[ i ][ j ]=m[ i-1 ][ j-w[ i ] ] + v[ i ]。 这里的m[ i-1 ][ j-w[ i ] ]指的就是考虑了i-1件物品,背包容量为j-w[i]时的最大价值,也是相当于为第i件物品腾出了w[i]的空间。

如果不拿,m[ i ][ j ] = m[ i-1 ][ j ] , 同(1)

究竟是拿还是不拿,自然是比较这两种情况那种价值最大。

由此可以得到状态转移方程:

例:0-1背包问题。在使用动态规划算法求解0-1背包问题时,使用二维数组m[i][j]存储背包剩余容量为j,可选物品为i、i+1、……、n时0-1背包问题的最优值。绘制

价值数组v = {8, 10, 6, 3, 7, 2},

重量数组w = {4, 6, 2, 2, 5, 1},

背包容量C = 12时对应的m[i][j]数组。

0 1 2 3 4 5 6 7 8 9 10 11 12
1 0 0 0 8 8 8 8 8 8 8 8 8
2 0 0 0 8 8 10 10 10 10 18 18 18
3 0 6 6 8 8 14 14 16 16 18 18 24
4 0 6 6 9 9 14 14 17 17 19 19 24
5 0 6 6 9 9 14 14 17 17 19 21 24
6 2 6 8 9 11 14 16 17 19 19 21 24

(第一行和第一列为序号,其数值为0)
如m[2][6],在面对第二件物品,背包容量为6时我们可以选择不拿,那么获得价值仅为第一件物品的价值8,如果拿,就要把第一件物品拿出来,放第二件物品,价值10,那我们当然是选择拿。m[2][6]=m[1][0]+10=0+10=10;依次类推,得到m[6][12]就是考虑所有物品,背包容量为C时的最大价值。

public class MathContest {
	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		int score[] = new int[num];
		int time[] = new int[num];
		for (int i = 0; i < num; i++) {
			score[i] = scanner.nextInt();
		}
		for (int i = 0; i < num; i++) {
			time[i] = scanner.nextInt();
		}
		int totalTime = scanner.nextInt();
		System.out.println(getScore(score, time, totalTime));
	}
	public static int getScore(int score[], int time[], int totalTime) {
		int[][] dp = new int[score.length+1][totalTime+1];
		for (int i=0; i<score.length; i++) {
			for (int j=0; j<=totalTime; j++) {
				if (j<time[i]) {
					dp[i+1][j] = dp[i][j];
				}
				else {
					dp[i+1][j] = Math.max(dp[i][j], dp[i][j-time[i]]+score[i]);
				}
			}
		}
		return dp[score.length][totalTime];
	}
}

import java.util.Scanner;
import java.util.Arrays;
public class Main{
	public static void main(String[] arg) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] teamA = new int[n];
		int[] teamB = new int[n];
		for (int i = 0; i < n; i++) {
			teamA[i] = scanner.nextInt();
		}
		for (int i = 0; i < n; i++) {
			teamB[i] = scanner.nextInt();
		}
		System.out.println(getMostBonus(n, teamA, teamB));
	}
public static int getMostBonus(int n, int teamA[], int teamB[]) {
		boolean[] Bused = new boolean[n];
        boolean[] Aused = new boolean[n];
        int sum = 0 ;
        Arrays.sort(teamA);
        Arrays.sort(teamB);
        int count = 0;
        for (int i = n-1 ; i>=0 ; i-- ){
            for (int j = n-1 ; j>= 0 ; j--){
                if (Bused[j])
                    continue;
                if (teamA[i] == teamB[i])
                    break;
                if (teamA[i] > teamB[j]){
                    Aused[i] = true;
                    sum += 100;
                    Bused[j] = true;
                    count++;
                    break;
                }

            }
        }
        for (int i = n -1 ; i>=0 ; i--) {
            if (Aused[i])
                continue;
            for (int j = n-1; j >= 0; j--) {
                if (Bused[j])
                    continue;
                if (teamA[i] > teamB[j]){
                    Aused[i] = true;
                    sum += 100;
                    Bused[j] = true;
                    count++;
                    break;
                }
                if (teamA[i] == teamB[j]) {
                    Aused[i] = true;
                    Bused[j] = true;
                    count++;
                    break;
                }
            }
        }
        sum = sum - (n - count)*100;
        return sum;
    }

}

猜你喜欢

转载自blog.csdn.net/u012879957/article/details/82220126