热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

南阳理工题目9:posters(离散化+线段树)

posters时间限制:1000ms|内存限制:65535KB难度:6描述ThecitizensofBytetown,AB,couldnotstandthatthec

posters

时间限制:1000 ms  |  内存限制:65535 KB难度:6
 
描述
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. 
 
输入
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.
输出
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. 
http://acm.pku.edu.cn/JudgeOnline/images/2528_1.jpg
样例输入
1
5
1 4
2 6
8 10
3 4
7 10

样例输出
4
来源
POJ
上传者
iphxer

 

  离散化+线段树

  难度较高的一道线段树的题,用了离散化处理。在南阳理工学院的oj(http://acm.nyist.net/JudgeOnline/problem.php?pid=9)上提交成功,但是在poj上提交WA,不知道为什么……听说poj上测试数据有些问题,还要算上端点两侧的点??不清楚,有知道的同仁告诉我一下,拜谢。。。

  题意

  按顺序给你一些海报,这些海报可能相互重叠,求最后没有被完全盖住的海报的数量。

  给你海报的两个端点的位置,最多有10000张海报,但是位置最多可以是10000000。

  思路

  由于涉及到区间,首先应该想到线段树,但是海报的宽度太大,如果给你一个1-10000000大小的海报怎么办?肯定MLE超内存啊(这的new多少节点)。所以就要用到离散化的方法,听起来很神秘,但理解起来很简单,例如给你两张海报,[1,6]和[3,10],排序之后是1,3,6,10,离散化为1,2,3,4,即第一个区间为[1,3],第二个区间为[2,4],如此,创建一个[1,4]的线段树即可,而不用创建[1,10]的线段树,节省了空间。

  代码

 1 #include 
2 #include
3 #include <string.h>
4 #include
5 using namespace std;
6
7 #define MAXN 20000
8 bool tree[MAXN*4+1]; //存储区间有无海报
9 short hash[10000010]; //离散化后的端点位置
10 short x[MAXN+10]; //存储海报的两个端点
11 struct post{
12 short L;
13 short R;
14 }a[MAXN+10]; //原数组
15 int cnt; //记录露在外面的海报数量
16
17 bool Insert(int d,int L,int R,int l,int r)
18 {
19 if(tree[d])
20 return false;
21 if(L==l && R==r){ //找到区间
22 tree[d] = true;
23 return true;
24 }
25
26 int mid = (L+R)/2;
27 bool res;
28 if(mid>=r){
29 res = Insert(d<<1,L,mid,l,r);
30 }
31 else if(mid<l){
32 res = Insert(d<<1|1,mid+1,R,l,r);
33 }
34 else {
35 bool f1 = Insert(d<<1,L,mid,l,mid);
36 bool f2 = Insert(d<<1|1,mid+1,R,mid+1,r);
37 res = f1||f2;
38 }
39 if(tree[d<<1] && tree[d<<1|1])
40 tree[d] = true;
41 return res;
42 }
43
44 int main()
45 {
46 int T;
47 scanf("%d",&T);
48 while(T--){
49 memset(tree,0,sizeof(tree));
50 memset(hash,0,sizeof(hash));
51 memset(x,0,sizeof(x));
52 int i,n,nCount=0;
53
54 //输入
55 scanf("%d",&n);
56 for(i=0;i){
57 scanf("%d%d",&a[i].L,&a[i].R);
58 x[nCount++] = a[i].L;
59 x[nCount++] = a[i].R;
60 }
61
62 sort(x,x+nCount);
63 nCount = unique(x,x+nCount) - x; //去重
64
65 int key=1;
66 for(i=0;i//离散化处理
67 hash[x[i]] = key;
68 if(i1){
69 if(x[i+1]-x[i]==1)
70 key++;
71 else
72 key=key+2;
73 }
74 }
75
76 cnt = 0;
77 for(i=n-1;i>=0;i--){
78 if(Insert(1,1,key,hash[a[i].L],hash[a[i].R])) //将离散化后的海报插入到线段树中
79 cnt++;
80 }
81 printf("%d\n",cnt);
82 }
83 return 0;
84 }

 

Freecode : www.cnblogs.com/yym2013


推荐阅读
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文总结了Java中日期格式化的常用方法,并给出了示例代码。通过使用SimpleDateFormat类和jstl fmt标签库,可以实现日期的格式化和显示。在页面中添加相应的标签库引用后,可以使用不同的日期格式化样式来显示当前年份和月份。该文提供了详细的代码示例和说明。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了如何将CIM_DateTime解析为.Net DateTime,并分享了解析过程中可能遇到的问题和解决方法。通过使用DateTime.ParseExact方法和适当的格式字符串,可以成功解析CIM_DateTime字符串。同时还提供了关于WMI和字符串格式的相关信息。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
author-avatar
漆黑中的萤火虫
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有