Advertisement
a53

binarysearch

a53
Jan 1st, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void test_case(const int &tc) {
  5. int n;
  6. string s;
  7. cin >> n >> s;
  8. s = '$' + s;
  9. vector<int> lower_true, lower_false;
  10. queue<int> upper_true, upper_false;
  11. for (int i = 1; i <= n; ++i) {
  12. if (s[i] == '1') {
  13. upper_true.emplace(i);
  14. } else {
  15. upper_false.emplace(i);
  16. }
  17. }
  18. int x;
  19. for (x = 1; x <= n; ++x) {
  20. while (!upper_true.empty() && upper_true.front() <= x) {
  21. lower_true.emplace_back(upper_true.front());
  22. upper_true.pop();
  23. }
  24. while (!upper_false.empty() && upper_false.front() <= x) {
  25. lower_false.emplace_back(upper_false.front());
  26. upper_false.pop();
  27. }
  28. if (lower_true.size() + upper_false.size() - (s[x] == '1') == lower_false.size() + upper_true.size() - (s[x] == '0')) {
  29. break;
  30. }
  31. }
  32. if (s[x] == '1') {
  33. lower_true.pop_back();
  34. } else {
  35. lower_false.pop_back();
  36. }
  37. for (const int &it : lower_true) {
  38. cout << it << ' ';
  39. }
  40. while (!upper_false.empty()) {
  41. cout << upper_false.front() << ' ';
  42. upper_false.pop();
  43. }
  44. cout << x << ' ';
  45. for (const int &it : lower_false) {
  46. cout << it << ' ';
  47. }
  48. while (!upper_true.empty()) {
  49. cout << upper_true.front() << ' ';
  50. upper_true.pop();
  51. }
  52. cout << '\n';
  53. }
  54.  
  55. int main() {
  56. int T;
  57. cin >> T;
  58. for (int tc = 1; tc <= T; ++tc)
  59. test_case(tc);
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement