POJ 2528 Mayor's posters

本文探讨了一个关于市长竞选海报放置的算法问题,涉及到线段树数据结构的应用,具体讲解了如何通过线段树来解决海报覆盖的问题,以及如何计算最终可见的海报数量。

Mayor’s posters

https://vjudge.net/problem/POJ-2528
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
C++

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

const int MAXN = 10000 + 10;

struct node{
	int l, r, lazy;
}segTree[MAXN << 2];

int cnt, vis[MAXN], L[MAXN], R[MAXN], H[MAXN << 1];

void pushdown(int x)
{
	if(segTree[x].lazy != -1)
	{
		segTree[x << 1].lazy = segTree[x << 1 | 1].lazy = segTree[x].lazy;
		segTree[x].lazy = -1;
	}
}

void build(int x, int l, int r)
{
	segTree[x].l = l;
	segTree[x].r = r;
	if(l == r)
	{
		segTree[x].lazy = -1;
		return;
	}
	int mid = l + r >> 1;
	build(x << 1, l, mid);
	build(x << 1 | 1, mid + 1, r);
	segTree[x].lazy = -1;
}

void update(int x, int l, int r, int val)
{
	if(l <= segTree[x].l && segTree[x].r <= r)
	{
		segTree[x].lazy = val;
		return;
	}
	pushdown(x);
	int mid = segTree[x].l + segTree[x].r >> 1;
	if(r <= mid) update(x << 1, l, r, val);
	else if(l > mid) update(x << 1 | 1, l, r, val);
	else
	{
		update(x << 1, l, mid, val);
		update(x << 1 | 1, mid + 1, r, val);
	}
}

void query(int x, int l, int r)
{
	if(segTree[x].l == segTree[x].r)
	{
		if(segTree[x].lazy != -1)
		{
			if(!vis[segTree[x].lazy])
			{
				cnt ++;
				vis[segTree[x].lazy] = true;
			}
		}
		return;
	}
	pushdown(x);
	int mid = l + r >> 1;
	query(x << 1, l, mid);
	query(x << 1 | 1, mid + 1, r); 
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T --)
	{
		int n;
		memset(vis, false, sizeof(vis));
		cnt = 0;
		int tmp = 1;
		scanf("%d", &n);
		for(int i = 1; i <= n; i ++)
		{
			scanf("%d%d", &L[i], &R[i]);
			H[tmp ++] = L[i];
			H[tmp ++] = R[i];
		}
		sort(H + 1, H + tmp);
		int size = unique(H + 1, H + tmp) - H - 1;
		build(1, 1, size);
		int tl, tr;
		for(int i = 1; i <= n; i ++)
		{
			tl = lower_bound(H + 1, H + size + 1, L[i]) - H;
			tr = lower_bound(H + 1, H + size + 1, R[i]) - H;
			update(1, tl, tr, i);
		}
		query(1, 1, n);
		printf("%d\n", cnt);
	}
	return 0;
}
基于分布式模型预测控制的多个固定翼无人机一致性控制(Matlab代码实现)内容概要:本文围绕“基于分布式模型预测控制的多个固定翼无人机一致性控制”展开,采用Matlab代码实现相关算法,属于顶级EI期刊的复现研究成果。文中重点研究了分布式模型预测控制(DMPC)在多无人机系统中的一致性控制问题,通过构建固定翼无人机的动力学模型,结合分布式协同控制策略,实现多无人机在复杂环境下的轨迹一致性和稳定协同飞行。研究涵盖了控制算法设计、系统建模、优化求解及仿真验证全过程,并提供了完整的Matlab代码支持,便于读者复现实验结果。; 适合人群:具备自动控制、无人机系统或优化算法基础,从事科研或工程应用的研究生、科研人员及自动化、航空航天领域的研发工程师;熟悉Matlab编程和基本控制理论者更佳; 使用场景及目标:①用于多无人机协同控制系统的算法研究与仿真验证;②支撑科研论文复现、毕业设计或项目开发;③掌握分布式模型预测控制在实际系统中的应用方法,提升对多智能体协同控制的理解与实践能力; 阅读建议:建议结合提供的Matlab代码逐模块分析,重点关注DMPC算法的构建流程、约束处理方式及一致性协议的设计逻辑,同时可拓展学习文中提及的路径规划、编队控制等相关技术,以深化对无人机集群控制的整体认知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值