有毒還吃,有毒吧c++
#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(); }
咱們枚舉一個斷點,也就是前\(i\)個是順時針走的,後\(N - i\)個是逆時針走的
發現至關於左邊選後t個點,右邊選後t + 1個點(t <= i && t + 1 <= N - i)
(或者左邊後t + 1個,右t個,和這個狀況相似)
而後這些點到原點的距離乘二,再減去右邊最後一個點到原點的距離
是這種狀況能到達的最大值cookie
#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(); }
我構造水平低啊。。。
這題是若是\(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\)便可。。。less
#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(); }
這題好神仙啊QAQ
對於這種一臉計數的題,sd選手只會上去想計數,然而這題轉成機率作,很是妙
\(dp[t][i][j]\)表示第\(t\)次操做第\(i\)個位置大於第\(j\)個位置的方案數
每次轉移影響4n個位置
而後直接統計指望,最後乘上\(2^Q\)就是答案spa
#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(); }
題解的圖畫的挺好的,就不照搬了
就是在\(01\)和\(10\)之間畫一條線,發現就是這些線不停的移動,咱們枚舉某條線和哪條線匹配,讓後往兩邊擴展,最多匹配就N種code
#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(); }
看出來相似一個卡特蘭數的先後匹配,發現正着作就是不行,結果反着推就對了。。。看來正推複雜度堪憂就得試試反着
顯然能夠把兩個位置都填上數的位置所有刪掉,不影響答案
如今默認任意\(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]\)get
#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(); }