poj 2528 Mayor's poster

链接:http://poj.org/problem?id=2528

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 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

PS:此题是第一次接触离散化,此题数据有问题,NYOJ 9跟这是原题,POJ可以过,NYOJ不能过,测试

3

1 10  1 3 6 10 正确答案应该是3.过的代码中确实2.根本原因是离散化中的相同数值.这个例子中被离散成了1 4 1 2 3 4.所以答案就是2了.

题意:一面墙用来放置海报,由于宽度的范围很大,若不离散化,则线段树会造成严重浪费~

离散化:离散化是程序设计中一个非常常用的技巧,它可以有效的降低时间复杂度。其基本思想就是在众多可能的情况中“只考虑我需要用的值”。离散化可以改进一个低效的算法,甚至实现根本不可能实现的算法。要掌握这个思想,必须从大量的题目中理解此方法的特点。

简单的说就是把需要的用的数据离散在一起.增加时间和空间的效率.

此题通过排序的方法进行离散.给定这样的一个例子.<2000,4000>,<3000,5000>,<8000,10000>.

通过排序得 2000 3000 4000 5000 8000 10000

  映射     1   2    3   4   5    6

那么以上则可以这样表示<1,3>,<2,4>,<5,6>.

AC code: (虽然是错的~~睡觉)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
#define N 100008
int t;
int pos[N][2];
struct SegTree
{
	int left,right,col; 
}STree[N*4];
struct Lishanhua
{
	int point,num;
}List[N*2];
int cmp(const void* p1,const void* p2)
{
	return        ((*(Lishanhua*)p1).point>(*(Lishanhua*)p2).point)?1:-1;
}
bool flag[N+1];
void build(int l,int r,int k)  //建树
{
             STree[k].left=l;
	    STree[k].right=r;
	    STree[k].col=0;
	    if(l==r) return;
	    int mid=(l+r)/2;
		build(l,mid,2*k);
		build(mid+1,r,2*k+1);
}
void Insert(int l,int r,int k,int c)      //插入线段
{

	if(STree[k].left==l&&STree[k].right==r)
	{
		STree[k].col=c;
		return ;
	}
	if(STree[k].col>0&&STree[k].col!=c)
	{
		STree[2*k].col=STree[k].col;
		STree[2*k+1].col=STree[k].col;
		STree[k].col=0;
	}
	int mid=(STree[k].right+STree[k].left)/2;
	if(r<=mid)
	Insert(l,r,2*k,c);
	else if(l>mid)
	Insert(l,r,2*k+1,c);
	else
	{
		Insert(l,mid,2*k,c);
		Insert(mid+1,r,2*k+1,c);
	}
}	
void Query(int k)
{
	if(STree[k].col!=0)
	{
		if(!flag[STree[k].col])
		{
			t++;
			flag[STree[k].col]=true;
		}
		return ;
	}
	Query(2*k);
	Query(2*k+1);
}

int main()
{
	int i,j,test,num;
	scanf("%d",&test);
	while(test--)
	{
		memset(flag,0,sizeof(flag));
		scanf("%d",&num);
		t=0;
		for(i=0;i<num;i++)
		{
			scanf("%d%d",&pos[i][0],&pos[i][1]);
	        List[i*2].point=pos[i][0];
	        List[i*2+1].point=pos[i][1];
	        List[i*2].num=-(i+1);
	        List[i*2+1].num=i+1;
		}
		qsort(List,2*num,sizeof(List[0]),cmp);     //排序
		int temp=List[0].point,mm=1;      //离散
		for(i=0;i<2*num;i++)
		{
			if(List[i].point!=temp)
			{
				mm++;
				temp=List[i].point;
			}
			if(List[i].num<0)
			pos[-(List[i].num)-1][0]=mm;
			else
			pos[List[i].num-1][1]=mm;
		}
		build(1,mm,1);
		for(i=0;i<num;i++)
		Insert(pos[i][0],pos[i][1],1,i+1);
		Query(1);
		printf("%d\n",t);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值