POJ_1083_Moving Tables

[url]http://poj.org/problem?id=1083[/url]

[size=medium]题意:搬桌子,给出a,b,表示从a搬到b,至少需要多少时间才能完成所有搬运任务
注意:搬出来走廊时,从开始到结束这段时间,所影响到的房间不可以同时作为搬运任务的区间点[/size]
[img]http://poj.org/images/1083/1083_1.gif[/img]
[img]http://poj.org/images/1083/1083_2.gif[/img]

[color=red][size=medium]贪心代码:[/size][/color]

#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;
}


[color=red][size=medium]计数统计代码:【适合用于编号只有整数的题目,如The Skyline Problem 那题,[url]http://www.acm.cs.ecnu.edu.cn/problem.php?problemid=1505[/url]】[/size][/color]

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值