• Codeforces Round #798 (Div. 2) - E. ANDfinity


    题目链接:https://codeforces.com/contest/1689/problem/E

    解题思路:

            根据位运算可以简单的推算出,其实至多只需要2次操作就可以了。也就是操作最小bit位最大的那个,极端情况下需要对两个最小bit位最大的进行操作,一个+1,一个-1。

    1. #include
    2. using namespace std;
    3. #define lson l, mid, rt<<1
    4. #define rson mid+1, r, rt<<1|1
    5. typedef long long ll;
    6. typedef unsigned long long ull;
    7. const int mx = 2e3 + 10;
    8. const int mod = 998244353;
    9. typedef pair <int, int> Pa;
    10. int a[mx];
    11. int fa[mx];
    12. set <int> st, vec[30], val[mx];
    13. map <int, int> mp;
    14. int ans;
    15. int findfa(int x) {
    16. return x == fa[x]? x : fa[x] = findfa(fa[x]);
    17. }
    18. int cal(int n)
    19. {
    20. st.clear();
    21. for (int i=1;i<=n;i++)
    22. fa[i] = i;
    23. for (int i=0; i<30; i++) {
    24. if (vec[i].size() > 1) {
    25. auto it1 = vec[i].begin();
    26. auto it2 = it1;
    27. it2++;
    28. while (it2 != vec[i].end()) {
    29. int fu = findfa(*it1);
    30. int fv = findfa(*it2);
    31. fa[fv] = fu;
    32. it2++, it1++;
    33. }
    34. }
    35. }
    36. for (int i=1;i<=n;i++) {
    37. int fu = findfa(i);
    38. st.insert(fu);
    39. }
    40. return st.size();
    41. }
    42. void print(int n)
    43. {
    44. printf("%d\n", ans);
    45. for (int i=1;i<=n;i++)
    46. printf("%d%c", a[i], i==n?'\n':' ');
    47. }
    48. int main()
    49. {
    50. for (int i=0;i<30;i++)
    51. mp[1<
    52. int t;
    53. scanf("%d", &t);
    54. while (t--) {
    55. int n;
    56. scanf("%d", &n);
    57. for (int i=0;i<30;i++)
    58. vec[i].clear();
    59. ans = 0;
    60. for (int i=1;i<=n;i++) {
    61. val[i].clear();
    62. fa[i] = i;
    63. scanf("%d", a+i);
    64. if (a[i] == 0)
    65. ans++, a[i]++;
    66. for (int j=0; (1<
    67. if ((1<
    68. vec[j].insert(i);
    69. val[i].insert(j);
    70. }
    71. }
    72. }
    73. if (cal(n) == 1) {
    74. print(n);
    75. continue;
    76. }
    77. for (int i=1;i<=n;i++) {
    78. if (a[i] != 1) {
    79. int bit_val = a[i] - (a[i] & (a[i] - 1));
    80. int bit_pos = mp[bit_val];
    81. a[i]--;
    82. vec[bit_pos].erase(i);
    83. for (int j=0;j
    84. vec[j].insert(i);
    85. //printf("########### %d %d\n", i, bit_pos);
    86. if (cal(n) == 1) {
    87. ans++;
    88. print(n);
    89. break;
    90. }
    91. a[i]++;
    92. vec[bit_pos].insert(i);
    93. for (int j=0;j
    94. vec[j].erase(i);
    95. }
    96. for (int v: val[i])
    97. vec[v].erase(i);
    98. set <int> temp_st;
    99. a[i]++;
    100. for (int j=0; (1<
    101. if ((1<
    102. vec[j].insert(i);
    103. temp_st.insert(j);
    104. }
    105. }
    106. if (cal(n) == 1) {
    107. ans++;
    108. print(n);
    109. break;
    110. }
    111. a[i]--;
    112. for (int v: temp_st)
    113. vec[v].erase(i);
    114. for (int v: val[i])
    115. vec[v].insert(i);
    116. }
    117. if (st.size() != 1) {
    118. int max_val = 0, pos;
    119. for (int i=1;i<=n;i++) {
    120. int bit_val = a[i] - (a[i] & (a[i] - 1));
    121. if (bit_val > max_val) {
    122. max_val = bit_val;
    123. pos = i;
    124. }
    125. }
    126. a[pos]--;
    127. for (int i=1;i<=n;i++) {
    128. if (a[i] & max_val) {
    129. a[i]++;
    130. break;
    131. }
    132. }
    133. ans += 2;
    134. print(n);
    135. }
    136. }
    137. return 0;
    138. }
    139. // 8
    140. // 8
    141. // 4 4 8 8 16 16 32 32

  • 相关阅读:
    BCC源码内容概览(1)
    DEVOPS: 认证与调度
    设计模式笔记
    Web Components详解-Shadow DOM基础
    protobuf对象与JSON相互转换
    c++编程实例
    低代码开发平台,来自“未来”的软件开发方案
    知行合一
    JZ2440笔记:热插拔驱动
    1. 关于pytorch中的数据操作的广播机制
  • 原文地址:https://blog.csdn.net/a1214034447/article/details/126409412