• CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) C. Complementary XOR 题解


    原题链接:

    Problem - C - Codeforces (Unofficial mirror by Menci)

    题目描述:

    You have two binary strings aa and bb of length nn. You would like to make all the elements of both strings equal to 00. Unfortunately, you can modify the contents of these strings using only the following operation:

    • You choose two indices ll and rr (1≤l≤r≤n1≤l≤r≤n);
    • For every ii that respects l≤i≤rl≤i≤r, change aiai to the opposite. That is, ai:=1−aiai:=1−ai;
    • For every ii that respects either 1≤i

    Your task is to determine if this is possible, and if it is, to find such an appropriate chain of operations. The number of operations should not exceed n+5n+5. It can be proven that if such chain of operations exists, one exists with at most n+5n+5 operations.

    Input

    Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051≤t≤105) — the number of test cases. The description of test cases follows.

    The first line of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the strings.

    The second line of each test case contains a binary string aa, consisting only of characters 0 and 1, of length nn.

    The third line of each test case contains a binary string bb, consisting only of characters 0 and 1, of length nn.

    It is guaranteed that sum of nn over all test cases doesn't exceed 2⋅1052⋅105.

    Output

    For each testcase, print first "YES" if it's possible to make all the elements of both strings equal to 00. Otherwise, print "NO". If the answer is "YES", on the next line print a single integer kk (0≤k≤n+50≤k≤n+5) — the number of operations. Then kk lines follows, each contains two integers ll and rr (1≤l≤r≤n1≤l≤r≤n) — the description of the operation.

    If there are several correct answers, print any of them.

    Example

    题目大意:

    给定两个二进制字符串a和b,给定一种可执行的操作:

    选定一个区间[l,r]对a的[l,r]内的字符进行取反,并对b的[1,l - 1]和[r + 1, n]进行取反。

    问有没有可能在若干次操作之后使得两个字符串都变成全0.

    解题思路:

    我们反过来思考,初始状态a,b都是全零,通过操作能到达哪些状态.

    我们随便模拟几次可以发现操作奇数次后a,b一定每一位都不同,操作偶数次后a,b一定每一位都相同.

    所以只有a,b每一位都不同或者都相同才能通过操作到达全0状态.

    然后我们就很容易构造出一种方案了,我的方法是先把a串全部变为0,此时如果b只可能是全0或者全1,如果是全0那就不用操作了,否则就依次操作[1,n],[1,1],[2,n],即先把a全变为1,然后按照样例的方法把全1的a,b变为全0.

    代码(CPP):

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. using LL = long long;
    6. const int maxn = 2e5 + 5;
    7. int a[maxn], b[maxn];
    8. int main()
    9. {
    10. cin.tie(0);
    11. cout.tie(0);
    12. ios::sync_with_stdio(0);
    13. int T;
    14. cin >> T;
    15. while (T--)
    16. {
    17. int n;
    18. cin >> n;
    19. for (int i = 1; i <= n; i++)
    20. {
    21. char c;
    22. cin >> c;
    23. a[i] = c - '0';
    24. }
    25. int cnt = 0;
    26. for (int i = 1; i <= n; i++)
    27. {
    28. char c;
    29. cin >> c;
    30. b[i] = c - '0';
    31. cnt += (b[i] == a[i]);
    32. }
    33. if (cnt != n && cnt != 0)
    34. {
    35. cout << "NO" << '\n';
    36. continue;
    37. }
    38. bool flag = cnt == n ? 0 : 1;
    39. vectorint, int>> ans;
    40. for (int i = 1; i <= n; i++)
    41. {
    42. if (a[i] == 0)
    43. continue;
    44. int j = i;
    45. while (j + 1 <= n && a[j + 1] == 1)
    46. j++;
    47. ans.push_back({i, j});
    48. flag ^= 1;
    49. i = j;
    50. }
    51. if (flag)
    52. {
    53. ans.push_back({1, n});
    54. ans.push_back({1, 1});
    55. ans.push_back({2, n});
    56. }
    57. cout << "YES" << '\n';
    58. cout << ans.size() << '\n';
    59. for (auto [x, y] : ans)
    60. cout << x << ' ' << y << '\n';
    61. }
    62. return 0;
    63. }

  • 相关阅读:
    海思万能平台搭建:颜色空间转换YUV2RGB
    蔟/块/页/扇区
    HTB-ScriptKiddie
    PDF文件怎么合并在一起?这三种方法快利用起来
    Python的基础语法(八)(持续更新)
    C++ 单向链表手动实现(课后作业版)
    SpringBoot配置文件
    8Manage PM:通过项目管理信息系统做好进度管控
    SpringBoot 06: springboot中使用redis
    【力扣经典面试题】238. 除自身以外数组的乘积
  • 原文地址:https://blog.csdn.net/qq_45554473/article/details/127871527