`

POJ_1083_Moving Tables

阅读更多
http://poj.org/problem?id=1083

题意:搬桌子,给出a,b,表示从a搬到b,至少需要多少时间才能完成所有搬运任务
注意:搬出来走廊时,从开始到结束这段时间,所影响到的房间不可以同时作为搬运任务的区间点




贪心代码:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
//#include <map>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

struct Room{
    int x, y;
}room[405];
bool hash[405];
bool cmp (Room a, Room b)    //按区间开端x排序,求区间最多覆盖次数
{
    if (a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}
int main()
{
    int n, i, t, temp, j, res;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (i = 0; i < n; i++)
        {
            cin >> room[i].x >> room[i].y;
            if (room[i].x > room[i].y)    //每个区间开端为x,末端为y,y必须大于等于x
            {
                int t = room[i].x;
                room[i].x = room[i].y;
                room[i].y = t;
            }
            if (room[i].x % 2 == 0)//观察图可知,开端x是偶数时,会使得x-1的位置不可用
                room[i].x--;
            if (room[i].y % 2 == 1)//观察图可知,末端y是奇数时,会使得y+1的位置不可用
                room[i].y++;
        }
        /*for (i = 0; i < n; i++)
            cout << room[i].x << ' ' << room[i].y << "-----\n";*/
        sort (room, room+n, cmp);
        res = 0;
        memset (hash, true, sizeof(hash));
        for (i = 0; i < n; i++)
        {
            if (hash[i])    //往下寻找没有被覆盖其他区间
            {
                res++;   //记录被覆盖的区间数
                temp = room[i].y;    //记录临时区间尾
                for (j = i + 1; j < n; j++)
                    if (hash[j])
                        if (room[j].x > temp)    //该区间没有发生交叉则标记为false
                            hash[j] = false, temp = room[j].y;   //区间尾发生变化
            }
        }
        cout << res*10 << endl;
    }
    return 0;
}


计数统计代码:【适合用于编号只有整数的题目,如The Skyline Problem 那题,http://www.acm.cs.ecnu.edu.cn/problem.php?problemid=1505
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
//#include <map>
#include <queue>
#include <utility>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

int main()
{
	int room[405];
	int t, i, n, a, b, temp, maxs;
	cin >> t;
	while (t--)
	{
		memset (room, 0, sizeof(room));
		maxs = 0;
		cin >> n;
		for (i = 0; i < n; i++)
		{
			cin >> a >> b;
			if (a > b)   //对于下面的for循环,必须要b大于等于a
				temp = a, a = b, b = temp;
			if (!(a&1))   //同理,a是偶数的时候也会影响a-1的位置
				a--;
			if (b&1)    //同理,b是奇数的时候也会影响b+1的位置
				b++;
			if (b > maxs)
				maxs = b;
			//cout << a << ' ' << b << endl;
			for (int x = a; x <= b; x++)
				room[x]++;
		}
		cout << (*max_element (room, room+maxs+1))*10 << endl;    //返回至少区间覆盖数
	}
	return 0;
}
1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics