fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. long int n,q,k;
  6. cin>>n>>q>>k;
  7. vector<long int>A(n);
  8. for(int i=0;i<n;i++){
  9. cin>>A[i];
  10. }
  11. string s;
  12. cin>>s;
  13. for(int i=1;i<n;i++){
  14. if(A[i]!=0){
  15. A[i]=A[i]+A[i-1];
  16. }
  17. }
  18. long int a=-1,b=-1,c=-1,d=-1,x=0,y=0;
  19. if(A[0]!=0&&A[n-1]!=0){
  20.  
  21. a=n-A[n-1];
  22.  
  23. long int i=0;
  24. while(A[i+1]!=0){
  25. i++;
  26. }
  27. if(i!=n-1){
  28. x=A[i]+A[n-1];
  29. }
  30. else{
  31. x=A[i]+A[n-1]-1;
  32. }
  33. b=i;
  34. for(int j=b+1;j<a;j++){
  35. if(A[j]!=0){
  36.  
  37. while(j<a){
  38. j++;
  39. if(A[j]==0){
  40. if(A[j-1]>x){
  41. c=a;
  42. d=b;
  43. y=x;
  44. a=j-A[j-1];
  45. b=j-1;
  46. x=A[j-1];
  47.  
  48. }
  49. else if(A[j-1]>y){
  50. c=j-A[j-1];
  51. d=j-1;
  52. y=A[j-1];
  53. }
  54. break;
  55. }
  56. }
  57.  
  58. }
  59. }
  60.  
  61. }
  62. else{
  63.  
  64. for(int j=0;j<n;j++){
  65. if(A[j]!=0){
  66.  
  67. while(j<n){
  68. j++;
  69. if(A[j]==0||j==n){
  70.  
  71. if(A[j-1]>x){
  72. c=a;
  73. d=b;
  74. y=x;
  75. a=j-A[j-1];
  76. b=j-1;
  77. x=A[j-1];
  78. }
  79. else if(A[j-1]>y){
  80. c=j-A[j-1];
  81. d=j-1;
  82. y=A[j-1];
  83. }
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. //cout<<a<<" "<<b<<" "<<x<<endl<<c<<" "<<d<<" "<<y<<endl;
  91.  
  92. for(int i=0;i<q;i++){
  93. long int ans=0;
  94. if(s[i]=='!'){
  95. if(a!=-1){
  96. a=(a+1)%n;
  97. b=(b+1)%n;
  98. }
  99. if(c!=-1){
  100. c=(c+1)%n;
  101. d=(d+1)%n;
  102. }
  103. }
  104. if(s[i]=='?'){
  105. if(a!=-1&&b!=-1){
  106. if(b==-1){
  107. if(a<=b){
  108. ans=x;
  109. }
  110. else{
  111. long int pp=n-a;
  112. long int qq=x-pp;
  113. ans=max(pp,qq);
  114. }
  115. }
  116. else{
  117. if(a<=b){
  118. ans=x;
  119. }
  120. else{
  121. long int rr=y;
  122. long int pp=n-a;
  123. long int qq=x-pp;
  124. ans=max(pp,qq);
  125. ans=max(ans,rr);
  126. }
  127. }
  128. }
  129. else{
  130. ans=0;
  131. }
  132. if(ans>k){
  133. cout<<k<<endl;
  134. }
  135. else{
  136. cout<<ans<<endl;
  137. }
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144. return 0;
  145. }
  146.  
  147.  
Runtime error #stdin #stdout #stderr 0s 80768KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc