【AtCoder】ARC100 题解
C - Linear Approximation
找出\(A_i - i\)的中位数作为\(b\)即可
题解
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define enter putchar('\n')
#define space putchar(' ')
#define fi first
#define se second
#define mp make_pair
#define MAXN 200005
//#define ivorysi
#define pii pair<int,int>
using namespace std;
typedef long long int64;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {putchar('-');x = -x;}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int64 A[MAXN];
int N;
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(A[i]);
A[i] -= i;
}
sort(A + 1,A + N + 1);
int64 t = A[N / 2 + 1],ans = 0;
for(int i = 1 ; i <= N ; ++i) {
ans += abs(A[i] - t);
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
D - Equal Cut
枚举2 和 3中间的位置,两边都必须切成绝对值相差最小才能使总体绝对值相差最小,切的位置不断右移,直接两个指针扫一遍即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
//#define ivorysi
#define MAXN 200005
typedef long long int64;
using namespace std;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
int64 a[MAXN],sum[MAXN];
int64 get_abs(int l1,int r1,int l2,int r2) {
return abs((sum[r1] - sum[l1 - 1]) - (sum[r2] - sum[l2 - 1]));
}
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);sum[i] = sum[i - 1] + a[i];
}
int l = 1,p = 2,r = p + 1;
int64 ans = sum[N];
while(p <= N - 2) {
while(l + 1 < p && get_abs(1,l,l + 1,p) > get_abs(1,l + 1,l + 2,p)) ++l;
r = max(r,p + 1);
while(r + 1 < N && get_abs(p + 1,r,r + 1,N) > get_abs(p + 1,r + 1,r + 2,N)) ++r;
int64 m[] = {sum[l],sum[p] - sum[l],sum[r] - sum[p],sum[N] - sum[r]};
int64 tmp = 0;
for(int i = 0 ; i <= 3 ; ++i) {
for(int j = i + 1 ; j <= 3 ; ++j) {
tmp = max(tmp,abs(m[j] - m[i]));
}
}
ans = min(ans,tmp);
++p;
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
E - Or Plus Max
我们转化一下问题,处理出每个i or j正好是k的子集的答案,再处理成前缀max即可
这样的话类似FMT的更新每个子集里的最大和次大即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <ctime>
#include <map>
#include <set>
#define fi first
#define se second
#define pii pair<int,int>
//#define ivorysi
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define MAXN 100005
using namespace std;
typedef long long int64;
typedef double db;
typedef unsigned int u32;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9' ) {
res = res * 10 - '0' + c;
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
pii f[(1 << 18) + 5];
int ans[(1 << 18) + 5];
pii Merge(pii a,pii b) {
if(a.fi < b.fi) swap(a,b);
return mp(a.fi,max(a.se,b.fi));
}
void Solve() {
read(N);
for(int i = 0 ; i < (1 << N) ; ++i) {
read(f[i].fi);f[i].se = 0;
}
for(int i = 1 ; i < (1 << N) ; i <<= 1) {
for(int j = 0 ; j < (1 << N) ; ++j) {
if(j & i) {
f[j] = Merge(f[j],f[j ^ i]);
}
}
}
for(int i = 1 ; i < (1 << N) ; ++i) {
ans[i] = f[i].fi + f[i].se;
ans[i] = max(ans[i - 1],ans[i]);
out(ans[i]);enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
F - Colorful Sequences
我不会数数啊QAQ
先求出所有的序列里M这一段出现的次数的总和
答案是\((N - M + 1)K^{N - M}\)
然后求M这一段出现在不多彩的序列里次数的总和
如果M已经是多彩的了,那么答案是0
如果M不是多彩的且没有重复的数字
那么求所有N长的序列里M长含有不同数字的连续子段有多少个,答案除上\(\frac{K!}{(K - M)!}\)
那么记录dp[i][j]作为第i个,然后前j个数都是互不相同的数,j+1开始出现重复
更新的时候从dp[i - 1][h]更新
\(\left\{\begin{matrix}
1 & h \geq j \\
K - h & h = j - 1\\
0 & h < j - 1
\end{matrix}\right.\)
然后用前缀和处理可以做到\(O(NK)\)
用cnt[i][j]表示i长的序列里,倒数j个数都是互不相同的数,M长含有不同数字的连续子段有多少个
在每遇到一个dp[i][j]的j>=M就累加上
剩下转移方式类似
如果M是多彩的且有重复数字
那么就记录F为前缀最多到哪是互不相同的,B为后缀最多到哪是互不相同的
枚举M所在位置用类似的dp转移
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
//#define ivorysi
#define fi first
#define se second
#define MAXN 25005
#define enter putchar('\n')
#define space putchar(' ')
typedef long long ll;
using namespace std;
template <class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
c = getchar();
if(c == '-') f = -1;
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template <class T>
void out(T x) {
if(x < 0) {x = -x;}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,K,M;
int A[MAXN],fac[MAXN],invfac[MAXN],inv[MAXN];
int F,B,L,vis[405];
int dp[MAXN][405],cnt[MAXN][405],sum[405],sum_cnt[405],f[MAXN],b[MAXN];
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Init() {
read(N);read(K);read(M);
for(int i = 1 ; i <= M ; ++i) read(A[i]);
inv[1] = 1;
for(int i = 2 ; i <= K ; ++i) inv[i] = mul(inv[MOD % i],MOD - MOD / i);
fac[0] = invfac[0] = 1;
for(int i = 1 ; i <= K ; ++i) {
fac[i] = mul(fac[i - 1],i);
invfac[i] = mul(invfac[i - 1],inv[i]);
}
F = 0;B = 0;
memset(vis,0,sizeof(vis));
for(int i = 1 ; i <= M ; ++i) {
if(!vis[A[i]]) {
++F;
vis[A[i]] = 1;
}
else break;
}
memset(vis,0,sizeof(vis));
for(int i = M ; i >= 1 ; --i) {
if(!vis[A[i]]) {
++B;
vis[A[i]] = 1;
}
else break;
}
memset(vis,0,sizeof(vis));
int l = 0;
for(int i = 1 ; i <= M ; ++i) {
l = max(l,vis[A[i]]);
L = max(L,i - l);
vis[A[i]] = i;
}
}
void Process(int st,int *a) {
memset(dp,0,sizeof(dp));
dp[0][st] = 1;
memset(sum,0,sizeof(sum));
for(int i = st ; i <= K ; ++i) sum[i] = 1;
a[0] = 1;
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j < K ; ++j) {
dp[i][j] = inc(dp[i][j],mul(dp[i - 1][j - 1],(K - j + 1)));
dp[i][j] = inc(dp[i][j],inc(sum[K],MOD - sum[j - 1]));
}
for(int j = 1 ; j <= K ; ++j) {
sum[j] = inc(sum[j - 1],dp[i][j]);
}
a[i] = sum[K - 1];
}
}
void Solve() {
if(L == K) {
out(mul(N - M + 1,fpow(K,N - M)));enter;
}
else if(F == M) {
dp[0][0] = 1;
int ans = mul(N - M + 1,fpow(K,N - M)),tmp = 0;
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j < K ; ++j) {
dp[i][j] = inc(dp[i][j],mul(dp[i - 1][j - 1],(K - j + 1)));
dp[i][j] = inc(dp[i][j],inc(sum[K],MOD - sum[j - 1]));
cnt[i][j] = inc(cnt[i][j],mul(cnt[i - 1][j - 1],(K - j + 1)));
cnt[i][j] = inc(cnt[i][j],inc(sum_cnt[K],MOD - sum_cnt[j - 1]));
if(j >= M) cnt[i][j] = inc(cnt[i][j],dp[i][j]);
}
for(int j = 1 ; j <= K ; ++j) {
sum[j] = inc(sum[j - 1],dp[i][j]);
sum_cnt[j] = inc(sum_cnt[j - 1],cnt[i][j]);
}
}
for(int j = 0 ; j < K ; ++j) {
tmp = inc(tmp,cnt[N][j]);
}
tmp = mul(tmp,fpow(mul(fac[K],invfac[K - M]),MOD - 2));
ans = inc(ans,MOD - tmp);
out(ans);enter;
}
else {
Process(F,f);Process(B,b);
int ans = mul(N - M + 1,fpow(K,N - M));
for(int i = 1 ; i <= N - M + 1 ; ++i) {
int j = i + M - 1;
ans = inc(ans,MOD - mul(f[i - 1],b[N - j]));
}
out(ans);enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
}