跳到正文

个人记录

浮水法学习笔记

STL_qwq约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年4月30日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 2 分钟

版本

  1. v1当前

参考资料:链接

一种用来解决区间染色问题的算法(因为窝不会线段树

主要思路是倒序处理线段(因为后覆盖的能挡住先覆盖的),然后一层一层往后浮,要是被挡住了就让没挡住的那一段往上浮,能浮到最上层说明对答案有贡献。

C++
void work(int ll,int rr,int t,int tot){
	/*
    ll 记现在左边界
    rr 记现在右边界
    t 记现在的层数
    tot记编号防止重复上浮
   */
	if(vis[tot]) return ;//如果有一部分浮到顶了就不用再浮了 
	while(t<=m&&(ll>=r[t]||rr<=l[t])) t++;//整个都能往上浮 
	if(t>m) ans++,vis[tot]=1;//浮到最顶端了,能被看到 
	if(ll<l[t]&&rr>l[t]) work(ll,l[t],t+1,tot);
	if(rr>r[t]&&ll<r[t]) work(r[t],rr,t+1,tot);
	//没挡住的向上浮 
}

然后是例题:P3740 [HAOI2014]贴海报

code:

C++
#include<cstdio>
#include<iostream>

using namespace std;

int n,m,now,ans;
const int N=1e4+10;
int l[N],r[N];
bool vis[N];

void work(int ll,int rr,int t,int tot){
	if(vis[tot]) return ;
	while(t<=m&&(ll>=r[t]||rr<=l[t])) t++;
	if(t>m) ans++,vis[tot]=1;
	if(ll<l[t]&&rr>l[t]) work(ll,l[t],t+1,tot);
	if(rr>r[t]&&ll<r[t]) work(r[t],rr,t+1,tot);
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=m;i++){
		scanf("%d %d",&l[i],&r[i]);
		r[i]++;
	}
	for(int i=m-1;i>=1;i--) work(l[i],r[i],i+1,i);
	printf("%d",ans+1);//还得算最后一张
	return 0;
} 

本蒟蒻退役好久了,如有错误敬请指正。

原文
存档评论3 条抓取于