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 ...
随机推荐
- GET,POST请求
get请求 post请求
- React 第二天
第二天 01 关于Vue和React中key的作用 在循环的时候一定要为组件加key 02关于jsx语法的注意事项 jsx中的注释 {/* */} class要写成className label标签 ...
- python编写登录与注册
#编写简单的注册与登陆模块 #使用死循环来检测 while True: #如果条件为真,则一直循环 sum=3 #定义密码输入的次数 username = input("请输入用户名:&qu ...
- salt 安装kubernetes集群3节点
[root@linux-node1 k8s]# tree .├── etcd.sls├── files│ ├── cfssl-1.2│ │ ├── cfssl-certinfo_linux ...
- Vim配置及使用
Vim配置 1.打开~/.vimrc,将以下内容考入文件.vimrc中 "行号" set nu "高亮" syntax enable syntax on &qu ...
- 关于iptables允许samba的问题
今天同事跟我说他们部门的共享不能用了,想了想,最近变更的只有iptables,于是看看是否是这个原因,发现没有允许samba的入站和出站规则,我的iptables规则默认是所有都drop的,但是不知确 ...
- 洛谷 P1541 乌龟棋 (四维费用背包)
一开始直接用01背包 后来发现这个物品和位置有关. 也就是价值不是固定的 后来看了题解 看了卡片最多就4 所以这是一个四维费用的背包, 每一维是卡片的数量 价值就是当前的位置的价值. 但是与常规的背包 ...
- Python学习第一天-编写登陆接口
编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 帐号文件user.txt内容如下: qaz 123qwe 12345qweqwr 12321424...... 锁文件user_l ...
- Mybaties下的分页功能的实现
jsp页面 <!-- 页码 --> <div class="ipRListNav2"> <a href="zyxx.do?findZyxx& ...
- hadoop-02-关闭防火墙
hadoop-02-关闭防火墙 su root service iptables status #查看状态 即时关闭: service iptables stop #关闭 重启之后关闭: chkcon ...