Codeforces Round #690 (Div. 3)
阿新 • • 發佈:2020-12-16
Codeforces Round #690 (Div. 3)
A - Favorite Sequence
按著題意讀入, 順序輸出完事
int main() { IOS; for (cin >> _; _; --_) { cin >> n; rep (i, 1, n) { if (i <= (n + 1 >> 1)) cin >> a[i * 2 - 1]; else cin >> a[n - i + 1 << 1]; } rep (i, 1, n) cout << a[i] << ' '; cout << '\n'; } return 0; }
B - Last Year's Substring
模擬, 比A麻煩
int main() { IOS; for (cin >> _; _; --_) { cin >> n >> s + 1; bool f = 0; if (s[1] == '2' && s[2] == '0' && s[3] == '2' && s[4] == '0') f = 1; else if (s[n - 3] == '2' && s[n - 2] == '0' && s[n - 1] == '2' && s[n] == '0') f = 1; else if (s[1] == '2' && s[2] == '0' && s[3] == '2' && s[n] == '0') f = 1; else if (s[1] == '2' && s[2] == '0' && s[n - 1] == '2' && s[n] == '0') f = 1; else if (s[1] == '2' && s[n - 2] == '0' && s[n - 1] == '2' && s[n] == '0') f = 1; cout << (f ? "YES\n" : "NO\n"); } return 0; }
C - Unique Number
就9個數, 肯定不用0, 從9各位, 開始放就完事
int main() { IOS; for (cin >> _; _; --_) { cin >> n; int i; for (i = 1; i <= 9 && n; ++i) a[i] = min(n, 10 - i), n -= a[i]; if (i == 1 || n) cout << "-1"; else while (--i) cout << a[i]; cout << '\n'; } return 0; }
D - Add to Neighbour and Remove
無非是把序列分段, 每段的和一樣, 直接列舉每段的首尾dp完事, 用unordered_map判斷是否能轉移
複雜度 \(O(n^2)\) 有個unordered_map的常數, 這次最喜歡的一道題
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; unordered_map<int, int> st[n + 1];
rep (i, 1, n) {
cin >> a[i]; a[i] += a[i - 1]; st[i][a[i]] = i - 1;
rep (j, 1, i - 1) if (st[j].count(a[i] - a[j])) st[i][a[i] - a[j]] = st[j][a[i] - a[j]] + i - j - 1;
}
int ans = n - 1;
for (auto &i : st[n]) umin(ans, i.se);
cout << ans << '\n';
}
return 0;
}
E1&E2 - Close Tuples
組合數學
這裡只放E2的程式碼了, E1無非是m,k給定, 不取膜(C用平常約分的方式計算)
int C(int n, int m) {
if (n < m) return 0;
return fac[n] * facinv[m] % mod * facinv[n - m] % mod;
}
int main() {
IOS; inv[0] = inv[1] = fac[0] = facinv[0] = 1;
rep (i, 2, 2e5) inv[i] = (mod - mod / i) * inv[mod % i] % mod;
rep (i, 1, 2e5) fac[i] = fac[i - 1] * i % mod, facinv[i] = facinv[i - 1] * inv[i] % mod;
for (cin >> _; _; --_) {
cin >> n >> m >> k;
rep (i, 1, n) cin >> a[i];
sort(a + 1, a + 1 + n);
ll ans = 0;
for (int i = 1, j = 1; i <= n; ++i) {
while(a[i] - a[j] > k) ++j;
ans = (ans + C(i - j, m - 1)) % mod;
}
cout << ans << '\n';
}
return 0;
}
F - The Treasure of The Segments
無非就是不相交, 右節點再當前段左節點左邊, 左節點再當前段右節點右邊 一定不相交
二分找一下就行, 正難則反
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; VI l(n), r(n);
rep (i, 1, n) cin >> a[i].fi >> a[i].se, l.pb(a[i].fi), r.pb(a[i].se);
sort(all(l)); sort(all(r));
int ans = n - 1;
rep (i, 1, n) umin(ans, lower_bound(all(r), a[i].fi) - r.begin() + n - (upper_bound(all(l), a[i].se) - l.begin()));
cout << ans << '\n';
}
return 0;
}