Mayor's posters
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 26435 |
|
Accepted: 7624 |
Description
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral
wall for placing the posters and introduce the following rules:
- 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
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they
were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li
<= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.
Output
For each input data set print the number of visible posters after all the posters are placed.
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;
}
相关推荐
POJ2528-Mayor's posters 【区间映射压缩(离散化)+线段树】 解题报告+AC代码 http://hi.csdn.net/!s/83XZJU ========> 我的所有POJ解题报告链接: http://blog.csdn.net/lyy289065406/article/details/6642573
用线段树计算贴海报不被覆盖的海报数量, 首先需要对整个海报进行离散化处理
poj-2528 Mayor's posters 测试数据及答案
POJ2528-Mayor's posters 测试数据。数据来源:Alberta Collegiate Programming Contest 2003.10.18 – 问题G
Data_on_Prefectural_Party_Secretary_and_Mayor_of_P._R._China,_2000-2010(02)
Mayor & ESI-2019纽约夜生活经济报告(英文)-2019.12-80页.rar
jason_wu_mayor
mayor0v.github.io
persona-mayor-csharp:尖锐的
numero-mayor-o-menor
布拉德利塔静态 这是洛杉矶市市长的仪表板。... 数据存储在Google文档中。... 可视化:交互式图表,图形,地图,可深入探究关键政策/问题领域。 这些图表通常是使用D3或CartoDB手动构建的,尽管有些图表是通过与Socra
U3E4-Numero-menor-mayor
/*初始化*/ void Initlist(Sqlist &L) { L.length=0; } /*按学号查找*/ int Binsearch(Sqlist &L,int n) { int i,j; int low=0,high=n-1,mid; char k[20]; Linelist temp; system("cls"); for(i=1;i;i...
Las impresoras HP LaserJet 4200 el LaserJet 4300 series fueron ... La mayor difer- ncia está en el diseño de la tolva el toner. La de la HP4300 es más rande y carga 1050 gramos de
授权人作者识别码使用: 贝叶斯冒名顶替者稀疏表示在 PAN14 中运行的说明训练三种方法bash 脚本/train_pan14.sh -i TRAINDIR -o MODELDIR贝叶斯测试bash 脚本/test_pan14_bayes.sh -i INPUTDIR -m MODELDIR -o ...
Mayor & ESI-2019纽约夜生活经济报告(英文)-2019.12-80页.pdf
什么是代理模式呢? 简单来说就像小朋友报名上学,小朋友本身肯定没有做到这种事的能力,这时候他妈妈就可以帮他报名,然后告诉小朋友报好名啦,可以读书了。 这里是一个简单的ProxyPatternDemo。
主要次要数字程序,用于获取用Java键盘输入的列表的最大和最小数目(无需初始化变量)
超级市长列出最近在芝加哥市311系统中创建,更新或关闭的服务请求。 它使用来自市民数据。 特征: 服务请求在创建/更新/关闭时出现在市长跳过新提交的请求并在更新/关闭请求时收集框时,观看(和收听)市长背景变化...
C)To resign from his position in the woman's company. D)To exchange stock market infotmation with the woman. <br>20. A)He is head of a small teading company. B)He works in an ...