选择结构
AC代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #include<bits/stdc++.h> // #include <iostream> // #include <cstdio> // #include <queue> // #include <deque> // #include <stack> // #include <string> // #include <cstring> // #include <numeric> // #include <functional> // #include <cstdlib> // #include <vector> // #include <set> // #include <map> // #include <algorithm> // #include <cmath> // #include <iomanip> using i64 = long long; using namespace std; #define lowbit(x) ((x) & -(x)) #define endl '\n' #define IOS1 ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define IOS2 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; typedef long long ll; // typedef long long i64; template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T power(T a, int b) { T res = 1; for (; b; b >>= 1, a = a * a) { if (b & 1) { res = res * a; } } return res; } template <typename T> T Myabs(T a) { return a >= 0 ? a : -a; } template <typename T> inline void read(T& x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0', ch = getchar(); } x *= f; } const int INF = 0x3f3f3f3f; // const int mod = 1000000007; const int mod = 998244353; const double PI = acos(-1.0); const double eps = 1e-6; inline int sgn(double x) { return x < -eps ? -1 : x > eps; } /* Tips: 1.int? long long? 2.don't submit wrong answer 3.figure out logic first, then start writing please 4.know about the range 5.check if you have to input t or not 6.modulo of negative numbers is not a%b, it is a%b + abs(b) */ void solve() { int a, b, c, x; cin >> a >> b >> c >> x; double ans = 1; if (x <= a) { cout << fixed << setprecision(12) << ans << endl; } else if (x > b) { ans = 0; cout << fixed << setprecision(12) << ans << endl; } else { ans = 1.0 * c / (b - a); cout << fixed << setprecision(12) << ans << endl; } return; } signed main() { IOS1; // IOS2; #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif int __t = 1; // cin >> __t; for (int _t = 1; _t <= __t; _t++) { solve(); } return 0; } /* */
用输入的字符串按从小到大输出即可
AC代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #include<bits/stdc++.h> // #include <iostream> // #include <cstdio> // #include <queue> // #include <deque> // #include <stack> // #include <string> // #include <cstring> // #include <numeric> // #include <functional> // #include <cstdlib> // #include <vector> // #include <set> // #include <map> // #include <algorithm> // #include <cmath> // #include <iomanip> using i64 = long long; using namespace std; #define lowbit(x) ((x) & -(x)) #define endl '\n' #define IOS1 ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define IOS2 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; typedef long long ll; // typedef long long i64; template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T power(T a, int b) { T res = 1; for (; b; b >>= 1, a = a * a) { if (b & 1) { res = res * a; } } return res; } template <typename T> T Myabs(T a) { return a >= 0 ? a : -a; } template <typename T> inline void read(T& x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0', ch = getchar(); } x *= f; } const int INF = 0x3f3f3f3f; // const int mod = 1000000007; const int mod = 998244353; const double PI = acos(-1.0); const double eps = 1e-6; inline int sgn(double x) { return x < -eps ? -1 : x > eps; } /* Tips: 1.int? long long? 2.don't submit wrong answer 3.figure out logic first, then start writing please 4.know about the range 5.check if you have to input t or not 6.modulo of negative numbers is not a%b, it is a%b + abs(b) */ void solve() { string s; cin >> s; int len = s.size(); vector<int> a(26); string ans = ""; for (int i = 0; i < len; i++) { a[s[i] - 'a']++; } for (int i = 0; i < 26; i++) { for (int j = 0; j < a[i]; j++) { ans += 'a' + i; } } cout << ans << endl; return; } signed main() { IOS1; // IOS2; #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif int __t = 1; // cin >> __t; for (int _t = 1; _t <= __t; _t++) { solve(); } return 0; } /* */
线性DP计算每种可能
AC代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #include<bits/stdc++.h> // #include <iostream> // #include <cstdio> // #include <queue> // #include <deque> // #include <stack> // #include <string> // #include <cstring> // #include <numeric> // #include <functional> // #include <cstdlib> // #include <vector> // #include <set> // #include <map> // #include <algorithm> // #include <cmath> // #include <iomanip> using i64 = long long; using namespace std; #define lowbit(x) ((x) & -(x)) #define endl '\n' #define IOS1 ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define IOS2 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; typedef long long ll; // typedef long long i64; template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T power(T a, int b) { T res = 1; for (; b; b >>= 1, a = a * a) { if (b & 1) { res = res * a; } } return res; } template <typename T> T Myabs(T a) { return a >= 0 ? a : -a; } template <typename T> inline void read(T& x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0', ch = getchar(); } x *= f; } const int INF = 0x3f3f3f3f; // const int mod = 1000000007; const int mod = 998244353; const double PI = acos(-1.0); const double eps = 1e-6; inline int sgn(double x) { return x < -eps ? -1 : x > eps; } /* Tips: 1.int? long long? 2.don't submit wrong answer 3.figure out logic first, then start writing please 4.know about the range 5.check if you have to input t or not 6.modulo of negative numbers is not a%b, it is a%b + abs(b) */ void solve() { int n; cin >> n; vector<vector<int>> dp(1000010, vector<int> (10, 0)); for (int i = 0; i < 10; i++) { dp[1][i] = 1; } for (int i = 2; i <= n; i++) { for (int j = 1; j <= 9; j++) { for (int k = max(1, j - 1); k <= min(9, j + 1); k++) { dp[i][k] += dp[i - 1][j]; dp[i][k] %= mod; } } } i64 ans = 0; for (int i = 1; i <= 9; i++) { ans = (ans + dp[n][i]) % mod; } cout << ans << endl; return; } signed main() { IOS1; // IOS2; #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif int __t = 1; // cin >> __t; for (int _t = 1; _t <= __t; _t++) { solve(); } return 0; } /* */
找规律,考虑最后的字符串是怎么递归过来的,当然是从第t-1次变换过来的,观察题目给出的变换公式,即假设变的是ABC中的第i个,那么它会变成第(2*i-1)%3和第(2*i)%3,因此可以计算出这个字符是从第t-1次变换的(2*k+1)/2位置的字符变过来的,k如果是奇数说明偏移量为1,是偶数则偏移量是2,然后继续这样不断地递归递归,递归到k=0,然后只需要得出偏移量%3就是最后的字符
AC代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #include<bits/stdc++.h> // #include <iostream> // #include <cstdio> // #include <queue> // #include <deque> // #include <stack> // #include <string> // #include <cstring> // #include <numeric> // #include <functional> // #include <cstdlib> // #include <vector> // #include <set> // #include <map> // #include <algorithm> // #include <cmath> // #include <iomanip> using i64 = long long; using namespace std; #define lowbit(x) ((x) & -(x)) #define endl '\n' #define IOS1 ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define IOS2 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; typedef long long ll; // typedef long long i64; template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T power(T a, int b) { T res = 1; for (; b; b >>= 1, a = a * a) { if (b & 1) { res = res * a; } } return res; } template <typename T> T Myabs(T a) { return a >= 0 ? a : -a; } template <typename T> inline void read(T& x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0', ch = getchar(); } x *= f; } const int INF = 0x3f3f3f3f; // const int mod = 1000000007; const int mod = 998244353; const double PI = acos(-1.0); const double eps = 1e-6; inline int sgn(double x) { return x < -eps ? -1 : x > eps; } /* Tips: 1.int? long long? 2.don't submit wrong answer 3.figure out logic first, then start writing please 4.know about the range 5.check if you have to input t or not 6.modulo of negative numbers is not a%b, it is a%b + abs(b) */ void solve() { string s; cin >> s; int q; cin >> q; while (q--) { i64 t, k; cin >> t >> k; k--; i64 ans = 0; while (t && k) { ans += k % 2 + 1; t--; k >>= 1; } ans += t; int x = s[k] - 'A'; x = (x + ans) % 3; cout << char(x + 'A') << endl; } return; } signed main() { IOS1; // IOS2; #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif int __t = 1; // cin >> __t; for (int _t = 1; _t <= __t; _t++) { solve(); } return 0; } /* */
莫队模板题
AC代码:
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> PII; typedef long long ll; const int N = 5e5 + 10, M = 1e6 + 10; int g[N], pos[N]; int num[N]; struct node { int l, r, idx; friend bool operator <(node a, node b) { if (pos[a.l] != pos[b.l]) return pos[a.l] < pos[b.l]; if (a.r != b.r) return a.r < b.r; return a.idx < b.idx; } }qu[M]; ll res = 0; ll ans[M]; inline int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch=='-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f ? -x : x; } void del(int x) { num[g[x]] --; res -= (num[g[x]] & 1); } void add(int x) { res += (num[g[x]] & 1); num[g[x]] ++; } void solve() { int n = read(); int block = sqrt(n); for (int i = 1; i <= n; i ++ ) g[i] = read(), pos[i] = i / block; int m = read(); for (int i = 1; i <= m; i ++ ) { int l = read(), r = read(); qu[i] = {l, r, i}; } sort(qu + 1, qu + m + 1); int l = 1, r = 0; for (int i = 1; i <= m; i ++ ) { while (l < qu[i].l) del(l++); while (l > qu[i].l) add(--l); while (r < qu[i].r) add(++r); while (r > qu[i].r) del(r --); ans[qu[i].idx] = res; } for (int i = 1; i <= m; i ++ ) printf("%lld\n", ans[i]); } int main() { int t = 1; while (t -- ) solve(); return 0; }
AtCoder Beginner Contest 242(A-D.G莫队模板题)
于 2022-03-10 19:57:07 首次发布