最近沉迷寫前端代碼,如今緩過來簡單補下html
比賽連接:https://ac.nowcoder.com/acm/contest/9667#question前端
簡單的並查集模板ios
待補c++
math數組
// Author : RioTian // Time : 20/12/07 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 10; int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); ll n, cnt = 0; cin >> n; ll ans = n / 4; cnt += ans * 2; ans = n % 4; if (ans < 3) cnt += ans; else cnt++; cout << cnt << endl; }
素數篩反向選spa
// Author : RioTian #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 10; int n; bool isPrime[N]; int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; int cnt = 0; for (int i = 2; i <= n; i++) { if (!isPrime[i]) { cnt++; for (int j = i * 2; j <= n; j += i) { isPrime[j] = 1; } } } // cout << cnt << endl; cout << (n <= 3 ? -1 : cnt + 2) << endl; }
模擬題code
若是兩數字位數不一樣就先補零便於後面處理htm
// Author : RioTian // Time : 20/12/07 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 10; string a, b, ans; int cot = 0, len; bool lin; int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> a >> b; if (a.size() > b.size()) { cot = a.size() - b.size(); while (cot--) b = '0' + b; } else { cot = b.size() - a.size(); while (cot--) a = '0' + a; } len = a.size(); ans = ""; lin = true; for (int i = 0; i < len; i++) { if ((a[i] - '0' + b[i] - '0') % 10 == 0 && lin) continue; lin = false; ans += (char)((a[i] - '0' + b[i] - '0') % 10 + '0'); } if (ans.size() == 0) ans += "0"; cout << ans << endl; }
模擬一下樣例1,會發現16 + 15,容易發下16爲5項和,而\(15 = max(a[i]) * (n - 2)\)對象
// Author : RioTian // Time : 20/12/07 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 10; int n; ll a[N]; int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; ll cnt = 0, Max = 0; for (int i = 1; i <= n; ++i) { Max = max(Max, a[i]); cnt += a[i]; } cout << Max * (n - 2) + cnt; }
相似二分圖,在知足條件的狀況下儘可能增長匹配數。blog
直接寫二分圖確定很麻煩,但轉過來一想,爲數組排序呢?
// Author : RioTian // Time : 20/12/07 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 10; int n, m; ll a[N], b[N]; int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= m; ++i) cin >> b[i]; sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + m); int i, j; int ans = 0, l = 1, r = n; for (i = m; i >= 1; i--) { if (b[i] < a[r]) r--, ans++; else l++; } cout << ans << endl; }
看到題的瞬間想用線段樹,但簡單算了下時間複雜度,可能會超時,因此轉而想了想主席樹(沒了解的同窗能夠看這裏:Here),但以爲牛客應該不會在小白賽出這種碼量大的題。
被迫無奈看了下題解,發現原來快速寫下優先隊列便可(都沒想到這個)
如下是AC代碼
// Author : RioTian // Time : 20/12/07 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 10; ll a[N]; priority_queue<ll> que; int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; sort(a + 1, a + 1 + n); int ans = min(n, k); for (int i = 1; i <= ans; ++i) que.push(a[i]); int opt, x; while (m--) { cin >> opt; if (opt == 1) { cin >> x; if (que.size() <= k - 1) que.push(x); else { if (x > que.top()) continue; que.push(x), que.pop(); } } else { if (que.size() != k) cout << -1 << endl; else cout << que.top() << endl; } } }
這兩個待補