ZOJ1029 Moving Tables

简介:
#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;
}

复制代码


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/10/28/1321489.html,如需转载请自行联系原作者
目录
相关文章
|
11月前
UVa11565 - Simple Equations
UVa11565 - Simple Equations
40 0
|
11月前
uva101 The Blocks Problem
uva101 The Blocks Problem
37 0
LeetCode 283. Move Zeroes
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
72 0
LeetCode 283. Move Zeroes
|
算法 Python
LeetCode 283. 移动零 Move Zeroes
LeetCode 283. 移动零 Move Zeroes
杭电oj-1050 Moving Tables
杭电oj-1050 Moving Tables
86 0
杭电oj-1050 Moving Tables
|
算法
HDU-1050,Moving Tables(不用贪心也AC)
HDU-1050,Moving Tables(不用贪心也AC)
LeetCode之Move Zeroes
LeetCode之Move Zeroes
85 0
uva 10706 - Number Sequence
点击打开链接uva 10706 题目意思:    有一个数组 s[1] = 1 , s[2] = 1 2 , .......s[k] = 1....k,要求给定一个n表示数组的第几位,要求这个第几位是什么数。
930 1
|
索引 Python Java
LeetCode 283:移动零 Move Zeroes
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。
749 0