B. Mocha and Red and Blue(贪心)

曾经终败给现在 2022-09-05 13:57 180阅读 0赞

(1)如果第一个位置是问号,分别把当做是R/B,往下模拟
(2)如果第一个位置是B,那就直接往下模拟
(3)如果第一个位置是R,还是直接往下模拟
第二个和第三个只需要一个字符串组就可以完成,但为了copy代码,以及适应下面的求和取最小的计算,需要变成两个字符串组。

  1. // Problem: B. Mocha and Red and Blue
  2. // Contest: Codeforces - Codeforces Round #738 (Div. 2)
  3. // URL: https://codeforces.com/contest/1559/problem/B
  4. // Memory Limit: 256 MB
  5. // Time Limit: 1000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8. #include<iostream>
  9. #include<cstdio>
  10. #include<string>
  11. #include<ctime>
  12. #include<cmath>
  13. #include<cstring>
  14. #include<algorithm>
  15. #include<stack>
  16. #include<climits>
  17. #include<queue>
  18. #include<map>
  19. #include<set>
  20. #include<sstream>
  21. #include<cassert>
  22. #include<bitset>
  23. #include<list>
  24. #include<unordered_map>
  25. using namespace std;
  26. #define lowbit(x) (x&-x)
  27. #define pf(a) printf("%d\n",a)
  28. #define mem(x,y) memset(x,y,sizeof(x))
  29. #define dbg(x) cout << #x << " = " << x << endl
  30. #define rep(i,l,r) for(int i = l; i <= r; i++)
  31. #define fep(i,a,b) for(int i=b;i>=a;--i)
  32. typedef long long ll;
  33. typedef unsigned long long ull;
  34. const int inf=0x3f3f3f3f;
  35. const int N=1e2+100;
  36. int n;
  37. char a[N],b[N],c[N];
  38. void solve()
  39. {
  40. cin >> n;
  41. rep(i,1,n) cin >> a[i];
  42. if(a[1]=='?')
  43. {
  44. b[1] = 'B';
  45. c[1] = 'R';
  46. rep(i,2,n)
  47. {
  48. if(a[i]!='?')
  49. {
  50. b[i] = a[i];
  51. c[i] = a[i];
  52. continue;
  53. }
  54. if(b[i-1]=='B') b[i]='R';
  55. if(b[i-1]=='R') b[i]='B';
  56. if(c[i-1]=='B') c[i]='R';
  57. if(c[i-1]=='R') c[i]='B';
  58. }
  59. }
  60. else if(a[1]=='B')
  61. {
  62. b[1] = 'B';
  63. c[1] = 'B';
  64. rep(i,2,n)
  65. {
  66. if(a[i]!='?')
  67. {
  68. b[i] = a[i];
  69. c[i] = a[i];
  70. continue;
  71. }
  72. if(b[i-1]=='B') b[i]='R';
  73. if(b[i-1]=='R') b[i]='B';
  74. if(c[i-1]=='B') c[i]='R';
  75. if(c[i-1]=='R') c[i]='B';
  76. }
  77. }
  78. else{
  79. b[1] = 'R';
  80. c[1] = 'R';
  81. rep(i,2,n)
  82. {
  83. if(a[i]!='?')
  84. {
  85. b[i] = a[i];
  86. c[i] = a[i];
  87. continue;
  88. }
  89. if(b[i-1]=='B') b[i]='R';
  90. if(b[i-1]=='R') b[i]='B';
  91. if(c[i-1]=='B') c[i]='R';
  92. if(c[i-1]=='R') c[i]='B';
  93. }
  94. }
  95. int a1=0, a2=0;
  96. rep(i,1,n-1)
  97. {
  98. if(b[i]==b[i+1]) a1++;
  99. if(c[i]==c[i+1]) a2++;
  100. }
  101. if(a1>=a2) rep(i,1,n) cout << c[i];
  102. else rep(i,1,n) cout << b[i];
  103. cout << endl;
  104. }
  105. int main()
  106. {
  107. ios::sync_with_stdio(false);cin.tie(0);
  108. int Case;cin >> Case;
  109. while(Case--)
  110. solve();
  111. return 0;
  112. }

发表评论

表情:
评论列表 (有 0 条评论,180人围观)

还没有评论,来说两句吧...

相关阅读

    相关 【AGC014E】Blue and Red Tree

    [题目][Link 1] 正着考虑把边割断感觉非常难以考虑,于是考虑一下将整个过程倒过来,也就是把红边树变成蓝边树 不难发现最后一步我们割断的边肯定是两棵树上都有的边,毕竟