Mayor's posters 线段树+离散化+曾经RE无数边
来源:互联网 发布:问东西的软件 编辑:程序博客网 时间:2024/06/05 14:32
Description
- Every candidate can place exactly one poster on the wall.
- All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
- The wall is divided into segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
Input
Output
The picture below illustrates the case of the sample input.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
Source
有一面墙,被等分为1QW份,一份的宽度为一个单位宽度。现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW。后贴的海报若与先贴的海报有交集,后贴的海报必定会全部或局部覆盖先贴的海报。现在给出每张海报所贴的位置(左端位置和右端位置),问张贴完N张海报后,还能看见多少张海报?(PS:看见一部分也算看到。)
下面的离散化的实现比较容易理解 但是消耗空间较大
#include<stdio.h>
#include<algorithm>
using namespace std;
#define max 10005
struct haha
{
int c;
int left;
int right;
}node[max*10];//一开始一个劲的WA 就是这里错了 我乘的是4 但是注意有可能点会很多 达到2*n 即每个起点终点都不同 而不是n个 故乘以4绝对不行
int rem[max*2],used[max*2],left[max],right[max],ans;
int stl[10000000+5];
void build(int left,int right,int nd)
{
int mid;
node[nd].left=left;
node[nd].right=right;
node[nd].c=0;
if(left==right) return ;
mid=(left+right)/2;
build(left,mid,nd*2);
build(mid+1,right,nd*2+1);
}
void update(int left,int right,int per,int nd)
{
int mid;
if(node[nd].c==per) return ;
if(left==node[nd].left&&right==node[nd].right)
{
node[nd].c=per;
return ;
}
if(node[nd].c>0)
{
node[nd*2].c=node[nd].c;node[nd*2+1].c=node[nd].c;
node[nd].c=0;
}
mid=(node[nd].left+node[nd].right)/2;
if(right<=mid) update(left,right,per,nd*2);
else if(left>mid) update(left,right,per,nd*2+1);
else
{
update(left,mid,per,nd*2);
update(mid+1,right,per,nd*2+1);
}
}
int query(int idx){
int count=0;
if (node[idx].c>0){
if(!used[node[idx].c])
{
used[node[idx].c]=1;
count=1;
}
return count;
}
return count=query(idx*2)+query(idx*2+1);
}
int main()
{
int i,cas,n,cnt,num,t,j;
scanf("%d",&cas);
while(cas--)
{
cnt=0;num=0;
memset(used,0,sizeof(used));
scanf("%d",&n);
for(i=1,j=n+1;i<=n;i++,j++)
{
scanf("%d %d",&left[i],&right[i]);
rem[i]=left[i];
rem[j]=right[i];
}
sort(rem+1,rem+1+2*n);
t=1; stl[rem[1]]=t;
for(i=2;i<=2*n;i++)
{
if(rem[i]!=rem[i-1])
stl[rem[i]]=++t;//主要是这里 为离散化的关键步骤 就是把不同的rem[i]映射为新的节点
}
build(1,t,1);
for(i=1;i<=n;i++)
{
update(stl[left[i]],stl[right[i]],i,1);
}
printf("%d\n",query(1));
}
return 0;
}
此方法也很容易理解 且消耗空间少
#include<stdio.h>
#include<algorithm>
using namespace std;
struct haha
{
int c;
int left;
int right;
}node[10000*20];
int rem[10000*2+20],used[10000*2+20],left[10020],right[10000+20],ans;
void build(int left,int right,int nd)
{
int mid;
node[nd].left=left;
node[nd].right=right;
node[nd].c=-1;
if(left==right) return ;
mid=(left+right)/2;
build(left,mid,nd*2);
build(mid+1,right,nd*2+1);
}
int bsearch(int n,int s,int e)
{
int mid;
mid=(s+e)/2;
while(n!=rem[mid])
{
mid=(s+e)/2;
if(n>rem[mid]) s=mid+1;
else if(n<rem[mid]) e=mid-1;
}
return mid;
}
void update(int left,int right,int per,int nd)
{
int mid;
if(node[nd].c==per) return ;
if(left==node[nd].left&&right==node[nd].right)
{
node[nd*2].c=node[nd*2+1].c=node[nd].c;
node[nd].c=per;
return ;
}
if(node[nd].c!=-1)
{
node[nd*2].c=node[nd].c;node[nd*2+1].c=node[nd].c;
node[nd].c=-1;
}
mid=(node[nd].left+node[nd].right)/2;
if(right<=mid) update(left,right,per,nd*2);
else if(left>mid) update(left,right,per,nd*2+1);
else
{
update(left,mid,per,nd*2);
update(mid+1,right,per,nd*2+1);
}
}
void query(int nd)
{
if(used[node[nd].c]) return;
if(node[nd].c!=-1)
{
used[node[nd].c]=1;ans++;
return ;
}
query(nd*2);
query(nd*2+1);
}
int main()
{
int i,j,cas,n,cnt,r,l,num;
scanf("%d",&cas);
while(cas--)
{
cnt=0;num=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d %d",&left[i],&right[i]);
rem[cnt++]=left[i];
rem[cnt++]=right[i];
}
sort(rem,rem+cnt);
j=1;
for(i=1;i<cnt;i++)
{
if(rem[i]!=rem[i-1])
rem[j++]=rem[i];
}
cnt=j;
sort(rem,rem+cnt);
build(0,cnt-1,1);//
memset(used,0,sizeof(used));
for(i=1;i<=n;i++)
{
l=bsearch(left[i],0,cnt-1);//
r=bsearch(right[i],0,cnt-1);//
update(l,r,i,1);
}
ans=0;
query(1);
printf("%d\n",ans);
}
return 0;
}
- Mayor's posters 线段树+离散化+曾经RE无数边
- 【线段树+离散化】Mayor's Posters
- 线段树+离散化Mayor's posters
- 离散化+线段树 POJ 2528 (Mayor's posters)
- POJ 2528 Mayor's posters(离散化+线段树)
- poj 2528 Mayor's posters 离散化 线段树
- pku 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 (线段树+离散化)
- pku 2528 Mayor's posters(线段树+离散化)
- poj 2528 Mayor's posters(线段树+离散化)
- POJ 2528 Mayor's posters 线段树+离散化
- poj - 2528 - Mayor's posters 线段树+离散化
- 22/7/2012 ICPC培训 第七天
- 湘潭市赛 Josephus Problem 线段树
- 开发流程
- 决定写个天气预报
- 新浪微博客户端 -- OAuth认证
- Mayor's posters 线段树+离散化+曾经RE无数边
- 部分复杂类型的总结
- linux进程的休眠(等待队列)
- Linux进程的睡眠和唤醒简析
- 广州 Android 2011 3K
- hdu1542 面积交详细讲解
- C++Builder之Edit和Memo
- poj 2051 Argus
- hdu1542 Atlantis 面积交