hdu 1050 Moving Tables(排序方法小结。贪心)

43 篇文章 0 订阅

 

Problem 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. 



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

 

题目意思:2间对面房公用一条道,若两次活动有道重叠则要分开进行,求一共要进行多少次。

解题方案:                              A    暴力(简单)

       无公用则可同时进行,所以只要求出哪一条道经过次数最多,那就是最大答案。

                                               B   贪心

   设置标记(1数组 2改数据,以下改数据方法),排序后,枚举判断是否有重叠部分,没有则同时进行并标记。

   意思就是搞定所有方案要运行几次,每运行一次ans++,记录运行了几次,最后输出答案。

  

注意点:  输入的两个数不一定前面那个比较小,输入要处理,暴力的话还要减1除2,求出第几条道。

              贪心解法要注意1:以开始还是以结尾排序?        2:以不同排序方法以正序还是倒序枚举

为什么以头排序或者以尾排序要不同的比较顺序呢,这里也卡了我几次,可以说是很严重的思想问题

从一些简单的数据看不出自己的错误,后来也是通过数据看出区别。

  1:以结束时间做排序,运行的时候倒序枚举求最多数量,因为正序求得是最长的链接,会存在非最优匹配。

  2:以开始时间做排序,正序枚举求最多,倒序枚举求最长。

  什么是求最多? 就是能够接在别人后面的样例的数量(多条加起来的数量)

  什么是最长连接? 就是一条链上的样例数量最多(一条的数量)

很难说,下面举个例子。下面是一个数据,已经经过结束时间做排序,然后正序枚举求答案。

14 42
17 100
98 104    (98>42)接上第一个 1
19 112
61 116
84 140
36 143
75 154
125 158   (125>104) 接上第三个 3
66 163
53 165
81 168
15 170
21 171
48 176
3 189
151 195   接上第二个2
65 196
105 199  (这个被前面的抢先了,本来两个都可以的)
这样才是最优
17 100 -〉105 199
19 122 -〉151 195 
但是以尾来排,能找到最长,但是可能存在尾比较小,这样他优先,到后面尾巴变大了,却接不上了,
17 100 -〉151 195 (195<199他先接)
19 122 -〉105 199 (接不上了)(不Ok,比较小的结尾被抢了,导致到它时已经没有能匹配的尾)    
以开头来排序,不用担心被抢,因为开头递增,你能接我也能接,正序没错。

所以不同的排序方法,在匹配的顺序也要做出调整,要知道目标,知道怎样最优。

下面附上两种解决方案的代码:    A  暴力

 

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
    int cas,ma,n,a,b,ans[200],kai,jie,i;
    cin>>cas;
    while(cas--)
    {
        ma=0;
        memset(ans,0,sizeof(ans));
        cin>>n;
        for(i=0;i<n;i++)
        {
        cin>>a>>b;
        if(a<b)
        {
            kai=(a-1)/2;
            jie=(b-1)/2;
        }
        else
        {
            kai=(b-1)/2;
            jie=(a-1)/2;
        }
        for(;kai<=jie;kai++)
        {
            ans[kai]++;
            if(ans[kai]>ma)
                ma=ans[kai];
        }
        }
        cout<<ma*10<<endl;
    }
    return 0;
}

                                                                       B 贪心

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
struct fangjian
{
    long long int kai;
    long long int jie;
}fj[5000];
int cmp(fangjian a,fangjian b)
{
    if(a.jie<b.jie)
        return 1;
    return 0;
}
int main()
{
    long long int cas,n,i,j,a,b,en,ans;
    cin>>cas;
    while(cas--)
    {
        ans=0;
          cin>>n;
          for(i=0;i<n;i++)
          {
              cin>>a>>b;
              if(a<b)
              {
                  fj[i].kai=a;
                  fj[i].jie=b;
              }
              else
              {
                   fj[i].kai=b;
                  fj[i].jie=a;
              }
          }
          sort(fj,fj+n,cmp);
          en=fj[0].jie;
          for(i=n-1;i>=0;i--)
          {
              if(fj[i].kai==-1&&fj[i].jie==-1)
                continue;
                en=fj[i].kai;
              ans++;
              for(j=i-1;j>=0;j--)
              {
                  if(fj[j].kai==-1&&fj[j].jie==-1)
                    continue;
                  if(fj[j].jie%2==1)//单数
                  {
                      if(en-fj[j].jie>=2)
                      {
                          en=fj[j].kai;
                          fj[j].jie=-1;
                          fj[j].kai=-1;
                      }
                  }
                  else if(en-fj[j].jie>=1)
                  {
                         en=fj[j].kai;
                          fj[j].jie=-1;
                          fj[j].kai=-1;
                  }
              }
          }
          cout<<ans*10<<endl;
    }
    return 0;
}

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值