JAVA编程——POJ1083

/*
 * Author: poj_whut_shz
 * Problem: Moving Tables
 * Code: poj_1083
 * URL: http://poj.org/problem?id=1083
 * Data: 2019/9/11
 */

/*
 *  【题意分析】:
 *  如图所示在一条走廊的两侧各有200个房间,
 *  现在给定一些成对的房间相互交换桌子,但是搬运过程的路线不能交叉,
 *  每次搬运消耗10min,问完成所有的搬运的最少用时。
 *  
 *  【解题思路】:
 *  利用房间号将走廊分割为一百份,每个“子走廊”都设置一个计数器,
 *  搬运过程需要经过的子走廊计数器+1,
 *  最后选出全部计数器的最大值,×10就是最终答案。
 */

package dp;

import java.util.Scanner;

public class P1083 {

	private static int tests = 0; //需要处理的用例数量
	private static int num = 0; //每个用例中需要搬运的桌子数量
	private static int first = 0; //搬运桌子的起点
	private static int second = 0; //搬运桌子的终点
	private static int max = 0; //最终结果
	private static int[] count; //子走廊计数器数组
	
	//房间号转换为子走廊号
	private static int toNum(int num) {
		if(num % 2 == 0) {
			return ((num / 2) - 1);
		}
		else {
			return ((num - 1) / 2);
		}
	}
	
	//操作计数器
	private static void addCount(int first, int second) {
		
		if(first > second) { //保证起点小于等于终点
			int temp = first;
			first = second;
			second = temp;
		}
		
		for(; first <= second; first++) {
			count[first]++;
		}
	}
	
	//获得最大值
	private static void findMax() {
		for(int i = 0; i < 100; i++) {
			if(count[i] > max) {
				max = count[i];
			}
		}
	}
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		tests = scan.nextInt();
		
		for(int i = 0; i < tests; i++) { //对每个用例循环操作
			num = scan.nextInt();
			count = new int[100];
			for(int j = 0; j < num; j++) {
				first = toNum(scan.nextInt());
				second = toNum(scan.nextInt());
				
				addCount(first, second); //操作计数器
			}
			findMax(); //找到最大值
			System.out.println(max * 10); //打印结果
		}
		scan.close();
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值