……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 eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; 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); } string s; void Solve() { cin >> s; bool f = 0; for(int i = s.length() - 1 ; i >= 0 ; --i) { if(s[i] == 'F') f = 1; if(s[i] == 'C' && f) { puts("Yes");return; } } puts("No"); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
找到最多的一天個數是m,若是m > K - m答案就是2m - K - 1數組
#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 eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; 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,T,a[105]; void Solve() { read(K);read(T); int m = 0; for(int i = 1 ; i <= T ; ++i) { read(a[i]); m = max(a[i],m); } if(K - m >= m) {puts("0");return;} else {out(m - (K - m) - 1);enter;} } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
若是是最大值變化的地方,那麼必然是個固定值,咱們判斷若是兩個都固定了某值,這兩個值相不相等,若是隻有一個地方固定了,那麼看看滿不知足另外一邊的條件spa
若是都沒固定就是這個位置兩邊最大值較小的那個及如下均可以實現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 eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; 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 T[MAXN],A[MAXN]; int N; int mul(int a,int b) { return 1LL * a * b % MOD; } void Solve() { read(N); for(int i = 1 ; i <= N ; ++i) read(T[i]); for(int i = 1 ; i <= N ; ++i) read(A[i]); int ans = 1; for(int i = 1 ; i <= N ; ++i) { if(T[i] != T[i - 1] || A[i] != A[i + 1]) { int a = -1,b = -1; if(T[i] != T[i - 1]) a = T[i]; if(A[i] != A[i + 1]) b = A[i]; if(a != -1 && b != -1) { if(a != b) {puts("0");return;} } if(b > T[i]) {puts("0");return;} if(a > A[i]) {puts("0");return;} } else { ans = mul(ans,min(A[i],T[i])); } } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
若是隻有兩列,設\(dp[i][j]\)爲第一列下移了i第二列下移了jci
轉移是\(dp[i][j] = min(dp[i - 1][j] + cost[i - 1][j],dp[i][j - 1] + cost[i][j - 1])\)rem
\(cost[i][j]\)能夠用\(H^2\)求出,先暴力求出全部某一維爲0的大小,而後從\(cost[i - 1][j - 1]\)轉移過來,看看消掉的那一位是否相同get
而不少列的話,咱們能夠對相鄰兩列都作一遍,這樣是合法的,由於咱們先把第一列和第二列的最優方案肯定了,咱們第二列和第三列就能夠忽視掉第一列,把第三列的最優答案放在第二列的相對位置,以此類推string
#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 eps 1e-10 #define MAXN 500005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; 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 dp[305][305],ans,cost[305][305]; int H,W; char s[305][305]; void Solve() { read(H);read(W); for(int i = 1 ; i <= H ; ++i) scanf("%s",s[i] + 1); for(int i = 1 ; i < W ; ++i) { for(int j = 0 ; j <= H ; ++j) { for(int h = 0 ; h <= H ; ++h) { dp[j][h] = 1e18; } } dp[0][0] = 0; memset(cost,0,sizeof(cost)); for(int j = 0 ; j <= H ; ++j) { int p1 = H - j,p2 = H; while(p1 >= 1 && p2 >= 1) { if(s[p1][i] == s[p2][i + 1]) ++cost[j][0]; --p1;--p2; } } for(int j = 1 ; j <= H ; ++j) { int p1 = H,p2 = H - j; while(p1 >= 1 && p2 >= 1) { if(s[p1][i] == s[p2][i + 1]) ++cost[0][j]; --p1;--p2; } } for(int j = 1 ; j <= H ; ++j) { for(int h = 1 ; h <= H ; ++h) { cost[j][h] = cost[j - 1][h - 1] - (s[H - j + 1][i] == s[H - h + 1][i + 1]); } } for(int j = 0 ; j <= H * 2 ; ++j) { for(int h = 0 ; h <= min(j,H) ; ++h) { int a = h,b = j - h; if(a > H || b > H) continue; int c = cost[a][b]; if(a + 1 <= H) { dp[a + 1][b] = min(dp[a + 1][b],dp[a][b] + c); } if(b + 1 <= H) { dp[a][b + 1] = min(dp[a][b + 1],dp[a][b] + c); } } } ans += dp[H][H]; } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
若是對於一個位置\(s_{i}\)咱們這個排列能乘上的數就是it
\((s_i - 1 - \sum_{j = 1}^{i - 1}(s_j < s_{i}))(N - i)!\)io
忽略掉\((N - i)!\),咱們對每一個\(i\)統計括號裏面的總和是多少
咱們分開,統計對於位置i,有多少種排列,\(s_j\)小於\(s_i\)的個數和
而後加上全部排列的\(s_{i} - 1\)
前一項很好統計,設空的位置個數爲\(K\),那麼若是這一位有了一個數
答案就是\((p[i] - 1)K!\)
若是沒有數,就是全部未填的數 - 1的總和\(sum\)
答案是\(sum(K - 1)!\)
第二項分開考慮
若是這一位是空位,那麼j能夠是一個有數的地方,這個時候是空位大於j的方案數,能夠用一個前綴變量記一下
若是j沒有數,就是在K個數裏選2個,剩下任填的方案
若是這一位不是空位,\(j\)有數,那麼只要用樹狀數組維護比\(p_i\)小且在i之前的\(p_j\)有多少個
若是\(j\)沒數,就是在未填的數中選一個\(p_i\)小的數,而後填在\(j\)那裏,剩下\(K - 1\)個任填
#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 eps 1e-10 #define MAXN 500005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; 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; int fac[MAXN],invfac[MAXN],p[MAXN],s[MAXN],k; int tr[MAXN],ans[MAXN]; bool vis[MAXN]; 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 C(int n,int m) { if(n < m) return 0; return mul(fac[n],mul(invfac[m],invfac[n - m])); } int lowbit(int x) {return x & (-x);} void insert(int x,int c) { while(x <= N) { tr[x] = inc(tr[x],c); x += lowbit(x); } } int query(int x) { int r = 0; while(x > 0) { r = inc(r,tr[x]); x -= lowbit(x); } return r; } void update(int &x,int y) { x = inc(x,y); } void Solve() { read(N); fac[0] = 1; for(int i = 1 ; i <= N ; ++i) { fac[i] = mul(fac[i - 1],i); } invfac[N] = fpow(fac[N],MOD - 2); for(int i = N - 1 ; i >= 0 ; --i) { invfac[i] = mul(invfac[i + 1],i + 1); } for(int i = 1 ; i <= N ; ++i) { read(p[i]); if(p[i]) vis[p[i]] = 1; else ++k; } int sum = 0; for(int i = N ; i >= 1 ; --i) { s[i] += s[i + 1]; if(!vis[i]) { s[i]++; update(sum,i - 1); } } int pre = 0; int rem = 0; for(int i = 1 ; i <= N ; ++i) { if(!p[i]) { update(ans[i],MOD - pre); if(k >= 2) update(ans[i],MOD - mul(rem,mul(C(k,2),fac[k - 2]))); update(ans[i],mul(sum,fac[k - 1])); ++rem; } else { update(ans[i],mul(p[i] - 1,fac[k])); update(ans[i],MOD - mul(query(p[i] - 1),fac[k])); if(k) update(ans[i],MOD - mul(rem,mul(s[1] - s[p[i]],fac[k - 1]))); if(k) update(pre,mul(s[p[i] + 1],fac[k - 1])); insert(p[i],1); } } int res = fac[k]; for(int i = 1 ; i <= N ; ++i) { update(res,mul(fac[N - i],ans[i])); } out(res);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }