【闲话】9 月 7 闲话
警钟敲烂,摆了十几天后基础题都写不出来了,距今 0 天。
复健,写几道基础题。
CF1726E - Almost Perfect
求有多少长度为
n 的排列p ,满足\forall i\in [1, n],|p_i - p^{-1}_i| = 1 。
我们将
#define N 300005
int n, f[N], fc[N], iv[N];
inline int C(int x,int y){return fc[x] * (LL)iv[y] % P * iv[x - y] % P;}
void solve(){
read(n);
fc[0] = 1; rp(i, n)fc[i] = fc[i - 1] * (LL)i % P;
iv[n] = Pow(fc[n], P - 2); pr(i, n)iv[i - 1] = iv[i] * (LL)i % P;
f[0] = f[1] = 1;
rep(i, 2, n)f[i] = (f[i - 1] + f[i - 2] * (i - 1LL)) % P;
int ans = 0;
for(int i = 0; i * 4 <= n; i ++)
ans = (ans + C(n - i - i, i + i) * (LL)fc[i + i] % P * iv[i] % P * f[n - 4 * i]) % P;
printf("%d\n", ans);
}
int main() {int T; read(T); while(T--)solve();}
ARC147E - Examination
给定
N 个二元组(A_i,B_i) ,问最少选出多少个二元组,将他们的A 重排,使得对于所有的i 满足A_i\ge B_i 。
显然有解的充要条件是是将
我们先将所有
#define N 300005
int n, m, a[N], b[N], c[N], d[N], u[N];
vector<int>p[N];
priority_queue<int>q;
int main() {
read(n);
rp(i, n)read(a[i], b[i]), u[i] = b[i];
sort(u + 1, u + n + 1);
m = unique(u + 1, u + n + 1) - u - 1;
u[++m] = inf; int ans = 0;
rp(i, n){
a[i] = upper_bound(u + 1, u + m + 1, a[i]) - u - 1,
b[i] = lower_bound(u + 1, u + m + 1, b[i]) - u;
if(!a[i]){puts("-1"); return 0;}
if(a[i] < b[i])c[a[i]]--, c[b[i]]++, ans ++;
else if(a[i] > b[i])p[b[i]].pb(a[i]);
d[a[i]]--, d[b[i]]++;
}
rp(i, m){
d[i] += d[i - 1];
if(d[i] < 0){puts("-1"); return 0;}
}
int w = 0;
rp(i, m){
w += c[i];
go(x, p[i])q.push(x);
while(w < 0 && !q.empty()){
c[q.top()]--, w ++, q.pop(), ans++;
}
if(w < 0){puts("-1"); return 0;}
}
printf("%d\n", n - ans);
return 0;
}
ARC146D
题面 https://atcoder.jp/contests/arc146/tasks/arc146_d
条件非常像 2-SAT,我们将条件简化一下,
#define N 200005
int n, m, k, f[N], p[N];
struct node{
int w, u, v;
bool operator<(const node o)const{return w < o.w;}
};
vector<node>c[N];
int main() {
read(n, m, k);
rp(i, k){
int p, q, x, y;
read(p, x, q, y);
c[p].pb(node{x, q, y});
c[q].pb(node{y, p, x});
++ x, ++ y;
c[p].pb(node{x, q, y});
c[q].pb(node{y, p, x});
}
queue<int>q;
rp(i, n)f[i] = 1, q.push(i), sort(c[i].begin(), c[i].end());
while(!q.empty()){
int x = q.front(); q.pop();
while(p[x] < si(c[x]) && f[x] >= c[x][p[x]].w){
node cur = c[x][p[x]++];
cmx(f[cur.u], cur.v), q.push(cur.u);
}
}
LL ans = 0;
rp(i, n)if(f[i] > m){puts("-1"); return 0;} else ans += f[i];
cout << ans << endl;
return 0;
}
ARC146E - Simple Speed
给定长度为
N 的数组A ,问有多少个长度为\sum A 的数组B 满足B 中i 的出现次数恰好为A_i ,且任意相邻两位的差的绝对值为1 ,N,A_i\le 2\times 10^5 。
转化到图上,显然是求有多少种构成一条链的方案。从小到大考虑每一个数,大概写出一个 DP 方程
所以我们在状态里加一维,
#define N 200005
int n, a[N], fc[N], iv[N];
map<int,int>f[2][3];
inline int C(int x,int y){
if(x < 0 || y < 0 || x < y)return 0;
return fc[x] * (LL)iv[y] % P * iv[x - y] % P;
}
int main() {
read(n);
fc[0] = 1;
rp(i, N - 5)fc[i] = fc[i - 1] * (LL)i % P;
iv[N - 5] = Pow(fc[N - 5], P - 2);
pr(i, N - 5)iv[i - 1] = iv[i] * (LL)i % P;
rp(i, n)read(a[i]);
f[1][0][a[1]] = 1;
rep(i, 2, n){
int op = i & 1, pv = op ^ 1;
rep(j, 0, 2){
for(auto [k, x] : f[pv][j])rep(w, j, 2){
f[op][w][a[i] - k + w] = (f[op][w][a[i] - k + w] + x * (j == 0 && w == 1 ? 2LL : 1LL) * C(a[i] - 1, k - w)) % P;
}
f[pv][j].clear();
}
}
int ans = 0;
rep(i, 0, 2)ad(ans, f[n & 1][i][1]);
cout << ans << endl;
return 0;
}