现在的位置: 首页 > 综合 > 正文

ZOJ1029 Moving Tables

2012年10月08日 ⁄ 综合 ⁄ 共 2192字 ⁄ 字号 评论关闭
#include <iostream>
#include 
<vector>
#include 
<algorithm> 
using namespace std;

const int TIMEPERMOVE = 10;//每次分钟
const int MAXSIZE = 200;
struct Move
{
    
int srcRoom;//
    int desRoom;//目标
}moves[MAXSIZE];

bool lessThan(const Move& m1,const Move& m2)  
{  
    
return m1.srcRoom<m2.srcRoom;                   //按照srcRoom从小到大排序 
}   

int main(void)
{
    
int Cases,i,j,k,n,s,t;
    cin
>>Cases;
    
for (i=1;i<=Cases;++i)
    {
        vector
<Move> moveVect;
        cin
>>n;
        
for (j=0;j<n;++j)
        {
            cin
>>s;
            cin
>>t;
            
if (s > t) 
                swap(s,t);
            moves[j].srcRoom 
= (s+1)/2;
            moves[j].desRoom 
= (t+1)/2;
            moveVect.push_back(moves[j]);
        }
        
//排序
        sort(moveVect.begin(),moveVect.end(),lessThan);
        
int max = 0;
        
for (j=0; j<n; ++j) 
        {
            
int count = 1, from = moveVect[j].srcRoom, to = moveVect[j].desRoom;
            
for (k=0; k<n; ++k) 
            {
                
if (j == k) continue;
                
if (moveVect[k].srcRoom<=to && moveVect[k].desRoom>=from)
                {
                    
if (from < moveVect[k].srcRoom) 
                        from 
= moveVect[k].srcRoom;
                    
if (to > moveVect[k].desRoom)
                        to 
= moveVect[k].desRoom;
                    count
++;
                }
            }
            
if (count > max)
                max 
= count;
        }
        cout
<<TIMEPERMOVE*max<<endl;
    }
    
return 0;
}

还有人给出了不使用贪心的算法,贪心是将每次能同时搬运的桌子都搬运,求总共需要次数。能否同时搬运桌子,取决于搬运使用的走廊是否被占用。因此,实际上我们只需要求出,走廊最多被占用多少次,就可以得出最多要花多少时间,实在是高!

#include <iostream>
using namespace std;

#define MAXN 201
int map[MAXN];

void solve()
{
    
int i,n,start,end,m;

    for(i=0;i<MAXN;i++)//初始化
        map[i] = 0;

    cin >> n;
    
while(n--)
    {
        cin 
>> start;
        cin 
>> end;
        
if(start > end)
        {
                
int temp = start;
                start 
= end;
                end 
= temp;
        }
        
for(i=(start+1)/2;i<=(end+1)/2;i++)
            map[i] 
+= 1;
    }

    m = map[1];
    
for(i=2;i<MAXN;i++)
    {
        
if(map[i]>m)
            m 
= map[i];
    }
    cout 
<< m*10 << endl;
}

int main()
{
    
int t;
    cin 
>> t;
    
while(t--)
        solve();
    
return 0;
}

 

抱歉!评论已关闭.