开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第2天,点击查看活动详情
Educational Codeforces Round 71 (Rated for Div. 2)
Problem - 1207D - Codeforces
You are given a sequence of nn pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences:
- s=[(1,2),(3,2),(3,1)] is bad because the sequence of first elements is sorted: [1,3,3]
- s=[(1,2),(3,2),(1,2)] is bad because the sequence of second elements is sorted: [2,2,2];
- s=[(1,1),(2,2),(3,3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted;
- s=[(1,3),(3,3),(2,2)] is good because neither the sequence of first elements ([1,3,2]) nor the sequence of second elements ([3,3,2]) is sorted.
Calculate the number of permutations of size nn such that after applying this permutation to the sequence ss it turns into a good sequence.
A permutation pp of size nn is a sequence p1,p2,…,pnp1,p2,…,pn consisting of nn distinct integers from 11 to nn (1≤pi≤n1≤pi≤n). If you apply permutation p1,p2,…,pnp1,p2,…,pn to the sequence s1,s2,…,sns1,s2,…,sn you get the sequence sp1,sp2,…,spnsp1,sp2,…,spn. For example, if s=[(1,2),(1,3),(2,3)] and p=[2,3,1] then ss turns into [(1,3),(2,3),(1,2)].
Input
The first line contains one integer nn (1≤n≤3⋅10^5).
The next nn lines contains description of sequence ss. The ii-th line contains two integers aiai and bibi (1≤ai,bi≤n) — the first and second elements of ii-th pair in the sequence.
The sequence ss may contain equal elements.
Output
Print the number of permutations of size nn such that after applying this permutation to the sequence ss it turns into a good sequence. Print the answer modulo 998244353 (a prime number).
Examples
input
3
1 1
2 2
3 1
output
3
Note
In first test case there are six permutations of size 33:
- if p=[1,2,3]p=[1,2,3], then s=[(1,1),(2,2),(3,1)]s=[(1,1),(2,2),(3,1)] — bad sequence (sorted by first elements);
- if p=[1,3,2]p=[1,3,2], then s=[(1,1),(3,1),(2,2)]s=[(1,1),(3,1),(2,2)] — bad sequence (sorted by second elements);
- if p=[2,1,3]p=[2,1,3], then s=[(2,2),(1,1),(3,1)]s=[(2,2),(1,1),(3,1)] — good sequence;
- if p=[2,3,1]p=[2,3,1], then s=[(2,2),(3,1),(1,1)]s=[(2,2),(3,1),(1,1)] — good sequence;
- if p=[3,1,2]p=[3,1,2], then s=[(3,1),(1,1),(2,2)]s=[(3,1),(1,1),(2,2)] — bad sequence (sorted by second elements);
- if p=[3,2,1]p=[3,2,1], then s=[(3,1),(2,2),(1,1)]s=[(3,1),(2,2),(1,1)] — good sequence.
问题解析
题目要求ai序列和bi序列都排成不是单调非降的情况,而不是单调非降的排法正常情况来说有太多太多了,直接计算过于困难。
我们可以换一个方向来解决问题,根据容斥定理,我们知道:结果=总排列数 - ai序列单调非降排列数 - bi序列单调非降排列数 + ai和bi序列同时单调非降排列数。
总排列数:n的阶乘;
- ai/bi序列单调非降排列数:各个不同元素(ai或bi)的出现次数的阶乘的和;
- ai和bi序列同时单调非降排列数:各个不同元素({ai,bi})的出现次数的阶乘的和;
注意,如果对{ai,bi}序列排序后,出现bi+1<bi的情况,则ai和bi序列同时单调非降排列数为0.
AC代码
const int N = 3e5 + 50, MOD = 998244353;
//a/b的出现次数
map<int, int>a, b;
//{a,b}的出现次数
map<PII, int>mymap;
//f[i]表示i的阶乘
int f[N];
void solve()
{
int n, res = 1, ans = 1;
f[0] = 1;
cin >> n;
vector<PII>c(n);
for (int i = 1; i <= n; i++)
{
f[i] = (f[i - 1] * i) % MOD;
cin >> c[i-1].first >> c[i-1].second;
a[c[i-1].first]++;
b[c[i-1].second]++;
mymap[c[i-1]]++;
}
res = f[n];
sort(c.begin(), c.end());
for (auto& i : a)ans = (ans * f[i.second]) % MOD;
res = (res - ans + MOD) % MOD;
ans = 1;
for (auto& i : b)ans = (ans * f[i.second]) % MOD;
res = (res - ans + MOD) % MOD;
ans = 1;
for (auto& i : mymap)ans = (ans * f[i.second]) % MOD;
for (int i = 1; i < n; i++)
{
if (c[i].second < c[i - 1].second)
{
ans = 0;
break;
}
}
res = (res + ans) % MOD;
cout << res;
}