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;
}
分享到:
相关推荐
北大POJ1083-Moving Tables 解题报告+AC代码
2遍dp poj_3613解题报告 poj_3613解题报告
poj题目2775文件子目录源代码,递归经典题目,
poj典型题目解题思路详解 包含源代码和解题时应注意的问题及题目陷阱设计分析
poj 1699的代码和方法说明,个人原创
C_(POJ_1854)(分治).cpp
poj上第1990题目源码,用到了2个树状数组,这题数据结构是关键,想到了题目就很简单了
D_(POJ_1723)(思维)(sort).cpp
原理为:以数链思想,移动数组中的内容 使数组在没有扩充情况下,达成移动的效果 当然,有更简单的,大牛不要笑哦
D_(POJ_1723)(思维)(第k大数).cpp
O(nlogn)凸包问题 poj2187
POJ 3131 双向BFS解立体八数码问题
poj两道题的c++实现。已经测试过可以通过oj
POJ题目及算法,包括动态规划、深搜广搜等算法。含相关注释。
这是北大在线测试的第1002题,方便记忆的电话号码的解题例程,题目中有一个列表,记录着许多方便记忆的电话号码。不同的方便记忆的电话号码可能对应相同的标准号码,这个程序的任务就是找到它们
问题:求平面上多个矩形的总面积。 算法:线段树(经典的线段树题目)
poj 3310 的代码和方法说明,个人原创
看了测试用例后,大家估计已经明白题目的意思了吧!题目很简单,但是就是很烦。 开始直接是没思路,不知道怎么模拟,但是想了带该半个小时,就搞定了。就是把每个数存到数组里面。
http://poj.grids.cn/problem?id=2774 POJ 2774 木棒加工 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给定了。当然,我们希望得到的小段越长越好,你的任务是计算能够...
POJ上面题目的解题报告。涵盖挺多的。可作参考。代码都正确。ACM新手入门必下~~ 加油...