voidtest_case(){ string s; cin >> s; int a = 0, b = 0, c = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'A') ++a; elseif (s[i] == 'B') ++b; else ++c; } if (a + c == b) cout << "YES" << endl; else cout << "NO" << endl; }
voidtest_case(){ int n; cin >> n; deque<int> que; for (int i = 1; i <= n; ++i) { int x; cin >> x; if (i == 1) { que.push_back(x); continue; } if (x <= que.front()) que.push_front(x); else que.push_back(x); } for (auto e : que) cout << e << ' '; cout << endl; }
constint maxn = 2e5 + 5; int n; int a[maxn], b[maxn]; //离散化后的数组 int c[maxn]; //树状数组 structnode { int val, id; } in[maxn]; intlowbit(int i){ return i & (-i); } voidupdate(int i, int k){ while (i <= n) { c[i] += k; i += lowbit(i); } } intgetsum(int i){ int res = 0; while (i > 0) { res += c[i]; i -= lowbit(i); } return res; } voidtest_case(){ cin >> n; //存值 for (int i = 1; i <= n; i++) { cin >> a[i]; //a[]到时候还得用来索引 b[i] = a[i]; //生成a[]的副本 } //排序 sort(b + 1, b + 1 + n); //去重 int len = unique(b + 1, b + 1 + n) - b - 1; //len为去重后的数组长度 for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b; //赋给离散的值 int maxx = 0; for (int i = 1; i <= n; ++i) maxx = max(maxx, a[i]); //树状数组求逆序 memset(c, 0, sizeof(c)); longlong ans = 0; //要是处理很多数,即使离散化了也很可能超出int范围 for (int i = 1; i <= n; i++) { update(a[i], 1); //1代表该位置数存在 ans += min(i - getsum(a[i]), getsum(a[i] - 1)); } cout << ans << endl; }
constint maxn = 1e6 + 5; int n, d; int a[maxn]; voidtest_case(){ cin >> n >> d; for (int i = 0; i <= n - 1; ++i) cin >> a[i]; int res = 0; priority_queue<pii, vector<pii>, greater<pii> > que; //大顶堆每次选择循环次数最小的 for (int i = 0; i <= n - 1; ++i) if (!a[i]) que.push({0, i}); //循环次数和0的位置 while (que.size()) { auto now = que.top(); que.pop(); int nex = (now.second + d) % n; if (!a[nex]) continue; else { a[nex] = 0; que.push({now.first + 1, nex}); res = max(res, now.first + 1); } } bool flag = true; for (int i = 0; i <= n - 1; ++i) if (a[i]) { flag = false; break; } if (flag) cout << res << endl; else cout << -1 << endl; }
boolcheck(int x){ bitset<2005> dp, tmp; for (int i = 0; i <= x; ++i) dp[i] = 1; tmp = dp; //记录当前轮情况 for (int i = 1; i <= n; ++i) { //滚动数组求下一轮情况 dp = (dp << a[i]) | (dp >> a[i]); dp &= tmp; //防止跳出界用上一轮限制一下 } return dp.any(); }
voidtest_case(){ cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; int l = 0, r = 2001; while (l < r) { //找下界 int mid = l + r >> 1; if (check(mid)) r = mid; else l = mid + 1; } cout << l << endl; }