首页 > 专题系列 > Java解POJ > POJ 1083 Moving Tables [解题报告] Java
2013
11-09

POJ 1083 Moving Tables [解题报告] Java

Moving Tables

问题描述 :

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.



For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager’s problem.

输入:

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. Each test case begins with a line containing an integer N , 1 <= N <= 200, that represents the number of tables to move.

Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t each room number appears at most once in the N lines). From the 3 + N -rd

line, the remaining test cases are listed in the same manner as above.

输出:

The output should contain the minimum time in minutes to complete the moving, one per line.

样例输入:

3 
4 
10 20 
30 40 
50 60 
70 80 
2 
1 3 
2 200 
3 
10 100 
20 80 
30 50 

样例输出:

10
20
30

解题代码:

import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Arrays;

 public class Main {

     public static void main(String[] args) throws NumberFormatException,
             IOException {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         int t = Integer.parseInt(read.readLine());
         int s;
         int[][] m;
         String[] str;
         int[] rooms = new int[200];
         int len;
         int start;
         int max;
         for (int i = 0; i < t; i++) {
             Arrays.fill(rooms, 0);
             s = Integer.parseInt(read.readLine());
             m = new int[s][2];
             for (int j = 0; j < s; j++) {
                 str = read.readLine().split(" ");
                 m[j][0] = Integer.parseInt(str[0]);
                 m[j][1] = Integer.parseInt(str[1]);
             }
             for (int j = 0; j < s; j++) {
                 if (m[j][0] % 2 == 1) {
                     m[j][0] = m[j][0] + 1;
                 }
                 if (m[j][1] % 2 == 1) {
                     m[j][1] = m[j][1] + 1;
                 }
                 len = Math.abs(m[j][0] - m[j][1]) / 2 + 1;
                 start = Math.min(m[j][0], m[j][1]) / 2 - 1;
                 for (int k = 0; k < len; k++) {
                     rooms[start + k]++;
                 }
             }
             max = rooms[0];
             for (int j = 1; j < 200; j++) {
                 if (rooms[j] > max) {
                     max = rooms[j];
                 }
             }
             System.out.println(max * 10);
         }
     } 
}

  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
    应该是,不可能小于合并后的第K小值吧

  3. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])