AcWing第16場周賽題解
阿新 • • 發佈:2022-04-11
A. 3955. 統一大小寫
題目連結:https://www.acwing.com/problem/content/3958/
題目大意:略。
解題思路:迴圈一輪記錄大小寫字母出現次數,然後相應地進行大小寫轉換。
示例程式:
#include <bits/stdc++.h> using namespace std; int T, c; char s[1110]; int main() { cin >> T; while (T--) { cin >> s; c = 0; for (int i = 0; s[i]; i++) isupper(s[i]) ? c++ : c--; for (int i = 0; s[i]; i++) s[i] = (c > 0) ? toupper(s[i]) : tolower(s[i]); puts(s); } return 0; }
B. 3956. 截斷陣列
題目連結:https://www.acwing.com/problem/content/3959/
題目大意:將陣列從中間截斷,得到三個非空子陣列。要求,三個子陣列內各元素之和都相等。
解題思路:字首和 + dp。劃分的第一個位置對應字首和 \(\frac{1}{3}\),劃分的第二個位置對應字首和 \(\frac{2}{3}\)。
示例程式:
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int n, m, a[maxn], c1; long long ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i-1]; } if (a[n] % 3) { cout << 0 << endl; return 0; } for (int i = 1; i < n; i++) { if (a[i] * 3 == a[n] * 2) ans += c1; if (a[i] * 3 == a[n]) c1++; } cout << ans << endl; return 0; }
C. 3957. 子序列
題目連結:https://www.acwing.com/problem/content/3960/
題目大意:在數列中找三個下標同時滿足:
- \(i \lt j \lt k\)
- \(a_i \lt a_j \gt a_k\) 或 \(a_i \gt a_j \lt a_k\)
解題思路:
最短的要麼沒有,要麼長度為 \(3\)。
開四個陣列,對應字首最大值、字首最小值、字尾最大值、字尾最小值的下標。
示例程式:
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int n, a[maxn], f1[maxn], f2[maxn], f3[maxn], f4[maxn]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; f1[1] = f3[1] = 1; for (int i = 2; i <= n; i++) { int p = f1[i-1]; if (a[i] < a[p]) f1[i] = i; else f1[i] = f1[i-1]; p = f3[i-1]; if (a[i] > a[p]) f3[i] = i; else f3[i] = f3[i-1]; } f2[n] = f4[n] = n; for (int i = n-1; i >= 1; i--) { int p = f2[i+1]; if (a[i] < a[p]) f2[i] = i; else f2[i] = f2[i+1]; p = f4[i+1]; if (a[i] > a[p]) f4[i] = i; else f4[i] = f4[i+1]; } for (int i = 2; i < n; i++) { int x = f1[i-1], y = f2[i+1]; if (a[x] < a[i] && a[i] > a[y]) { cout << 3 << endl; cout << x << " " << i << " " << y << endl; return 0; } x = f3[i-1], y = f4[i+1]; if (a[x] > a[i] && a[i] < a[y]) { cout << 3 << endl; cout << x << " " << i << " " << y << endl; return 0; } } cout << 0 << endl; return 0; }