CF2182C Production of Snowmen

思路

首先我们发现 $a$ 与 $b$,$b$ 与 $c$ 的相对顺序之间并不互相影响。
换句话说这道题就等于求 $a$ 与 $b$,$b$ 与 $c$ 之间所有合法匹配方案的乘积。
题目数据范围较小,直接枚举 $a,b$ 和 $b,c$ 的合法匹配即可。
但是由于 $i,j$ 的匹配与 $i+1,j+1$ 的匹配是等价的,所以总方案数还要再乘上 $n$。

代码

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
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
#define fir first
#define sec second
#define int long long
#define pii pair<int,int>
#define fep(i,s,e) for(int i=s;i<e;i++)
#define pef(i,s,e) for(int i=s;i>e;i--)
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define per(i,s,e) for(int i=s;i>=e;i--)
namespace FastIO{
template<typename T>inline void read(T &x){
x=0;int f=1;char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);x*=f;
}
template<typename T,typename...Args>
inline void read(T &x,Args&...args){
read(x);
read(args...);
}
template<typename T>void print(T x){
if(x<0)x=-x,putchar('-');
if(x>9)print(x/10);
putchar((x%10)^48);
}
}
using namespace std;
using namespace FastIO;
const int N=5e3+5;
int n,T,a[N],b[N],c[N];
signed main(){
read(T);
while(T--){
read(n);
rep(i,1,n)read(a[i]);
rep(i,1,n)read(b[i]);
rep(i,1,n)read(c[i]);
b[0]=b[n],c[0]=c[n];
int ab=0,bc=0;
rep(i,1,n){//b开头
int flag=1;
rep(j,1,n){
if(b[(i+j)%n]<=a[j]){
flag=0;
break;
}
}
ab+=flag;
}
rep(i,1,n){//c开头
int flag=1;
rep(j,1,n){
if(c[(i+j)%n]<=b[j]){
flag=0;
break;
}
}
bc+=flag;
}
print(ab*bc*n);puts("");
}
}