POJ 2528 Mayor's posters(线段树 + 离散化--静态实现)
2016-07-26 23:00
239 查看
Mayor's posters
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
Sample Output
Source
Alberta Collegiate Programming Contest 2003.10.18
之前写过线段树的动态实现,现在借此题搞搞静态的实现方法
静态实现不借助指针指向两个子节点,而是一个线性结构,借助计算(node*2)(node*2 - 1)来指明两个子节点
不过这题难点是离散化和判断海报能否最终被看到
1、这题的离散化是有点特殊的
例如这样一个样例
1 10
1 5
6 10
那么这个样例应该答案是2
1 5 6 10,离散化后是0 1 2 3,答案是对的
如果样例换成
1 10
1 4
6 10
1 4 6 10,离散化后还是0 1 2 3,这样答案就错了,因为没有考虑到4和6之间还有空
我们只要加一层循环就可以了,看一下如果后一个数字a[i]比前一个a[i-1]大的超过1,那么就在原有数组的基础上加上一个a[i-1]+1,那么1 4 6 10离散化后就是1 2 4 5 6 7 10
2、在判断海报是,从后往前判断,这样省事很多
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e5 + 100;
struct Post {
int l, r;
} post[MAXN];
struct Node {
int st, ed;
int val;
bool vis;
} tree[MAXN << 4];
int t, n;
int disp[MAXN << 4];
bool canPost;
void Build(int node, int st, int ed) {
tree[node].st = st;
tree[node].ed = ed;
tree[node].val = 0;
tree[node].vis = false;
if (st == ed) {
return;
}
int mid = st + ed >> 1;
Build(node << 1, st, mid);
Build(node << 1 | 1, mid + 1, ed);
}
void PushUp(int node) {
if (tree[node << 1].vis && tree[node << 1 | 1].vis) {
tree[node].vis = true;
}
}
void Update(int node, int st, int ed, int val) {
if (tree[node].vis) {
return;
}
if (tree[node].st == st && tree[node].ed == ed) {
tree[node].vis = true;
canPost = true;
return;
}
if (tree[node << 1].ed >= ed) {
Update(node << 1, st, ed, val);
}
else if (tree[node << 1 | 1].st <= st) {
Update(node << 1 | 1, st, ed, val);
}
else {
Update(node << 1, st, tree[node << 1].ed, val);
Update(node << 1 | 1, tree[node << 1 | 1].st, ed, val);
}
PushUp(node);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int cnt = 0;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &post[i].l, &post[i].r);
disp[cnt++] = post[i].l;
disp[cnt++] = post[i].r;
}
sort(disp, disp + cnt);
cnt = unique(disp, disp + cnt) - disp;
for (int i = 1, ed = cnt; i < ed; ++i) {
if (disp[i] - disp[i - 1] != 1) {
disp[cnt++] = disp[i - 1] + 1;
}
}
sort(disp, disp + cnt);
Build(1, 1, cnt);
int ans = 0;
for (int i = n - 1; i >= 0; --i) {
int hashl = lower_bound(disp, disp + cnt, post[i].l) - disp + 1;
int hashr = lower_bound(disp, disp + cnt, post[i].r) - disp + 1;
canPost = false;
Update(1, hashl, hashr, i + 1);
if (canPost) ++ans;
}
printf("%d\n", ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 58066 | Accepted: 16794 |
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
Alberta Collegiate Programming Contest 2003.10.18
之前写过线段树的动态实现,现在借此题搞搞静态的实现方法
静态实现不借助指针指向两个子节点,而是一个线性结构,借助计算(node*2)(node*2 - 1)来指明两个子节点
不过这题难点是离散化和判断海报能否最终被看到
1、这题的离散化是有点特殊的
例如这样一个样例
1 10
1 5
6 10
那么这个样例应该答案是2
1 5 6 10,离散化后是0 1 2 3,答案是对的
如果样例换成
1 10
1 4
6 10
1 4 6 10,离散化后还是0 1 2 3,这样答案就错了,因为没有考虑到4和6之间还有空
我们只要加一层循环就可以了,看一下如果后一个数字a[i]比前一个a[i-1]大的超过1,那么就在原有数组的基础上加上一个a[i-1]+1,那么1 4 6 10离散化后就是1 2 4 5 6 7 10
2、在判断海报是,从后往前判断,这样省事很多
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e5 + 100;
struct Post {
int l, r;
} post[MAXN];
struct Node {
int st, ed;
int val;
bool vis;
} tree[MAXN << 4];
int t, n;
int disp[MAXN << 4];
bool canPost;
void Build(int node, int st, int ed) {
tree[node].st = st;
tree[node].ed = ed;
tree[node].val = 0;
tree[node].vis = false;
if (st == ed) {
return;
}
int mid = st + ed >> 1;
Build(node << 1, st, mid);
Build(node << 1 | 1, mid + 1, ed);
}
void PushUp(int node) {
if (tree[node << 1].vis && tree[node << 1 | 1].vis) {
tree[node].vis = true;
}
}
void Update(int node, int st, int ed, int val) {
if (tree[node].vis) {
return;
}
if (tree[node].st == st && tree[node].ed == ed) {
tree[node].vis = true;
canPost = true;
return;
}
if (tree[node << 1].ed >= ed) {
Update(node << 1, st, ed, val);
}
else if (tree[node << 1 | 1].st <= st) {
Update(node << 1 | 1, st, ed, val);
}
else {
Update(node << 1, st, tree[node << 1].ed, val);
Update(node << 1 | 1, tree[node << 1 | 1].st, ed, val);
}
PushUp(node);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int cnt = 0;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &post[i].l, &post[i].r);
disp[cnt++] = post[i].l;
disp[cnt++] = post[i].r;
}
sort(disp, disp + cnt);
cnt = unique(disp, disp + cnt) - disp;
for (int i = 1, ed = cnt; i < ed; ++i) {
if (disp[i] - disp[i - 1] != 1) {
disp[cnt++] = disp[i - 1] + 1;
}
}
sort(disp, disp + cnt);
Build(1, 1, cnt);
int ans = 0;
for (int i = n - 1; i >= 0; --i) {
int hashl = lower_bound(disp, disp + cnt, post[i].l) - disp + 1;
int hashr = lower_bound(disp, disp + cnt, post[i].r) - disp + 1;
canPost = false;
Update(1, hashl, hashr, i + 1);
if (canPost) ++ans;
}
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- [置顶] Android DocumentUi(下载管理)是如何向DownloadProvider中获取数据的
- [转载]图像缩放的双线性内插值算法的原理解析【转】
- 以另外一种方式写二叉树,感受很深啊!
- HDU 3366 Passage (概率DP)
- 多态
- Tesseract-OCR 字符识别---样本训练
- scala基础27-SAM转换
- EventBus3.0-线程之间传递
- php学习笔记(五)删除
- 第21条:用函数对象表示策略
- CentOS 7 : Docker私有仓库搭建和使用
- Mac 下 MySQL 5.7 连接 navicat 中文乱码问题终极解决方案
- java 获得项目绝对路径
- SQLserver总结(1)
- Spring IoC和DI——学习笔记
- cookie与session详解
- after伪元素实现空心三角箭头和X图标
- Linux学习篇之用户与用户组
- Windows概要
- setMobileDataEnabled方法不能再被调用 Android L and later