【Codeforces 1420 D】Rescue Nibel!,卢卡斯组合数,排序,枚举贡献

举报
小哈里 发表于 2022/04/29 22:05:45 2022/04/29
【摘要】 problem D. Rescue Nibel! time limit per test2 seconds memory limit per test256 megabytes inputstandar...

problem

D. Rescue Nibel!
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins… nothing happened.

Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door.

There are n lamps with Spirit Tree’s light. Sein knows the time of turning on and off for the i-th lamp — li and ri respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on.

While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998244353.

Input
First line contains two integers n and k (1≤n≤3⋅105, 1≤k≤n) — total number of lamps and the number of lamps that must be turned on simultaneously.

Next n lines contain two integers li ans ri (1≤li≤ri≤109) — period of time when i-th lamp is turned on.

Output
Print one integer — the answer to the task modulo 998244353.

Examples
inputCopy
7 3
1 7
3 8
4 5
6 7
1 3
5 10
8 9
outputCopy
9
inputCopy
3 1
1 1
2 2
3 3
outputCopy
3
inputCopy
3 2
1 1
2 2
3 3
outputCopy
0
inputCopy
3 3
1 3
2 3
3 3
outputCopy
1
inputCopy
5 2
1 3
2 4
3 5
4 6
5 7
outputCopy
7
Note
In first test case there are nine sets of k lamps: (1,2,3), (1,2,4), (1,2,5), (1,2,6), (1,3,6), (1,4,6), (2,3,6), (2,4,6), (2,6,7).

In second test case k=1, so the answer is 3.

In third test case there are no such pairs of lamps.

In forth test case all lamps are turned on in a time 3, so the answer is 1.

In fifth test case there are seven sets of k lamps: (1,2), (1,3), (2,3), (2,4), (3,4), (3,5), (4,5).

solution

/*
题意:
+ 给出n盏灯和他们的打开关闭时间,选择k盏灯。
+ 求满足在某一时刻同时打开的这k盏灯的选择方案数。
思路:
+ 将所有灯按开始和结束时间从小到大排序,逐个枚举每盏灯
+ 考虑当前灯对答案的贡献,即记前面开始比他早的灯中跟他重合的灯的个数为x,贡献即为C(x,k-1),他们与当前灯共同构成一种不重复的情况。可以用优先队列维护。
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
const int mod = 998244353;

LL fac[maxn], inv[maxn];
LL mpow(LL a, LL x) {
	if(x==0)return 1;
	LL t = mpow(a, x>>1);
	if(x%2==0)return t*t%mod;
	return t*t%mod*a%mod;
}
LL init(){
	fac[0]=inv[0]=1;
	for(int i = 1; i < maxn; i++){
		fac[i]=fac[i-1]*i%mod; inv[i]=mpow(fac[i],mod-2);
	}return 0;
}
LL C(int x, int y) {
	if(y<0 || y>x)return 0;
	return fac[x]*inv[y]%mod*inv[x-y]%mod;
}

int main(){
	ios::sync_with_stdio(false);
	LL n, k;  cin>>n>>k;
	vector<pair<LL,LL> >a(n);
	for(auto &[l, r]: a)cin>>l>>r;
	sort(a.begin(),a.end());
	
	LL ans = 0;  init();
	priority_queue<LL, vector<LL>, greater<LL> >q;
	for(int i = 0; i < n; i++){
		auto [l,r] = a[i];
		while(q.size() && q.top()<l)q.pop();
		ans += C(q.size(), k-1);
		ans %= mod;
		q.push(r);
	}
	cout<<ans<<"\n";
	return 0;
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/113924878

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。