POJ 2528 Mayor's posters 题解
来源:互联网 发布:衡安称重软件 编辑:程序博客网 时间:2024/06/06 07:37
http://poj.org/problem?id=2528
题解:
将所有海报的端点瓷砖排序,把每个海报的端点瓷砖都看做一个单位区间,两个相邻的端点瓷砖之间的部分是一个单位区间
这样最多会有20000 + 19999个单位区间
关键:插入数据的顺序------从上往下依次插入每张海报,这样后插入的海报不可能覆盖先插入的海报,因此插入一张海报时,如果发现海报对应区间有一部分露出来,就说明该海报部分可见。
时间复杂度: nlogn(n为海报数目)
#include <iostream>#include <algorithm>#include <math.h>using namespace std;int n;struct CPost { int L,R;};CPost posters[10100];int x[20200];//海报的端点瓷砖编号int hash[10000010]; //hash[i]表示瓷砖i所处的离散化后的区间编号struct CNode { int L,R; bool bCovered; //区间[L,R]是否已经被完全覆盖 CNode * pLeft, * pRight;};CNode Tree[1000000];int nNodeCount= 0;int Mid( CNode * pRoot) { return (pRoot->L + pRoot->R)/2;}void BuildTree( CNode * pRoot, int L, int R) { pRoot->L = L; pRoot->R = R; pRoot->bCovered = false; if( L == R ) return; nNodeCount ++; pRoot->pLeft = Tree + nNodeCount; nNodeCount ++; pRoot->pRight = Tree + nNodeCount; BuildTree( pRoot->pLeft,L,(L+R)/2); BuildTree( pRoot->pRight,(L+R)/2 + 1,R);}bool Post( CNode *pRoot, int L, int R) { //插入一张正好覆盖区间[L,R]的海报,返回true则说明区间[L,R]是部分或全部可见的 if( pRoot->bCovered ) return false; if( pRoot->L == L && pRoot->R == R) { pRoot->bCovered = true; return true; } bool bResult ; if( R <= Mid(pRoot) ) bResult = Post( pRoot->pLeft,L,R); else if( L >= Mid(pRoot) + 1) bResult = Post( pRoot->pRight,L,R); else { bool b1 = Post(pRoot->pLeft ,L,Mid(pRoot)); bool b2 = Post( pRoot->pRight,Mid(pRoot) + 1,R); bResult = b1 || b2; }//要更新根节点的覆盖情况 if( pRoot->pLeft->bCovered && pRoot->pRight->bCovered ) pRoot->bCovered = true; return bResult;}int main() { int t; int i,j,k; scanf("%d",&t); int nCaseNo = 0; while(t--) { nCaseNo ++; scanf("%d",&n); int nCount = 0; for( i = 0; i < n; i ++ ) { scanf("%d %d", & posters[i].L, & posters[i].R ); x[nCount++] = posters[i].L; x[nCount++] = posters[i].R; } sort(x,x+nCount);
//<a target=_blank href="http://www.cnblogs.com/heyonggang/archive/2013/08/07/3243477.html">http://www.cnblogs.com/heyonggang/archive/2013/08/07/3243477.html</a> nCount = unique(x,x+nCount) -x; //去掉重复元素//下面离散化 int nIntervalNo = 0; for( i = 0; i < nCount; i ++ ) { hash[x[i]] = nIntervalNo; if( i < nCount -1) { if( x[i+1] -x[i] == 1)nIntervalNo ++; else nIntervalNo += 2;//表示除了两个端点,中间还有一段 ,整段用一个节点表示,正是节约内存处理 } } BuildTree( Tree,0,nIntervalNo ); int nSum = 0; for( i = n -1; i >= 0; i --) { // 从后往前看每个海报是否可见,若上面的海报已经把这个区间覆盖了,下面的海报就不可见了 //此处posters[i]的左右两个端点和中间的区间hash成了intervalnode,最多一共2*i(端点)+2*i-1(区间) if( Post(Tree,hash[posters[i].L],hash[posters[i].R]))nSum ++; } printf("%d\n",nSum); } return 0;}
0 0
- POJ 2528 Mayor's posters 题解
- 【poj 2528】Mayor's posters 题意&题解&代码(C++)
- POJ 2528 Mayor's posters
- poj 2528 Mayor's posters
- POJ 2528 Mayor's Posters
- Mayor's posters poj 2528
- poj 2528 Mayor's posters
- Poj 2528 Mayor's posters
- poj 2528 Mayor's posters
- POJ 2528 Mayor's posters
- POJ 2528 Mayor's posters
- POJ 2528 - Mayor's posters
- POJ-2528-Mayor's posters
- poj 2528 Mayor's posters
- poj 2528 Mayor's posters
- poj 2528 Mayor's posters
- POJ 2528 Mayor's posters
- POJ 2528 Mayor's posters
- 根据html页面模板动态生成html页面(c#类)
- 设计模式(知识点2)
- 解决MySQL出现大量unauthenticated user的问题
- [LeetCode] Validate Binary Search Tree
- hdu 2255 奔小康赚大钱 KM模板
- POJ 2528 Mayor's posters 题解
- 杭电2063————匈牙利算法(二分图最大匹配)
- SpyMemcache与Spring整合
- Drupal升级运行更新后出现public files directory的解决办法。
- winsock教程- windows下的socket编程(c语言实现)
- 数据库设计规范化的五个要求
- jsp验证码
- My SQL获取某个表的列名
- LeetCode - Two Sum