思路:
分別記錄字母A, B, C的數量,顯然要滿足B的數量等於A的數量和C的數量的和才行
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #define x first #define y second using namespace std; typedef long long LL; typedef pair<int, int>PII; const int N = 100010; int a[N]; vector<PII>index[100010]; int main() { int T; cin >> T; while (T--) { string s; cin >> s; int cnta = 0, cntb = 0, cntc = 0; for (int i = 0; i < s.size(); i++) if (s[i] == 'A') cnta++; else if (s[i] == 'B') cntb++; else if (s[i] == 'C') cntc++; if (cntb == cnta + cntc) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
題意:
題意有點難懂,意思是給你規定了一種操作,問要怎么進行一些列的這種操作才能使原數組遞增
思路:
比賽的時候想了了土方法,類比冒泡排序從后往前枚舉,遇到一個逆序對(a[i] > a[j]且,i < j)就整體前移一個,由於這個題數據范圍只有50,所以O(n^3)完全ok的
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #define x first #define y second using namespace std; typedef long long LL; typedef pair<int, int>PII; const int N = 55; vector<PII>index[100010]; int main() { int T; cin >> T; while (T--) { int n; cin >> n; int a[N]; for (int i = 1; i <= n; i++) cin >> a[i]; int tmp[N], b[N]; for (int i = 1; i <= n; i++) tmp[i] = a[i], b[i] = a[i]; sort(tmp + 1, tmp + 1 + n); bool flag = true; for (int i = 1; i <= n; i++) if (tmp[i] != a[i]) flag = false; if (flag) { cout << 0 << endl; continue; } //else int k = 0; //k是逆序對的數量 for (int i = n; i >= 1; i--) for (int j = i - 1; j >= 1; j--) if (b[i] < b[j]) { int x = b[j];//整體往前一個 for (int p = j; p <= i - 1; p++) b[p] = b[p + 1]; b[i] = x; k++; } cout << k << endl; int l = 0, r = 0, d = 1; for (int i = n; i >= 1; i--) for (int j = i - 1; j >= 1; j--) if (a[i] < a[j]) { cout << j << ' ' << i << ' ' << 1 << endl; int x = a[j];//整體往前一個 for (int p = j; p <= i - 1; p++) a[p] = a[p + 1]; a[i] = x; } } return 0; }
思路:
優先隊列,同時再開一個idx數組記錄兩個交談的人的下標,每次取出兩個隊頭元素,然后判斷是否大於1,大於一都自減一,然后再放回隊列,直到隊列中 元素只有一個或沒有,需要注意的是,我們剛開始往隊列中加元素的時候只存大於0的元素
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #define x first #define y second using namespace std; typedef long long LL; typedef pair<int, int>PII; const int N = 200010; int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { int n; cin >> n; vector<PII>idx; idx.clear(); priority_queue<PII>q; for (int i = 1; i <= n; i++) { int x; cin >> x; if(x) q.push({ x, i }); } while (q.size() > 1) { auto a = q.top(); q.pop(); auto b = q.top(); q.pop(); idx.push_back({ a.second, b.second }); if (a.first - 1 > 0) q.push({ a.first - 1, a.second }); if (b.first - 1 > 0) q.push({ b.first - 1, b.second }); } if (q.size()) q.pop(); cout << idx.size() << endl; for (int i = 0; i < idx.size(); i++) cout << idx[i].first << ' ' << idx[i].second << endl; } return 0; }
E1. Permutation Minimization by Deque
思路:
貪心 + 雙端隊列,每次比較和隊頭元素的大小,大於隊頭元素就插到隊尾,小於就插到隊首
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #define x first #define y second using namespace std; typedef long long LL; typedef pair<int, int>PII; const int N = 200010; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { memset(a, 0, sizeof a); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; deque<int>q; q.push_back(a[1]); for (int i = 2; i <= n; i++) if (a[i] < q.front()) q.push_front(a[i]); else q.push_back(a[i]); for (auto x : q) cout << x << ' '; cout << endl; } return 0; }
上了119分,還行吧