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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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
n 次操作,每次操作算一种颜色, 每次操作在 a 到 b 上 涂上一种颜色,绝对覆盖,问最后能看见多少种颜色。
a 和 b 很大,所以要离散化, 由于 只有 一万次操作,所以有 两万个点,离散后差不多两万个点。
把所有 a b 排序之后去重,然后在 按 1 2 3 4 这样 一一对应,就可以了。
1 10
1 4
7 10
这样的样例
1 4 7 10
↓ ↓ ↓ ↓
1 2 3 4
变成这样,
1 4
1 2
3 4
评测的时候 答案是 2 ,,是有一种颜色被离散的时候覆盖掉了,
所以,我觉得正确的离散得出来的结果是 3;
正确的离散,把两个点之间加一个点,应该就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define mem(x,v) memset(x,v,sizeof(x))
const int N = 4e4 + 10;
struct point{
int a,b,c;
bool operator < (const point &now) const {
return a < now.a;
}
}ed[N];
struct node{
int a,b,l,r,w,c;
int cal_m(){
return (a + b) / 2;
}
}f[N];
int n,t,x,y,z;
bool g[N];
void updata(int p){
if (f[p].c == 0) return;
f[f[p].l].c = f[f[p].r].c = f[p].c;
f[f[p].l].w = f[f[p].r].w = f[p].c;
f[p].c = 0;
return;
}
void build(int p, int a, int b){
f[p].a = a; f[p].b = b;
if (a + 1 == b){
return;
}
int m = f[p].cal_m();
t++; f[p].l = t; build(t,a,m);
t++; f[p].r = t; build(t,m,b);
return;
}
void Insert(int p){
if (x <= f[p].a && y >= f[p].b - 1){
f[p].w = f[p].c = z;
return;
}
updata(p);
int m = f[p].cal_m();
if (x < m) Insert(f[p].l);
if (y >= m) Insert(f[p].r);
return;
}
int main() {
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for (int i = 1; i <= n; i++){
scanf("%d%d",&ed[i].a,&ed[n + i].a);
ed[i].b = i;
ed[i + n].b = i + n;
}
sort(ed + 1, ed + 1 + 2 * n);
int tt = 1;
for (int i = 1; i <= 2 * n; i++){
ed[ed[i].b].c = tt;
if (ed[i].a != ed[i + 1].a) tt++;//这里我觉得是 tt += 2; 才是真正的离散化,不然会覆盖颜色。这样会变成 4 * n;
}
// for (int i = 1; i <= n; i++)
// printf("%d %d\n",ed[i].c, ed[i + n].c);
mem(f,0);
t = 1;
build(1,1,tt + 1);
for (int i = 1; i <= n; i++){
x = ed[i].c; y = ed[i + n].c; z = i;
Insert(1);
}
for (int i = 1; i <= t; i++)
if (f[i].c) updata(i);
mem(g,0);
int ans = 0;
for (int i = 1; i <= t; i++)
if (f[i].a + 1 == f[i].b && g[f[i].w] == 0 && f[i].w > 0){
ans++;
g[f[i].w] = 1;
}
printf("%d\n",ans);
}
return 0;
}
本文介绍了一种解决在巨大的选举墙上放置竞选海报的问题的方法。通过离散化处理大量且范围宽泛的位置坐标,确保每张海报完全覆盖指定的连续墙段,并在允许重叠放置的情况下计算最终可见海报的数量。

538

被折叠的 条评论
为什么被折叠?



