Problem a

来源:互联网 发布:ppt柱状图显示数据 编辑:程序博客网 时间:2024/04/29 04:42

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. 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 N+3-rd line, the remaining test cases are listed in the same manner as above.

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

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

Sample Output
102030

¢在经理给出的说明表格中,已经明确地描述了算法:
l(从房间30到50)和(从房间60到90)是允许的,因为没有占用公共的走廊;
l(从房间20到40)和(从房间31到80)是不允许的,因为要占用公共的走廊。
¢我们将每个房间之间的走廊作为一个统计单位,当所有的办公桌都搬运完成之后,看看这段走廊到底需要占用多少次?然后统计所有的走廊被占用的最大值max,这个值就是要单独安排的搬运次数,乘以10就是总的搬运时间。
¢该算法属于贪心算法,因为它尽可能使搬运办公桌同时进行,以便使单独安排的搬运次数最少。

int i, j;

int move[200];

int N;        //搬运次数

//每次搬运的起点和终点

int from, to;

scanf("%d", &N);

memset(move, 0, sizeof(move));

for(i = 0; i < N; i++)

{

  scanf("%d%d", &from, &to);

  //将房间号映射为走廊号

  from = (from - 1)/2;

  to = (to - 1)/2;

  //确保from<toC++使用:swap(from,to)

  if(from > to) {

    int temp = from;

    from = to;

    to = temp;

  }

  //占用走廊情况

  for(j = from; j <= to; j++)

    move[j]++;

}


以下是Accept的代码:

#include<iostream>

#include<memory.h>
using namespace std;
int main()
{
   int i,n1,move[200];
   int from,to,n;
   cin>>n;
   for(int i =0;i<n;++i)
   {
       memset(move,0,sizeof(move));
       cin>>n1;
       for(int p =0;p<n1;++p)
       {
          cin>>from>>to;
          from = (from - 1)/2;
          to = (to - 1)/2;
         if(from > to)
         {
           swap(from,to);
         }
         for(int j = from;j<=to;++j)
         {
           move[j]++;
         }
       }
       int max = 0;
       for(int i1 = 0;i1<200;++i1)
       {
           if(move[i1] > max)
           {
               max = move[i1];
           }
       }
       cout<<max*10<<endl;
   }


}
0 0
原创粉丝点击