個人主頁:Guiat
歸屬專欄:算法競賽
文章目錄
- 1. MC0455 · 四大名著-西游簽到
- 2. MC0456 · 斬斷靈藤
- 3. MC0457 · 符咒封印
- 4. MC0458 · 移鐵術
- 5. MC0459 · 昆侖墟
- 6. MC0460 · 星空迷軌陣
- 7. MC0461 · 排隊
- 8. MC0462 · 最后一難
正文
總共8道題。
1. MC0455 · 四大名著-西游簽到
【題目】 MC0455 · 四大名著-西游簽到
【AC_Code】
#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;const int N = 2e4 + 10; char s[N], t[N];void solve()
{int T; cin >> T;while (T --){cin >> s + 1 >> t + 1;int l = 1, r = strlen(s + 1);while (l < r && s[l] == t[l]) l ++;while (l < r && s[r] == t[r]) r --;int len = r - l + 1, check = 1;for (int i = 0; i < len; i ++) if (s[l + i] != t[r - i]) check = 0;if (check) cout << "Y\n"; else cout << "N\n"; }
}int main()
{IOS int _ = 1; // cin >> _;while (_ --) solve(); return 0;
}
2. MC0456 · 斬斷靈藤
【題目】MC0456 · 斬斷靈藤
【AC_Code】
#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;void solve()
{int n, m; cin >> n >> m; vector<vector<int>> c(n);for (int i = 0; i < n - 1; i ++){int u, v; cin >> u >> v; u --; v --;c[u].push_back(v); c[v].push_back(u);}function<int(int, int)> s; int ans = 0;s = [&](int h, int i){vector<int> k;for (int l : c[h]){if (l == i) continue;int m = s(l, h); k.push_back(m);}sort(k.begin(), k.end());int a = 1;for (int p : k){if (a + p > m && p > 0) ans ++;else if (a + p <= m) a += p;}return a;};if (s(0, -1) > 0) ans += (1 * 1);cout << ans << '\n';
}int main()
{IOS int _ = 1; // cin >> _;while (_ --) solve(); return 0;
}
3. MC0457 · 符咒封印
【題目】MC0457 · 符咒封印
【AC_Code】
#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;
using ll = long long;const int N = 5e5 + 10, mod = 998244353;
ll a[N], b[N], c[N], ans;void solve()
{int n, q; cin >> n >> q;for (int i = 1; i <= n; i ++){cin >> a[i];b[i] = (b[i - 1] + a[i]) % mod;c[i] = (c[i - 1] + a[i] * i) % mod;}while (q --){int l, r; cin >> l >> r;ll x = c[r] - c[l - 1], y = b[r] - b[l - 1], z = (l - 1) * y;ans = (x - z) % mod; ans = (ans + mod) % mod; cout << ans << '\n';}
}int main()
{IOS int _ = 1; // cin >> _;while (_ --) solve(); return 0;
}
4. MC0458 · 移鐵術
【題目】MC0458 · 移鐵術
【AC_Code】
5. MC0459 · 昆侖墟
【題目】MC0459 · 昆侖墟
【AC_Code】
6. MC0460 · 星空迷軌陣
【題目】MC0460 · 星空迷軌陣
【AC_Code】
7. MC0461 · 排隊
【題目】MC0461 · 排隊
【AC_Code】
#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;void solve()
{int T; cin >> T;while (T --){int n; cin >> n; vector<int> a[n + 1]; priority_queue<int> q;for (int i = 1; i <= n; i ++){int opt; cin >> opt; int x, y;if (opt == 1) { cin >> x; y = n - 1; }else if (opt == 2) { cin >> y; x = 0; }else cin >> x >> y;swap(x, y); x = n - x; y = n - y; a[x].push_back(y);}int ans = 1;for (int i = 1; i <= n; i ++){for (unsigned j = 0; j < a[i].size(); j ++) q.push(-a[i][j]);if (q.empty()) { ans = 0; break; }int x = -q.top(); q.pop();if (x < i) { ans = 0; break; }}cout << (ans ? 'Y' : 'N') << '\n';}
}int main()
{IOS int _ = 1; // cin >> _;while (_ --) solve(); return 0;
}
8. MC0462 · 最后一難
【題目】MC0462 · 最后一難
【AC_Code】
#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;size_t t = 0; int ans = 0;void solve()
{string s; cin >> s;while ((t = s.find("matiji", t)) != string :: npos) { t += 5; ans ++; }cout << ans << '\n';
}int main()
{IOS int _ = 1; // cin >> _;while (_ --) solve(); return 0;
}
結語
感謝您的閱讀!期待您的一鍵三連!歡迎指正!