背包问题

1:01背包

第九届山东省acm省赛cf

解题策略是,按每分钟下降的分数和解题时间的比值、进行排序(先写每分钟消耗分数高的题目),01背包求解最大值

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class K {

	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

	static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	static String next() throws IOException {
		in.nextToken();
		return (String) in.sval;
	}

	static int a[] = new int[2005], b[] = new int[2005], c[] = new int[2005];
	static int n, T, Tim;
	static int dp[] = new int[5005];

	public static void main(String[] args) throws IOException {
		n = nextInt();
		T = nextInt();
		long sum = 0;
		Node[] x = new Node[2005];
		for (int i = 1; i <= n; i++) {
			a[i] = nextInt();
		}

		for (int i = 1; i <= n; i++) {
			b[i] = nextInt();
		}

		for (int i = 1; i <= n; i++) {
			c[i] = nextInt();
		}

		for (int i = 1; i <= n; i++) {
			x[i] = new Node(a[i], b[i], c[i]);
		}

		Arrays.sort(x, 1, n + 1);
		int max = 0;
		for (int i = 1; i <= n; i++) { // 01背包
			for (int j = T; j >= x[i].c; j--) {
				dp[j] = Math.max(dp[j], dp[j - x[i].c] + x[i].score - x[i].dec * j);
				max = Math.max(dp[j], max);
			}
		}
		out.println(max);
		out.flush();
	}

	static class Node implements Comparable<Node> {
		int score, dec, c; // 分别代表分数、 每分钟下降的得分、 与 c分钟内能解决、

		public Node() {
		}

		public Node(int score, int dec, int c) {
			this.score = score;
			this.dec = dec;
			this.c = c;
		}

		@Override
		public int compareTo(Node o) {
			if ((this.dec * 1.0 / this.c) < (o.dec * 1.0 / o.c)) {// 下降的分数 同 解决时间的比值 排序
				return 1;
			} else if ((this.dec * 1.0 / this.c) == (o.dec * 1.0 / o.c)) {
				return 0;
			} else {
				return -1;
			}
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值