涂色问题 乘法原理(2024CCPC 山东省赛 C)

news/2024/10/6 17:58:31 标签: c语言, 算法, 开发语言

//*下午打得脑子连着眼睛一起疼

很多很基础的题目都没有做出来,规律题也找得很慢。比如下面这题,一定要多做,下次看到就直接写。


原题链接:https://codeforces.com/group/w6iGs8kreW/contest/555584/problem/C

C. Colorful Segments 2

time limit per test

2 seconds

memory limit per test

1024 megabytes

Consider nn segments on the number axis, where the left endpoint of the ii-th segment is lili and the right endpoint is riri. You need to paint each segment into one of the kk colors, so that for any two segments with the same color they do not overlap.

Calculate the number of ways to color the segments.

We say segment ii overlaps with segment jj, if there exists a real number xx satisfying both li≤x≤rili≤x≤ri and lj≤x≤rjlj≤x≤rj.

We say two ways of coloring the segments are different, if there exists one segment which has different colors in the two ways.

Input

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and kk (1≤n≤5×1051≤n≤5×105, 1≤k≤1091≤k≤109) indicating the number of segments and the number of colors.

For the following nn lines, the ii-th line contains two integers lili and riri (1≤li≤ri≤1091≤li≤ri≤109) indicating the left and right endpoints of the ii-th segment.

It's guaranteed that the sum of nn of all test cases will not exceed 5×1055×105.

Output

For each test case output one line containing one integer indicating the answer. As the answer might be large, output it modulo 998244353998244353.


大致题意  :  一共给k个颜色,有n个区间。规定两个重叠区间不能染相同颜色。问有多少种染色方法。 其实想很好想,就是 排序 遍历考虑出每个区间的颜色种数(只受其前面区间的限制) 乘法原理相乘 即可。

WA:一开始是把每个点的右端点排序,然后数这个区间与前面多少区间有重叠,如果与m个重叠,那可填颜色数就是k-m。但是!这样不对,例如:

(搞笑啊)

一号和二号都能填k种,但三号不止能填k-2种(当1 2填的颜色一样时,3能填k-1种)。

so右端点排序不对!

正解:左端点排序,可以用一个优先队列来储存,每段的初始颜色都是k-1(与上段不同),如果与上段没有重合,k++。否则break。

看代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define int long long 
 
int t;
int n,k;
const int mod=998244353;
 
struct O{
	int l,r;
}a[1000010];
vector<int>q;
 
bool sorrt(struct O i,struct O j){
	return i.l<j.l;
}
 
void solve(int n,int k){
	priority_queue<int,vector<int>,greater<int>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i].l>>a[i].r;
	}
	sort(a+1,a+n+1,sorrt);
	int ans=k%mod;
	k--;
	q.push(a[1].r);
	for(int i=2;i<=n;i++){
		while(!q.empty()&&q.top()<a[i].l){
			k++;
			q.pop();
		}
		ans=(ans*k)%mod;
		k--;
		if(k<0)k=0;
		q.push(a[i].r);
		
	}
	cout<<ans<<endl;
}
 
signed main()
{
	cin>>t;
	while(t--){
		cin>>n>>k;
		solve(n,k);
	}
	return 0;
}


http://www.niftyadmin.cn/n/5691963.html

相关文章

如何在 SQL 中创建一个新的数据库?

在SQL中创建一个新的数据库&#xff0c;首先你需要有一个可以执行SQL语句的环境。 这通常意味着你已经有了一个数据库管理系统&#xff08;DBMS&#xff09;&#xff0c;如MySQL、PostgreSQL、Oracle或Microsoft SQL Server等。 不同的DBMS可能有不同的细节&#xff0c;但基本…

每日读则推(五)——Rose

n/v. 照顾 n.护理 We sponsored the care these dogs needed and most of them found forever homes! One n.赞助者,赞助商 v.赞助(活动、节目等),为慈善活动捐资 lovely senior, Rose, is still waiting for hers. Shes mostly blind and needs eye medication, …

讯飞星火编排创建智能体学习(五):变量和文本拼接

引言 在讯飞星火编排创建智能体学习&#xff08;四&#xff09;&#xff1a;网页读取-CSDN博客中&#xff0c;我介绍了如何用网页读取功能从网上搜索车次信息。其中&#xff0c;我使用用大模型节点从文本中提取车次并合成了所需要的URL&#xff0c;今天介绍一下如何用变量和文…

k8s的简介和部署

一、k8s简介 在部署应用程序的方式上面&#xff0c;主要经历了三个阶段&#xff1a; 传统部署:互联网早期&#xff0c;会直接将应用程序部署在物理机上优点:简单&#xff0c;不需要其它技术的参与缺点:不能为应用程序定义资源使用边界&#xff0c;很难合理地分配计算资源&…

简单认识 redis -3 -其他命令

一.Redis HyperLogLog Redis HyperLogLog 是用来做基数统计的算法&#xff0c;HyperLogLog 的优点是&#xff0c;在输入元素的数量或者体积非常非常大时&#xff0c;计算基数所需的空间总是固定 的、并且是很小的。每个 HyperLogLog 键只需要花费 12 KB 内存&#xff0c;就可以…

(一)Web 网站服务之 Apache

一、Apache 的作用和特点 作用&#xff1a;Apache 是一款开源的网站服务器端软件&#xff0c;为网站的运行提供了稳定的基础。特点&#xff1a; 开源免费&#xff1a;这使得任何人都可以免费使用和修改它。模块化设计&#xff1a;具有高度的灵活性&#xff0c;可以根据需求选择…

vSAN04:vSAN远程数据存储挂载、双节点集群介绍/安装/组件读写/高级配置/故障处理方式

目录 vSAN远程数据存储挂载双节点vSAN集群介绍双节点vSAN集群安装双节点vSAN集群的组件读写方式双节点vSAN的高级配置双节点vSAN故障处理方式 vSAN远程数据存储挂载 在同一个vCenter下的VSAN集群可以互相挂载对方VSAN存储&#xff0c;以达到提高资源利用率的目的。 一个集群最…

《PyTorch深度学习快速入门教程》学习笔记(第15周)

目录 摘要 Abstract 1. 安装Anaconda 2. 查看显卡驱动 3. 安装Pytorch 4. Pytorch加载数据 5. 常用数据集两种形式 6. 路径直接加载数据 7. Dataset加载数据 摘要 本周报的目的在于汇报《PyTorch深度学习快速入门教程》课程第一周的学习成果&#xff0c;主要聚焦于py…