PKU 2528 Mayor's posters
题意:
一个公告板上面贴海报,宽度都是一样的,长度可能不一样。后面的海报可能把前面的覆盖掉。问最后能看见多少张不同的海报。
思路:
这题原来做过,是用线段树的区间染色写的。记录每个区间是纯色还是杂色。最后统计所有颜色。
今天发现可以用一种类似扫描线的想法来做。想象一条扫描线从左往右走。用set来维护当前位置对应的海报集合。然后记录当前位置最新(能被看到)的海报是哪一张。
最后统计一下能被看见的海报数量。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <time.h> using namespace std; const int INF = <<;
const int MAXN = (int) 1e4+; struct event {
int x, t; //位置,时间
int status; //进还是出 void init(int xx, int tt, int ss) {
x = xx;
t = tt;
status = ss;
} bool operator < (const event &_) const {
if (x!=_.x) return x<_.x;
//同一位置时,入边在出边前面
//同是入边时,时间大的在前面
//同是出边时,时间小的在前面
return status*t > _.status*_.t;
}
}a[MAXN<<]; set<int> S;
bool vis[MAXN];
int n; void solve() {
S.clear();
memset(vis, false, sizeof(vis));
int l, r; scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d%d", &l, &r);
a[i<<].init(l, i+, );
a[i<<|].init(r+, i+, -);
}
sort(a, a+n*); int ans = ;
set<int>::iterator p; for (int i = ; i < n*; i++) {
if (a[i].status>) S.insert(a[i].t);
else S.erase(a[i].t);
if (!S.empty()) {
p = S.end(); p--;
if (!vis[*p]) {
ans++;
vis[*p] = true;
}
}
}
printf("%d\n", ans);
} int main() {
#ifdef Phantom01
freopen("PKU2528.txt", "r", stdin);
#endif //Phantom01 int T;
scanf("%d", &T);
while (T--) {
solve();
} return ;
}
PKU 2528 Mayor's posters的更多相关文章
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ - 2528 Mayor's posters(dfs+分治)
POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2528 Mayor's posters
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
随机推荐
- [转]m3u8直播测试地址
http://www.cnblogs.com/yuandaozhe/p/5755453.html 调试m3u8的时候需要测试地址 找了几个,备用一下 安徽卫视 http://stream2.ahtv. ...
- 重温前端基础之-js排序算法
javascript数组常用的方法: push():返回值,数组新的长度 pop():返回值,被删除的数组末尾元素 shift():返回值,被删除的数组首部元素 unshift():返回值,数组新的长 ...
- 51nod 1158 全是1的最大子矩阵(单调栈 ,o(n*m))
前置问题:51nod 1102 面积最大的矩形 附上链接: 51nod 1102 面积最大的矩形 这题的题解博客 需要了解的知识:单调栈,在前置问题中已经讲解. 解题思路 对每行求左边连续1的个数,得 ...
- 利用canvas做一个简单个gif停止和播放
var ImagePlayer = function(options) { this.control = options.control; this.image = options.image; th ...
- 1028C:Rectangles
You are given n rectangles on a plane with coordinates of their bottom left and upper right points. ...
- http_build_query 字符串拼接
http_build_query 字符串拼接 产生一个urlencode之后的请求字符串. 1.将数组转化成url问号(?)后的字符串 <?php $date=array( 'name'=> ...
- Mac 如何修改Mac系统的默认截图路径
step 1 :打在桌面或者其他任意位置创建一个文件夹:截图图库.我创建的路径是:/Users/yilin/Documents/截图图库(仅供参考) step 2:打开终端,输入以下命令:defaul ...
- Python 批量处理特定格式文件
#批量对文件夹下的'.mat'进行处理 def file_name(file_dir,suff): L=[] for root, dirs, files in os.walk(file_dir): f ...
- LAMP 建立 Wordpress 站点 Linux Apache MariaDB PHP
使用LAMP建立Wordpress, 要求如下: 准备工作: VMware 14 CentOS 7.4 最小化 安装镜像 Wordpress 安装包, 下载 预热: 使用VMware新建4台虚拟机, ...
- CSS常用样式--background
CSS background 属性 参考:W3school- CSS background 所有浏览器都支持 background 属性,其简写形式,在一个声明中设置所有的背景属性,各属性需按顺序,语 ...