1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include <bits/stdc++.h> using namespace std;#define int long long int read () { int x = 0 , f = 1 ; char ch = getchar (); while (ch < '0' || ch > '9' ) { if (ch == '-' ) f = -1 ; ch = getchar (); } while (ch >= '0' && ch <= '9' ) { x = x * 10 + ch - '0' ; ch = getchar (); } return x * f; } signed main () { vector<int > a (26 ) ; string s; cin >> s; for (int i = 0 ; i < 4 ; i++) a[s[i] - 'A' ]++; for (int i = 0 ; i < 26 ; i++) { if (a[i] && a[i] != 2 ) { cout << "No\n" ; return 0 ; } } cout << "Yes\n" ; return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <bits/stdc++.h> using namespace std;#define int long long int read () { int x = 0 , f = 1 ; char ch = getchar (); while (ch < '0' || ch > '9' ) { if (ch == '-' ) f = -1 ; ch = getchar (); } while (ch >= '0' && ch <= '9' ) { x = x * 10 + ch - '0' ; ch = getchar (); } return x * f; } signed main () { int n = read (); vector<int > a (n) ; for (int i = 0 ; i < n; i++) a[i] = read (); int res = 0 ; for (int i = 0 ; i < n - 2 ; i++) { if (a[i] > a[i + 1 ] && a[i + 1 ] > a[i + 2 ]) res++; if (a[i] < a[i + 1 ] && a[i + 1 ] < a[i + 2 ]) res++; } cout << res; return 0 ; }
找到第n / 2 个和第n / 2 + 1个数求差
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <bits/stdc++.h> using namespace std;#define int long long int read () { int x = 0 , f = 1 ; char ch = getchar (); while (ch < '0' || ch > '9' ) { if (ch == '-' ) f = -1 ; ch = getchar (); } while (ch >= '0' && ch <= '9' ) { x = x * 10 + ch - '0' ; ch = getchar (); } return x * f; } signed main () { int n = read (); vector<int > a (n) ; for (int i = 0 ; i < n; i++) a[i] = read (); sort (a.begin (), a.end ()); cout << a[n / 2 ] - a[n / 2 - 1 ]; return 0 ; }
排列组合问题
类似与隔板法, 如果要移动$i$次,则要有$i$组Blue Balls插入在Red Balls之间(或者两边),选择插入的位置共有$C_{n-k +1}^{i}$
接下来就是如何将Blue Balls分成$i$组,总共又$k-1$个可分隔的地方,从中选出$i-1$个就分成了$i$组,即$C_{k-1}^{i-1}$
所以移动$i$的总方案数就是$ C_{k-1}^{i-1} * C_{n-k +1}^{i} $
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <bits/stdc++.h> using namespace std;#define int long long int read () { int x = 0 , f = 1 ; char ch = getchar (); while (ch < '0' || ch > '9' ) { if (ch == '-' ) f = -1 ; ch = getchar (); } while (ch >= '0' && ch <= '9' ) { x = x * 10 + ch - '0' ; ch = getchar (); } return x * f; } const int mod = 1e9 + 7 ;int qmi (int a, int b) { int res = 1 ; while (b) { if (b & 1 ) res = (res * a) % mod; b >>= 1 ; a = (a * a) % mod; } return res % mod; } signed main () { int n = read (), k = read (); vector<int > f (n + 1 ) , inv (n + 1 ) ; f[0 ] = 1 ; for (int i = 1 ; i <= n; i++) f[i] = f[i - 1 ] * i % mod; inv[0 ] = 1 ; inv[n] = qmi (f[n], mod - 2 ); for (int i = n - 1 ; i >= 1 ; i--) inv[i] = inv[i + 1 ] * (i + 1 ) % mod; auto C = [&](int a, int b) -> int { if (b < a) return 0 ; return f[b] * inv[b - a] % mod * inv[a] % mod; }; for (int i = 1 ; i <= k; i++) cout << (C (i - 1 , k - 1 ) * C (i, n - k + 1 )) % mod << endl; return 0 ; }
分层图最短路,主要是理解题意,然后建模
因为要是三的倍数所以分成三层,建图对于一条边就 1-> 2 -> 3-> 1 这样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <bits/stdc++.h> using namespace std;#define int long long int read () { int x = 0 , f = 1 ; char ch = getchar (); while (ch < '0' || ch > '9' ) { if (ch == '-' ) f = -1 ; ch = getchar (); } while (ch >= '0' && ch <= '9' ) { x = x * 10 + ch - '0' ; ch = getchar (); } return x * f; } typedef pair<int , int > PII;signed main () { int n = read (), m = read (); vector<vector<int >> a (3 * n + 1 ); for (int i = 0 ; i < m; i++) { int u = read (), v = read (); a[u].emplace_back (v + n); a[u + n].emplace_back (v + 2 * n); a[u + 2 * n].emplace_back (v); } int s = read (), t = read (); vector<int > dist (3 * n + 1 , 0x3f3f3f3f ) ; vector<int > st (3 * n + 1 ) ; auto bfs = [&]() -> void { queue<int > q; q.push (s);st[s] = 1 ;dist[s] = 0 ; while (q.size ()) { auto ver = q.front (); q.pop (); for (auto x : a[ver]) { if (st[x]) continue ; dist[x] = dist[ver] + 1 ; st[x] = 1 ; q.push (x); } } }; bfs (); if (dist[t] == 0x3f3f3f3f ) cout << -1 ; else cout << dist[t] / 3 ; return 0 ; }
有点晚了,看了下题感觉不是我能写的,有缘再见,看题解是数据结构优化dp。。。