双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)

Posted --hpy-7m

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)相关的知识,希望对你有一定的参考价值。

题意:https://codeforc.es/contest/1207/problem/D

n个元素,每个元素有a、b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时good)。

思路:

真难则反+容斥,反向考虑,ans1=如果a序列非递减则有a中各个数字出现次数的阶乘的乘积个,ans2=b序列也是一样。

ans3=然后还要减去a序列和b序列都是good的方案数,就是元素相同的出现次数阶乘的乘积(注意,如果不存在双good就不算ans3)。

ANS就是:全排列 - ans1 - ans2 + ans3

  1 #define ios ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 #include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register long long a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 typedef long long ll;
 31 void swapp(int &a,int &b);
 32 double fabss(double a);
 33 int maxx(int a,int b);
 34 int minn(int a,int b);
 35 int Del_bit_1(int n);
 36 int lowbit(int n);
 37 int abss(int a);
 38 //const long long INF=(1LL<<60);
 39 const double E=2.718281828;
 40 const double PI=acos(-1.0);
 41 const int inf=(1<<30);
 42 const double ESP=1e-9;
 43 const int mod=(int)998244353;
 44 const int N=(int)1e6+10;
 45 
 46 ll a[N],b[N];
 47 pair<ll,ll>s[N];
 48 map<pair<ll,ll>,ll>mp3;
 49 map<ll,ll> mp1,mp2;
 50 
 51 ll v(ll x)
 52 
 53     ll sum=1;
 54     fo(i,1,x)
 55         sum*=i,sum%=mod;
 56     return sum;
 57 
 58 
 59 int main()
 60 
 61     int n;
 62     ll ans=1,temp1,temp2,temp3;
 63     sc("%d",&n);
 64     fo(i,1,n)ans*=i,ans%=mod,sc("%lld%lld",&a[i],&b[i]),mp1[a[i]]++,mp2[b[i]]++,s[i]=a[i],b[i],mp3[s[i]]++;
 65     temp1=temp2=temp3=1;
 66     for(auto i:mp1)
 67         temp1*=v(i.second),temp1%=mod;
 68     for(auto i:mp2)
 69         temp2*=v(i.second),temp2%=mod;
 70     for(auto i:mp3)
 71         temp3*=v(i.second),temp3%=mod;
 72     sort(s+1,s+1+n);
 73     for(int i=1;i<n;++i)
 74         if(s[i].second>s[i+1].second)
 75             temp3=0;
 76     pr("%lld\n",((ans-temp1-temp2+temp3)%mod+mod)%mod);
 77     return 0;
 78 
 79 
 80 /**************************************************************************************/
 81 
 82 int maxx(int a,int b)
 83 
 84     return a>b?a:b;
 85 
 86 
 87 void swapp(int &a,int &b)
 88 
 89     a^=b^=a^=b;
 90 
 91 
 92 int lowbit(int n)
 93 
 94     return n&(-n);
 95 
 96 
 97 int Del_bit_1(int n)
 98 
 99     return n&(n-1);
100 
101 
102 int abss(int a)
103 
104     return a>0?a:-a;
105 
106 
107 double fabss(double a)
108 
109     return a>0?a:-a;
110 
111 
112 int minn(int a,int b)
113 
114     return a<b?a:b;
115 

 

以上是关于双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 1671. Minimum Number of Removals to Make Mountain Array

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

200. Number of Islands

Leetcode-673 (Number of Longest Increasing Subsequence)最长递增子序列的个数

LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)

Codewars Solution:Convert number to reversed array of digits