zoukankan      html  css  js  c++  java
  • HDU-1050 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.

    Input

    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

    10
    20
    30


    思路:

    • 把每次移动的区间按left进行升序排序,然后依次比较是否可以合并在一个时间搬运,可以的话计数器就+1,计数器等于总搬运次数就停止循环。
    • 代码不难写,主要刚开始小错误太多,看别人给的数据调了1个小时才调好。

    This is Code:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef struct{
     5     int l, r;
     6 }node;
     7 
     8 node ta[300];
     9 bool vis[300];
    10 
    11 bool cmp(node a, node b){
    12     return a.l < b.l;
    13 }
    14 
    15 bool judge(int a, int b){
    16     if (a % 2) ++a;
    17     if (b > a) return true;
    18     else return false;
    19 }
    20 
    21 int main()
    22 {
    23     int kase, n;
    24     scanf("%d", &kase);
    25     while(kase--){
    26         memset(ta, 0, sizeof(ta));
    27         memset(vis, 0, sizeof(vis));
    28         scanf("%d", &n);
    29         int a, b;
    30         for (int i = 0; i < n; ++i){
    31             scanf("%d%d", &a, &b);
    32             ta[i].l = min(a, b);
    33             ta[i].r = max(a, b);
    34         }
    35         sort(ta, ta+n, cmp);
    36         int cnt = 0, tempnum = 0, ans = 0;
    37         node temp = ta[0];
    38         while(cnt < n){
    39             for (int i = 1; i < n; ++i)
    40                 if (!vis[i])
    41                     if(judge(temp.r, ta[i].l)){
    42                         temp = ta[i];
    43                         vis[tempnum] = true;
    44                         tempnum = i;
    45                         ++cnt;
    46                     }
    47             vis[tempnum] = true;
    48             ++ans;
    49             ++cnt;
    50             for (int i = 0; i < n; ++i){
    51                 if (!vis[i]){
    52                     temp = ta[i];
    53                     tempnum = i;
    54                     break;
    55                 }
    56             }
    57         }
    58         printf("%d
    ", ans*10);
    59     }
    60 
    61     return 0;
    62 }

    其实这题还有更好的方法(虽然以上也是0MS过):
    方法就是求最大重叠区间数量。

    我一开始想不明白,后来反过来想,如果不重叠便可以合并,因此
    最大重叠区间段就是所有可合并集合都重叠也就是需要一个个来的一段。
    因此包含这一段的数量就是需要的十分钟数。

    以下是这种方法的代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int cnt[200];
     5 
     6 bool cmp(int a,int b)
     7 {
     8     return a > b;
     9 }
    10 
    11 int main()
    12 {
    13     int T, n;
    14     scanf("%d", &T);
    15     while(T--)
    16     {
    17         memset(cnt, 0, sizeof(cnt));
    18         scanf("%d",&n);
    19         int a, b, l, r;
    20         for(int i = 0; i < n; ++i)
    21         {
    22             scanf("%d%d",&a,&b);
    23             l = min(a, b);
    24             r = max(a, b);
    25             //将两个对面的房间优化成一个点
    26             l = (l+1) / 2;
    27             r =( r+1) / 2;
    28             for(int j = l; j <= r; ++j)
    29                 ++cnt[j];
    30         }
    31         sort(cnt, cnt+200, cmp);
    32         printf("%d
    ", cnt[0]*10);
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    redis参数AOF参数的bug
    tidb损坏tikv节点怎么恢复集群
    mysql主从延时临时解决办法
    python脚本批量杀死redis链接
    pt-online-schema-change 脚本化
    mysql查看锁等信息SQL
    mongo复制集脑裂问题如何处理
    日志收集及网络包收集方案
    各浏览器下载文件名不乱码的解决办法
    java 中 byte[]、File、InputStream 互相转换
  • 原文地址:https://www.cnblogs.com/robin1998/p/6359130.html
Copyright © 2011-2022 走看看