前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Poj第1083题--Moving Tables

Poj第1083题--Moving Tables

作者头像
felix
发布2018-06-13 14:36:54
6760
发布2018-06-13 14:36:54
举报
文章被收录于专栏:Felix的技术分享

Poj第1083题–Moving Tables

原题

Moving Tables

Description

The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.

The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.

解题思路

代码语言:javascript
复制
将一组测试数据存放到一张二维数组m[1...n][2]中,其中m[1...n][0]存放搬桌子的起始点,m[1...n][1]存放搬桌子的终点。
然后将房间号转化为过道号。
最后遍历过道号数组,找到搬桌子经过次数最多的过道号,找到此最大的次数,这就是并行处理需要的最多的单位时间了。

算法实现

代码语言:javascript
复制
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) throws NumberFormatException,
            IOException {
        Scanner read = new Scanner(System.in);
        int t = read.nextInt(); //一共有t组测试数据
        int s;  //s张桌子将搬动
        int[][] m; //存放几组搬桌子起点终点的房间号

        int[] corridors = new int[200]; //过道下标为k(对应的房间号为2k-1和2k+2),其值表示桌子经过过道的次数
        int start; //开始的过道编号
        int len;   //搬桌子的距离
        int max;
        for (int i = 0; i < t; i++) {
            Arrays.fill(corridors, 0);  //对每组数据都重新初始化
            s = read.nextInt();
            m = new int[s][2];
            for (int j = 0; j < s; j++) {
                m[j][0] = read.nextInt();
                m[j][1] = read.nextInt();
            }

            //遍历所有桌子,更新corridors数组中的值
            for (int j = 0; j < s; j++) {
                //将房间号转化为对应的过道号
                if (m[j][0] % 2 == 0) {
                    m[j][0] = m[j][0] / 2 - 1;
                } else {
                    m[j][0] = m[j][0] / 2;
                }
                if (m[j][1] % 2 == 0) {
                    m[j][1] = m[j][1] / 2 - 1;
                } else {
                    m[j][1] = m[j][1] / 2;

                }

                len = Math.abs(m[j][0] - m[j][1]) + 1;
                start = Math.min(m[j][0], m[j][1]);
                for (int k = 0; k < len; k++) {
                    corridors[start + k]++;
                }
            }

            //找到某个搬桌子过程中经过次数最多的过道号,找到此最大的次数
            max = corridors[0];
            for (int j = 1; j < 200; j++) {
                if (corridors[j] > max) {
                    max = corridors[j];
                }
            }
            System.out.println(max * 10);
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年04月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Poj第1083题–Moving Tables
    • 原题
      • 解题思路
      • 算法实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档