【AtCoder】AGC030
A - Poisonous Cookies
有毒还吃,有毒吧
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 100005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
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 + 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);
}
int64 A,B,C;
void Solve() {
read(A);read(B);read(C);
out(B + min(C,A + B + 1));enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
B - Tree Burning
我们枚举一个断点,也就是前\(i\)个是顺时针走的,后\(N - i\)个是逆时针走的
发现相当于左边选后t个点,右边选后t + 1个点(t <= i && t + 1 <= N - i)
(或者左边后t + 1个,右t个,和这个情况类似)
然后这些点到原点的距离乘二,再减去右边最后一个点到原点的距离
是这种情况能到达的最大值
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
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 + 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);
}
int64 L;int N;
int64 a[MAXN],sum[2][MAXN];
void Solve() {
read(L);read(N);
for(int i = 1 ; i <= N ; ++i) read(a[i]);
for(int i = 1 ; i <= N ; ++i) {
sum[0][i] = sum[0][i - 1] + a[i];
}
for(int i = N ; i >= 1 ; --i) {
sum[1][i] = sum[1][i + 1] + L - a[i];
}
int64 ans = max(L - a[1],a[N]);
for(int i = 1 ; i <= N - 1; ++i) {
int t = min(i,N - i);
int64 res = 2 * (sum[0][i] - sum[0][i - t] + sum[1][i + 1] - sum[1][i + 1 + t]);
int64 c = res - a[i];
if(i > t) {c += 2 * a[i - t];}
ans = max(ans,c);
c = res - (L - a[i + 1]);
if(N - i > t) c += 2 * (L - a[i + 1 + t]);
ans = max(ans,c);
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
C - Coloring Torus
我构造水平低啊。。。
这题是如果\(K\)是4的倍数,很好做
就是这么填,如果是4 * 4
而且标号认为是\(0-K - 1\)
0 1 2 3
5 6 7 4
2 3 0 1
7 4 5 6
如果要减少一个数,我们把\(i + n\)都替换成\(i\)即可。。。
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
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 + 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 K;
int a[505][505],N;
void Solve() {
read(K);
if(K == 1) {
puts("1");puts("1");return;
}
for(int i = 2 ; i <= 500 ; i += 2) {
if(2 * i >= K) {N = i;break;}
}
for(int i = 0 ; i < N ; ++i) {
for(int j = 0 ; j < N ; ++j) {
if(i & 1) a[i][j] = ((i + j) % N) + N;
else a[i][j] = (i + j) % N;
}
}
int p = N - 1;
while(K < 2 * N) {
for(int i = 0 ; i < N ; ++i) {
for(int j = 0 ; j < N ; ++j) {
if(a[i][j] == p + N) a[i][j] = p;
}
}
--p;
++K;
}
out(N);enter;
for(int i = 0 ; i < N ; ++i) {
for(int j = 0 ; j < N ; ++j) {
out(a[i][j] + 1);space;
}
enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
D - Inversion Sum
这题好神仙啊QAQ
对于这种一脸计数的题,sd选手只会上去想计数,然而这题转成概率做,非常妙
\(dp[t][i][j]\)表示第\(t\)次操作第\(i\)个位置大于第\(j\)个位置的方案数
每次转移影响4n个位置
然后直接统计期望,最后乘上\(2^Q\)就是答案
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
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 + 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);
}
const int MOD = 1000000007;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
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;
}
int N,Q,Inv2;
int dp[3005][3005],f[3005][3005];
int A[3005];
void Solve() {
Inv2 = (MOD + 1) / 2;
read(N);read(Q);
for(int i = 1 ; i <= N ; ++i) read(A[i]);
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= N ; ++j) {
if(A[i] > A[j]) dp[i][j] = 1;
}
}
int x,y;
for(int i = 1 ; i <= Q ; ++i) {
read(x);read(y);
for(int j = 1 ; j <= N ; ++j) {
f[x][j] = dp[x][j];
f[j][x] = dp[j][x];
f[j][y] = dp[j][y];
f[y][j] = dp[y][j];
}
for(int j = 1 ; j <= N ; ++j) {
if(j != x && j != y)
dp[x][j] = mul(Inv2,inc(f[x][j],f[y][j]));
dp[j][x] = mul(Inv2,inc(f[j][x],f[j][y]));
dp[y][j] = mul(Inv2,inc(f[y][j],f[x][j]));
dp[j][y] = mul(Inv2,inc(f[j][y],f[j][x]));
}
dp[x][y] = mul(Inv2,inc(f[x][y],f[y][x]));
dp[y][x] = mul(Inv2,inc(f[y][x],f[x][y]));
}
int ans = 0;
for(int i = 1 ; i <= N ; ++i) {
for(int j = i + 1 ; j <= N ; ++j) {
ans = inc(ans,dp[i][j]);
}
}
ans = mul(ans,fpow(2,Q));
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
E - Less than 3
题解的图画的挺好的,就不照搬了
就是在\(01\)和\(10\)之间画一条线,发现就是这些线不停的移动,我们枚举某条线和哪条线匹配,让后往两边扩展,最多匹配就N种
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
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 + 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);
}
const int MOD = 1000000007;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
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;
}
int N,st[2],ed[2];
char s[5005],t[5005];
vector<int> a,b;
void Init() {
read(N);
scanf("%s%s",s + 1,t + 1);
for(int i = 1 ; i <= 10010 ; ++i) {
a.pb(0);b.pb(0);
}
if(s[1] == '0') a.pb(0);
if(t[1] == '0') b.pb(0);
st[0] = a.size() - 1;st[1] = b.size() - 1;
for(int i = 1 ; i < N ; ++i) {
if(s[i] != s[i + 1]) a.pb(i);
if(t[i] != t[i + 1]) b.pb(i);
}
ed[1] = b.size();
for(int i = 1 ; i <= 10010 ; ++i) {
a.pb(N);b.pb(N);
}
}
void Solve() {
int p = st[0] - 2,q = ed[1] + 2;
if((p & 1) != (q & 1)) ++q;
int ans = N * N;
while(q >= st[1] - N - 2) {
int t0 = p + 1,t1 = q + 1;
int res = abs(a[p] - b[q]);
while(a[t0] != N || b[t1] != N) {
res += abs(a[t0] - b[t1]);
++t0;++t1;
}
t0 = p - 1,t1 = q - 1;
while(a[t0] != 0 || b[t1] != 0) {
res += abs(a[t0] - b[t1]);
--t0;--t1;
}
ans = min(ans,res);
q -= 2;
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
}
F - Permutation and Minimum
看出来类似一个卡特兰数的前后匹配,发现正着做就是不行,结果反着推就对了。。。看来正推复杂度堪忧就得试试反着
显然可以把两个位置都填上数的位置全部删掉,不影响答案
现在默认任意\(2i,2i+1\)两个位置没有填上数
设一对位置都是空的个数是\(cnt\),我们计算数互不相同的b序列有多个,答案再乘上\(cnt!\)
然后\(f[n][j][k]\)表示从2N到n,有j个没有在给出的A序列里的数出现过的数被钦定成了较大的一方,有k个在A序列里出现过的数被钦定成了较大的一方
转移的话,如果\(n\)在A中出现过就是
\(f[n][j][k + 1] \leftarrow f[n + 1][j][k]\)
\(f[n][j - 1][k] \leftarrow f[n + 1][j][k]\)
如果\(n\)没出现过就是
\(f[n][j][k - 1] \leftarrow f[n + 1][j][k] * k\)
\(f[n][j + 1][k] \leftarrow f[n + 1][j][k]\)
\(f[n][j - 1][k] \leftarrow f[n + 1][j][k]\)
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
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 + 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);
}
const int MOD = 1000000007;
int N,dp[2][605][605],C[606][606],fac[606];
int A[605],cnt;
bool vis[605],used[605];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
void Init() {
read(N);
for(int i = 1 ; i <= 2 * N ; ++i) read(A[i]);
cnt = 0;
for(int i = 1 ; i <= N ; ++i) {
if(A[2 * i] == -1 && A[2 * i - 1] == -1) ++cnt;
if(A[2 * i] != -1 && A[2 * i - 1] != -1) {
vis[A[2 * i]] = vis[A[2 * i - 1]] = 1;
}
else if(A[2 * i] != -1) used[A[2 * i]] = 1;
else if(A[2 * i - 1] != -1) used[A[2 * i - 1]] = 1;
}
C[0][0] = 1;
for(int i = 1 ; i <= 2 * N ; ++i) {
C[i][0] = 1;
for(int j = 1 ; j <= i ; ++j) {
C[i][j] = inc(C[i - 1][j - 1],C[i - 1][j]);
}
}
fac[0] = 1;
for(int i = 1 ; i <= 2 * N ; ++i) fac[i] = mul(fac[i - 1],i);
}
void Solve() {
int cur = 0;
dp[0][0][0] = 1;
for(int i = 2 * N ; i >= 1 ; --i) {
if(vis[i]) continue;
memset(dp[cur ^ 1],0,sizeof(dp[cur ^ 1]));
int t = (2 * N) - i;
if(used[i]) {
for(int j = 0 ; j <= t ; ++j) {
for(int k = 0 ; k <= t - j ; ++k) {
if(dp[cur][j][k]) {
update(dp[cur ^ 1][j][k + 1],dp[cur][j][k]);
if(j) update(dp[cur ^ 1][j - 1][k],dp[cur][j][k]);
}
}
}
}
else {
for(int j = 0 ; j <= t ; ++j) {
for(int k = 0 ; k <= t - j ; ++k) {
if(dp[cur][j][k]) {
update(dp[cur ^ 1][j + 1][k],dp[cur][j][k]);
if(k) update(dp[cur ^ 1][j][k - 1],mul(k,dp[cur][j][k]));
if(j) update(dp[cur ^ 1][j - 1][k],dp[cur][j][k]);
}
}
}
}
cur ^= 1;
}
int ans = dp[cur][0][0];
ans = mul(ans,fac[cnt]);
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
}