工厂机器安排(Java)

Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task’s level yi cannot complete this task. If the company completes this task, they will get (500xi+2yi) dollars.

The company has n machines. Each machine has a maximum working time and a level. If the time for the task is more than the maximum working time of the machine, the machine can not complete this task. Each machine can only complete a task one day. Each task can only be completed by one machine.

The company hopes to maximize the number of the tasks which they can complete today. If there are multiple solutions, they hopes to make the money maximum.

输入格式:
The input contains several test cases.

The first line contains two integers N and M. N is the number of the machines.M is the number of tasks(1 < =N <= 100000,1<=M<=100000).

The following N lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the maximum time the machine can work.yi is the level of the machine.

The following M lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the time we need to complete the task.yi is the level of the task.

输出格式:
For each test case, output two integers, the maximum number of the tasks which the company can complete today and the money they will get.

输入样例:
在这里给出一组输入。例如:

1 2
100 3
100 2
100 1
输出样例:
在这里给出相应的输出。例如:

1 50004

代码如下,需要解释的地方已经注释

import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;

//任务类
class task implements Comparable<task> {
	int time, level;

	public task(int time, int level) {
		super();
		this.time = time;
		this.level = level;
	}

	/*
	 * 按照时间降序排序,时间相同,按照等级降序排序
	 */
	@Override
	public int compareTo(task o) {
		if (this.time == o.time)
			return o.level - this.level;
		return o.time - this.time;
	}
}

//机器类
class machine implements Comparable<machine> {
	int time, level;
	int flag;// 标志位,1表示没有被使用,0表示已经被使用

	public machine(int time, int level) {
		super();
		this.time = time;
		this.level = level;
		this.flag = 1;
	}

	/*
	 * 按照时间降序排序,时间相同,按照等级降序排序
	 */
	@Override
	public int compareTo(machine o) {
		// TODO Auto-generated method stub
		if (this.time == o.time)
			return o.level - this.level;
		return o.time - this.time;
	}
}

public class Main5 {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner in = new Scanner(new BufferedInputStream(System.in));

		int n = in.nextInt();
		int m = in.nextInt();

		machine[] ma = new machine[n];
		task[] ta = new task[m];

		for (int i = 0; i < n; i++) {
			int t = in.nextInt();
			int l = in.nextInt();
			ma[i] = new machine(t, l);
		}

		for (int i = 0; i < m; i++) {
			int t = in.nextInt();
			int l = in.nextInt();
			ta[i] = new task(t, l);
		}

		Arrays.sort(ma);
		Arrays.sort(ta);

		int temp_level, temp_j = 0, time = 0;
		long money = 0;
		for (int i = 0; i < ta.length; i++) {// 遍历任务
			temp_level = 200;

			for (int j = 0; j < ma.length; j++) {// 遍历机器
				if (ma[j].flag == 1 && ma[j].time >= ta[i].time && ma[j].level >= ta[i].level) {// 该机器没有被使用并且满足条件
					if (ma[j].level < temp_level) {// 记录下等级最低的机器的值,并且记录他的位置,这一步很关键
						temp_level = ma[j].level;
						temp_j = j;
					}
				}
			}

			if (temp_level != 200) {// 如果找到该机器,就投入使用
				ma[temp_j].flag = 0;
				money += ta[i].time * 500 + ta[i].level * 2;
				time++;
			}

			if (time == m)// 如果机器全部投入使用,就结束循环
				break;
		}
		System.out.println(time + " " + money);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值