Moving Tables HDU - 1050


package D3_23;

import java.util.Scanner;

/**
 * 思路: 寻找重叠走廊最大值,最后乘以10即为最大用时 。注意:对面房间公用同一问题
 * 
 * @author Ming
 *
 */
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		int[] result = new int[t];
		while (t > 0) {
			int n = sc.nextInt();
			int[] a = new int[401];
			for (int i = 0; i < n; i++) {
				int b = sc.nextInt();
				int c = sc.nextInt();
				if (b > c) {
					int tmp = b;
					b = c;
					c = tmp;
				}
				// ----------注意------------
				if (b % 2 == 0) {
					b--;
				}
				if (c % 2 == 1) {
					c++;
				}
				// -------------------------
				for (int j = b; j <= c; j++) {
					a[j]++;
				}
			}
			int ans = 0;
			for (int i = 1; i <= 400; i++) {
				if (a[i] > ans) {
					ans = a[i];
					result[t - 1] = ans;
				}
			}
			t--;
		}
		for (int i = result.length - 1; i >= 0; i--) {
			System.out.println(result[i] * 10);
		}
	}
}


一开始是思路是将没有走廊重叠的情况放在一组,然后每一组10分钟,求总时间,但一直不通过,暂时不知道哪里出错了,现贴出代码。


package D3_23;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DP_MovingTables {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		int is;
		int[] result;
		int[] start;
		int[] end;
		int[] visit;
		List<Integer> list = new ArrayList<>();
		int tmp;
		n = sc.nextInt();
		result = new int[n];
		
		while (n > 0) {
			is = sc.nextInt();
			start = new int[is];
			end = new int[is];
			visit = new int[is];
			for (int i = 0; i < is; i++) {
				start[i] = sc.nextInt();
				end[i] = sc.nextInt();
				if (start[i] > end[i]) {
					tmp = start[i];
					start[i] = end[i];
					end[i] = tmp;
				}
				if (start[i] % 2 == 0) {
					start[i]--;
				}
				if (end[i] % 2 == 1) {
					end[i]++;
				}
			}
			for (int j = 0; j < is; j++) {
				if (visit[j] == 1) {
					continue;
				}
				list.add(j);
				for (int a = j; a < is; a++) {
					if (visit[a] == 1) {
						continue;
					}
					int b = 0;
					// 遍历添加时间错开的数据
					for (; b < list.size(); b++) {
						if (start[a] - end[list.get(b)] < 0 && end[a] - start[list.get(b)] > 0) {
							b = -1;
							break;
						}
					}
					if (b != -1) {
						visit[a] = 1;
						list.add(a);
					}
				}
				result[n - 1] += 10;
				list.clear();
			}
			n--;
		}
		for (int i = result.length - 1; i >= 0; i--) {
			System.out.println(result[i]);
		}
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值